我有两个 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。我找到了示例项目,它与我遇到的问题相同。这是该项目的 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]));
}
}
我收到此错误:
线程 1:EXC_BAD_ACCESS(代码=1,地址=0xc00000008)
[编辑#3:解决方案]
我要感谢@btype 下面的解决方案。我发现了为什么编辑#2 中的代码不起作用。问题是这样做:
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
IBAction 方法的内部!看起来ARC在到达范围结束时就擦除了我的UIViewController,这就是为什么我得到有关向已发布实例发送消息的信息的原因。我在 ViewController 类中创建了 SecondViewController 私有字段,之后一切正常。
如果有人认为我的解释是错误的并且知道真正困扰我的事情,请随时回答这个问题并纠正我。