gpt4 book ai didi

ios - 以编程方式在两个 UIViewController 之间切换

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:49:10 25 4
gpt4 key购买 nike

我有两个 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 的第二种方法,但我得到了unrecognized selector sent to instance错误。

非常感谢您提供的任何帮助。


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

这是第一个 View Controller .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

这是第二个 View Controller .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

整个项目作为单 View 应用程序制作,并针对 XCode 4.3 中的 iOS 5.1。我找到了与我遇到的问题相同的示例项目。这是该项目的 URL:http://www.devx.com/wireless/Article/42476/0/page/2它只是为早期的 iO​​S 版本编写的。有谁能看出我做错了什么,因为我真的没有想法?

PS:无论是否在代码中使用此动画,都会出现同样的问题,所以动画没有问题,我已经检查过了。


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

这是错误描述。在返回行的 main 方法中,如下所示:

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 方法内部! 看起来 ARC 在到达范围末尾时立即删除我的 UIViewController,这就是为什么我获得有关向已发布实例发送消息的信息。我在我的 ViewController 类中将 SecondViewController 设为私有(private)字段,之后一切正常。

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

最佳答案

编辑代码:

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

这应该有所帮助。

关于ios - 以编程方式在两个 UIViewController 之间切换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11969998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com