在视图控制器之间切换错误

问题描述

| 我在切换视图控制器时遇到了一个问题。 我遵循了此页面上的建议: 视图控制器:如何以编程方式在视图之间切换? 我的设置如下:我有一个带有按钮的启动屏幕,该按钮加载了我的第二个视图控制器。我将IntroductionViewController作为根视图,并且按钮显示“加载网站”,该加载网站事件我想切换到WebsiteViewController。 在IntroductionViewController上,我将按钮设置为IBOutlet和IBAction,单击时NSLogging很好,所以我知道该方法有效。 我需要的是,当您单击“加载网站”时,它将打开WebsiteViewController。到目前为止,我能相信上面的问题: 简介ViewController.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
简介ViewController.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)interfaceOr‌​ientation 
 { 
      // Overriden to allow any orientation. 
      return ((interfaceOrientation==UIInterfaceOrientationLandscapeLeft) || (interfaceOrientation==UIInterfaceOrientationLandscapeRight)); 
 }
对于方向问题,请再三考虑,您可以在显示以下内容时将Frame设置为视图
 - [yourViewController.view setFrame:CGRectMake(0,1024,768)] //iPad and 
 - [yourViewController.view setFrame:CGRectMake(0,480,320)] //iPhone.
可能是这样。 打蜡     ,似乎您没有分配websiteViewController,没有分配它,就没有使用或引用的实例,对其进行记录将仅返回null。 如果您未实现
UINavigationController
,我也将尝试呈现
ModalViewController
。这是分配您的webview控制器并将其显示为modalView的示例:
-(IBAction)loadWebsite:(id)sender{
    WebsiteViewController *webView = [[WebsiteViewController alloc] init];
    [self.view presentModalViewController:webView animated:YES];
    [webView release];
}
默认过渡样式为
UIModalTransitionStyleCoverVertical
,但请查阅文档以获取更多信息。您可以这样设置过渡样式:
webView.modalTransitionStyle = UIModalTransitionStyleCoverVertical
    ,我敢打赌,您的WebsiteViewController对象尚未实例化。
NSLog(@\"%@\",websiteViewController);
我打赌它为空! :p您需要创建对象-我不知道如何使用界面生成器来执行此操作。 无论如何,您正在使用旧方法在视图控制器之间进行切换。新方法/最好的方法是使用导航控制器执行以下操作:
-(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提供给navigationController,而navigationController本身完成所有添加和删除视图的工作! 您根本不需要使用界面生成器! :p(嗯...现在您必须学习如何构建视图而无需拖放!)。 无论如何...我希望能有所帮助。