以编程方式在两个UIViewControllers之间切换

时间:2012-08-15 13:17:52

标签: ios uiviewcontroller

我有两个xib文件,每个文件都有自己的类,它会覆盖UIViewController类。我在第一个xib文件中有一个按钮,当按下它时,它应该带我到新的UIViewController。我已经设法以不同的方式进行转变:

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。我找到了一个样本项目,它做了同样的事我遇到了麻烦。这是该项目的网址: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]));
    }
}

我收到此错误:

  

主题1:EXC_BAD_ACCESS(代码= 1,地址= 0xc00000008)


[编辑#3:解决方案]

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

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

内部的IBAction方法!看起来ARC一旦到达范围结束就擦拭了我的UIViewController,这就是我收到有关向已发布实例发送消息的信息的原因。我在我的ViewController类中创建了SecondViewController私有字段,之后一切正常。

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

1 个答案:

答案 0 :(得分: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];
}

这应该有所帮助。