【问题标题】:application crashed when I pushing new view controller to self.navigation controller当我将新视图控制器推送到 self.navigation 控制器时应用程序崩溃了
【发布时间】:2012-01-30 15:08:41
【问题描述】:

我有一个 iOS 5 应用程序。 当我想将视图推送到导航控制器时,应用程序崩溃:(

这是我的 appDelegate 部分:

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


    self.navigationController=[[UINavigationController alloc] init];

    StartViewController *startViewController=[[StartViewController alloc] init];
    [self.navigationController pushViewController:startViewController animated:YES];
    [self.navigationController setNavigationBarHidden:YES];
    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    self.window.backgroundColor = [UIColor whiteColor];
    self.window.rootViewController=self.navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

这是我推新视图控制器的方法:

-(IBAction)startPressed:(id)sender
{
    NSLog(@"startPressed: called");
    //loading RootViewController (mainscreen view)
    RootViewController *rootViewController=[[RootViewController alloc] init];
    [self.navigationController pushViewController:rootViewController animated:YES];
}

应用程序在字符串 [self.navigationController pushViewController:rootViewController animated:YES]; 处崩溃

请帮帮我。如何解决这个问题?

【问题讨论】:

  • 你的有问题,看我的回答。但请提供崩溃日志。
  • 我认为处理自动引用计数模式有问题。我想我错过了什么。这是我第一个启用了自动引用计数模式的 ios 5 应用程序
  • 应用程序非常简单。我的 startViewController 中只有一个按钮启动。当用户按下它时,我正在尝试加载 RootViewController。但是应用程序崩溃了:(

标签: iphone objective-c ios uinavigationcontroller pushviewcontroller


【解决方案1】:

一个 UINavigationController 想要一个 rootviewController:

StartViewController *startViewController=[[StartViewController alloc] init];
self.navigationController=[[UINavigationController alloc] initWithRootViewControllr:startViewController];

[self.navigationController setNavigationBarHidden:YES];
[[UIApplication sharedApplication] setStatusBarHidden:YES];

罢工>

改变

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

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 
[pool release];

return retVal;

【讨论】:

  • 崩溃日志:@autoreleasepool { return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); } SIGABRT 这里
  • 在调试中,当崩溃后按下停止按钮时:error while killing target (killing anyway): warning: error on line 2184 of "/SourceCache/gdb/gdb-1708/src/gdb/macosx/macosx-nat-inferior.c" in function "void macosx_kill_inferior_safe()": (os/kern) failure (0x5x)
  • 也尝试使用NSZombieEnabled 运行应用程序。
  • NSAutorelease pool 在自动引用计数模式下不可用
【解决方案2】:

解决了这个问题。我更改了 `[self.navigationController pushViewController:vc animated:NO] 并且调试给了我未设置视图出口的警告。所以我从界面生成器连接了视图,问题就消失了。谢谢大家的帮助

【讨论】:

    【解决方案3】:

    处理 UINavigationController 的方式很奇怪。在你的情况下,我想知道如果你改变它是否有效

    self.navigationController=[[UINavigationController alloc] init];
    
    StartViewController *startViewController=[[StartViewController alloc] init];
    [self.navigationController pushViewController:startViewController animated:YES];
    

    StartViewController *startViewController=[[StartViewController alloc] init];
    self.navigationController=[[UINavigationController alloc] initWithRootViewController:startViewController];
    

    如果您的应用仍然崩溃,如果您不使用 – initWithNibName:bundle: 初始化它,是否覆盖 StartViewController 类的 -loadView 方法会很有趣

    【讨论】:

    • 请说明-loadView方法。
    • 也许你的意思是viewDidLoad 方法?我可以使用 `- initWithNibName:bundle: 对其进行初始化,但应用程序会崩溃。
    • StartViewController 工作正常。但是当我试图将我的新视图控制器推送到 self.navigationController 应用程序崩溃时:(
    • 所以去掉等式的RootViewController。直接加载 UIViewController 并检查您的应用程序是否仍然再次崩溃
    • 尝试推送一个基本的 UIViewController 而不是你自己的 RootViewController
    【解决方案4】:

    我对 pushViewController 有同样的问题,但是当我尝试这样的事情时

    UINavigationController *rootViewController = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    NSLog(@"NavControler:%@, %@", self.navigationController, rootViewController);
    

    我得到相同的指针,并且 NSArray 有 1 个项目。 问题出在其他地方。

    如果有人可以提供帮助并添加一些适用于 pushViewController 的代码,因为我有正确的 NavigationController,但是当我尝试推送 ViewControler 时出现错误

      ViewController* NextView = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *rootViewController = (UINavigationController *)[[[UIApplication sharedApplication] keyWindow] rootViewController];
    [rootViewController pushViewController:NextView animated:YES];
    

    请有人为我解释一下这个问题。

    【讨论】:

      【解决方案5】:

      pushViewController 也有一些问题。我检查了所有目标成员资格,发现我的 xib 文件没有添加到必要的目标中。这对我有帮助。 我认为您还应该检查您的 xib 文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-01-21
        • 1970-01-01
        • 2010-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多