• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

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

[复制链接]
菜鸟教程小白 发表于 2022-12-12 22:28:33 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我有两个 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)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)switchToSecondPageid)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)initWithNibNameNSString *)nibNameOrNil bundleNSBundle *)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)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)switchToFirstPageid)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)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)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)applicationWillResignActiveUIApplication *)application
{
}

- (void)applicationDidEnterBackgroundUIApplication *)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)字段,之后一切正常。

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



Best Answer-推荐答案


编辑代码:

在 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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap