基本视图切换代码崩溃iPhone模拟器

时间:2011-06-23 23:18:02

标签: iphone xcode sdk

我一直试图从两个独立的视图控制器中切换两个视图一段时间,它永远不会工作,模拟器总是崩溃到主屏幕。我正在使用Xcode 3.2.5,这是我的代码 -

SwitchViewsViewController.h

#import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface SwitchViewsViewController : UIViewController {
}
-(IBAction)pushButton;
@end

SwitchViewsViewController.m

#import "SwitchViewsViewController.h"
#import "SecondViewController.h"

@implementation SwitchViewsViewController

-(IBAction)pushButton {
    SecondViewController *screen = [[SecondViewController alloc] initWithNibName:nil bundle:nil    
    screen.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    [self presentModalViewController:screen animated:YES]
    [screen release];
}

SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController {
}
-(IBAction)pushBack;
@end

SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

-(IBAction)pushBack{
    [self dismissModalViewControllerAnimated:YES];
}

在界面构建器中,我所做的就是链接文件的所有者类和按钮。还使SwitchViewsViewController首先加载,而不是MainWindow。一切都在构建,但是当我尝试运行应用程序时,它会崩溃并将其发送到主屏幕。任何人都可以帮我这个吗?

2 个答案:

答案 0 :(得分:0)

当您使用nib名称nil分配您的secondViewController init时,这实际上应该是您的nib文件的名称。

SecondViewController * screen = [[SecondViewController alloc] initWithNibName: nil bundle:nil];

此外,您应该关闭括号; - )

答案 1 :(得分:0)

编译时很少发现XIB错误。因此,当您说SwitchViewsViewController作为应用程序的起点时,我假设您已更改Info.plist中的主nib文件。通常该nib文件负责提供有关应用程序委托的信息。因此,如果您将其更改为SwitchViewsViewController,这是一个非常糟糕的主意。我建议你加入MainWindow.xib。我认为这更容易,但如果您有兴趣以编程方式进行操作而不是查看this post