2012-08-15 170 views
2

我有两个xib文件,每个文件都是自己的类,它覆盖了UIViewController类。我在第一个xib文件中有一个按钮,当它被按下时,它应该带我到新的UIViewController。我曾设法在不同的方式,过渡:以编程方式在两个UIViewControllers之间切换

UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:@"SecondViewControllerName" bundle:[NSBundle mainBundle]]; 
     [self.view addSubview:secondViewController.view]; 

UIViewController* secondViewController = [[UIViewController alloc] initWithNibName:@"SecondViewControllerName" bundle:[NSBundle mainBundle]]; 
     [self.navigationController pushViewController:secondViewController animated:YES]; 

在第二UIViewController的XIB文件我也有一个按钮,应该带我回到最初的UIViewController。但我不知道如何导航回第一个UIViewController。

我想:

[self.navigationController popViewControllerAnimated:YES]; 

导航到第二UIViewController中的第二种方式,但我得到无法识别的选择发送到实例错误。

非常感谢任何形式的帮助。


[编辑#1:添加源代码]

这里的第一视图控制器.m文件的源代码:

#import "ViewController.h" 
#import "SecondViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); 
} 

- (IBAction)switchToSecondPage:(id)sender 
{ 
    SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

    [UIView beginAnimations:@"flipping view" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut]; 
    [UIView setAnimationTransition: UIViewAnimationTransitionCurlDown forView:self.view cache:YES]; 

    [self.view addSubview:secondViewController.view]; 

    [UIView commitAnimations]; 

} 

@end 

这里的第二视图控制器.m文件的源代码:

#import "SecondViewController.h" 

@interface SecondViewController() 

@end 

@implementation SecondViewController 

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

- (IBAction)switchToFirstPage:(id)sender 
{ 
    [UIView beginAnimations:@"flipping view" context:nil]; 
    [UIView setAnimationDuration:1]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseIn]; 
    [UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView:self.view.superview cache:YES]; 

    [self.view removeFromSuperview]; 

    [UIView commitAnimations]; 
} 

@end 

这是我的AppDelegate.m文件:

#import "AppDelegate.h" 

#import "ViewController.h" 

@implementation AppDelegate 

@synthesize window = _window; 
@synthesize viewController = _viewController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
    // Override point for customization after application launch. 
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
} 

@end 

整个项目被制作为单一视图应用程序,并针对XCode 4.3中的iOS 5.1。我找到了一个样品项目,它可以完成我遇到的同样的事情。这是该项目的URL:http://www.devx.com/wireless/Article/42476/0/page/2 它只是为较早的iOS版本编写的。有人能够看到我做错了什么,因为我真的没有想法? PS:在代码中使用此动画或不使用 - 发生同样的问题,所以动画不是问题,我已经检查过。


[编辑#2:添加错误信息]

这里的错误描述。在上回线主要方法,列举如下:

int main(int argc, char *argv[]) 
{ 
    @autoreleasepool { 
     return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
    } 
} 

我得到这个错误:

Thread 1: EXC_BAD_ACCESS (code=1, address=0xc00000008)


[编辑#3:解]

我想感谢下面是他的解决方案@btype。我发现为什么编辑#2的代码不起作用。问题是这样做的:

SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 

IBAction METHOD里面!看起来像ARC在我的UIViewController一旦到达范围的末端就立刻擦掉了,这就是为什么我获得了有关将消息发送到已发布实例的信息。我在我的ViewController类中创建了SecondViewController专用字段,之后,一切正常。

如果有人认为我的解释是错误的,并且知道真正困扰我的是什么,请随时回答这个问题并纠正我。

回答

4

编辑代码:

在AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
// Override point for customization after application launch. 
ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 

UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
self.window.rootViewController = navController; 
[self.window makeKeyAndVisible]; 
return YES; 
} 

FirstViewController.m

- (IBAction)switchToSecondPage:(id)sender 
{ 
    [self.navigationController pushViewController:secondViewController animated:YES]; 
} 

SeconViewController.m

- (IBAction)switchToFirstPage:(id)sender 
{ 
    [self.navigationController popToRootViewControllerAnimated:YES]; 
} 

这应该帮助。

+0

我刚刚写到@jackiedigital。 IBAction没有被调用,但是我的第二个UIViewController中的viewDidLoad也没有被调用。我很肯定,我正确地将IBAction附加到了我在这个xib文件中唯一的UIButton。 – uerceg 2012-08-15 13:36:28

+0

检查您的文件所有者是否具有相同的类别?视图插座是否连接到您的视图?你的按钮连接到文件所有者?你是否将你的按钮的'touch up inside'事件连接到文件所有者IBAction方法?只是试图提供一些提示... – btype 2012-08-15 13:40:04

+0

我有FirstViewController和SecondViewController(.m和.h)文件。 FirstViewController设置为第一个xib文件的文件所有者,SecondViewController设置为第二个xib文件。我在第一个xib文件上有IBOutlet按钮,第二个按钮上也有一个IBOutlet。它们与xib文件上的按钮连接良好,但此时我没有使用该连接进行任何操作。我为第一个xib文件上的按钮创建了IBAction,该文件转换到第二个文件并且该方法正在被调用,并且转换到第二个UIViewController。我在SecondViewController类中做过的同样的事情。 – uerceg 2012-08-15 13:50:04

相关问题