在视图控制器之间切换错误
我在切换视图控制器时遇到了一些问题。在视图控制器之间切换错误
我跟着这个页面上的建议:
View Controllers: How to switch between views programmatically?
我的设置如下;我有一个启动屏幕上有一个按钮,该按钮加载我的第二个视图控制器。我有IntroductionViewController作为根视图,并且按钮显示“Load Website”,这个Load Website事件我想切换到WebsiteViewController。
关于IntroductionViewController我已经设置了按钮作为一个IBOutlet和IBAction这是NSLogging很好点击,所以我知道该方法正在工作。
我需要的是当你点击加载网站它打开WebsiteViewController。我至今是上面的问题的信用:
IntroductionViewController.h
#import <UIKit/UIKit.h>
#import "WebsiteViewController.h"
@class WebsiteViewController;
@interface IntroductionViewController : UIViewController {
IBOutlet UIButton *websiteButton;
IBOutlet WebsiteViewController *websiteViewController;
}
@property (nonatomic, retain) UIButton *websiteButton;
@property (nonatomic, retain) WebsiteViewController *websiteViewController;
-(IBAction)loadWebsite:(id)sender;
@end
IntroductionViewController.m
#import "IntroductionViewController.h"
@implementation IntroductionViewController
@synthesize websiteButton;
@synthesize websiteViewController;
-(IBAction)loadWebsite:(id)sender{
if(self.websiteViewController.view.superview == nil)
{
[self.view addSubview:websiteViewController.view];
}
}
行:[self.view addSubview:websiteViewController.view];
没有做任何事情,但我以前说过,这个函数是NSLogging很好的。我不知道如何继续这个。
可能您可能忘记将IBOutlet绑定到您的WebsiteViewController。而对于这两个方向的支持,你可以使用
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Overriden to allow any orientation.
return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight));
}
一个多想为你的定位问题,你可以SETFRAME到您的视图,同时显示像
- [yourViewController.view setFrame:CGRectMake(0,0,1024,768)] //iPad and
- [yourViewController.view setFrame:CGRectMake(0,0,480,320)] //iPhone.
可能是这样工作的。
Thax。
我敢打赌你的WebsiteViewController对象没有被实例化。 NSLog(@"%@", websiteViewController);
我敢打赌它是空的! :p您需要创建对象 - 我不知道如何使用接口生成器来完成此操作。
无论如何,您正在使用旧的方式在视图控制器之间切换。新way /最好的方法是使用导航控制器做这样的:
-(IBAction)loadWebsite:(id)sender{
[self.navigationController pushViewController:websiteViewController animated:YES];
}
但是,除非你已经设置了导航控制器,我不知道该怎么做,这将不起作用在界面构建器中。这就是为什么我讨厌界面生成器,并相信你应该停止学习! :P
没有界面生成器,这是你应该做的:
在你的应用程序代理.h文件中在顶部加入这样的:
#include "IntroductionViewController.h"
然后包括这与在其他@propertys代码:
@property (nonatomic, retain) UINavigationController *navController;
现在,在您的应用程序代理.m文件交换应用程序没有完成与此选项启动:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
[self.window makeKeyAndVisible];
// lets make the navigation controller by setting up a view controller first.
IntroductionViewController *viewController = [[IntroductionViewController alloc] initWithNibName:nil bundle:nil];
viewController.title = @"Home";
navController = [[UINavigationController alloc] initWithRootViewController:viewController];
[navController setNavigationBarHidden:NO]; // set to yes to hide the title bar
[viewController release];
[self.window addSubview:navController.view];
return YES;
}
这将给你一个导航控制器(你可以很容易地隐藏标题栏... - 见评论)。并显示第一个视图控制器(IntroductionViewController)。
现在运行我上面给你的代码将工作。该[self.navigationController pushViewController:websiteViewController animated:YES];
。这给viewController导航控制器和导航控制器做所有添加和删除视图等等本身!
而且您根本不必使用界面构建器! :p(好吧......现在你必须学会如何在不拖拽的情况下构建视图!)。
无论如何......我希望有所帮助。
谢谢,我没有使用导航控制器方法的原因是因为我不想要导航控制器栏。现在我知道你可以隐藏它,这节省了我一些努力。我没有将websiteViewController链接到接口生成器对象,这就是为什么它返回NULL。 – 2011-05-25 10:46:42
看起来像你没有分配你的websiteViewController,没有分配它你没有它的实例来使用或引用它,记录它将简单地返回null。
如果您未执行UINavigationController
,我也会尝试呈现一个ModalViewController
。这是分配好自己的网页流量控制器,并将其作为一个modalView的例子:
-(IBAction)loadWebsite:(id)sender{
WebsiteViewController *webView = [[WebsiteViewController alloc] init];
[self.view presentModalViewController:webView animated:YES];
[webView release];
}
默认过渡的风格是UIModalTransitionStyleCoverVertical
但检查出documentation更多。您可以像这样设置过渡样式:
webView.modalTransitionStyle = UIModalTransitionStyleCoverVertical
最简单的答案...它的工作,thx – 2011-07-11 20:37:23
你为什么不只是推动websiteViewController? – singingAtom 2011-05-25 10:16:32
@Daniel只是一个想法。你有绑定的IBOutlet的WebsiteViewController? – 2011-05-25 10:19:18
@Jennis,那会是问题!但是,IntroductionViewController和WebsiteViewController都是横向的,当显示WebsiteViewController时,它位于左侧,右侧位于iPad的顶部。我如何使两个视图控制器的方向都能够坐在风景中? – 2011-05-25 10:24:34