2011-06-23 50 views
0

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

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]; 
} 

在Interface Builder中,所有我所做的就是链接的文件的所有者类和按钮。首先加载SwitchViewsViewController,而不是MainWindow。一切都在建立,但是当我尝试运行应用程序时,它会崩溃并将其发送到主屏幕。谁能帮我这个?

回答

0

当你分配你的secondViewController时你初始化了nib名字为nil,这实际上应该是你的nib文件的名字。

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

此外,应关闭括号;-)

+0

我试过了,仍然崩溃:( – Ouija

+0

等一下,当你按下按钮或者它启动时应用程序崩溃了吗? –

+0

崩溃在按钮上,但我修复了它,查看没有链接到文件老板 – Ouija

0

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

+0

我修好了,查看不到第二个viewcontroller没有链接到文件的所有者。感谢您的帮助! – Ouija