【问题标题】:iOS Xcode 4 UINavigationControlleriOS Xcode 4 UINavigationController
【发布时间】:2012-02-09 07:02:26
【问题描述】:

我的程序在 iOS 4/Xcode 3 中运行良好。我最近升级到了最新版本的 Xcode 4/iOS 5。我在以下行中获得了“SIGABRT”:

UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

这一行是在应用程序中确实完成了在委托中启动。下面是一些示例代码:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    rootViewController = [[MyCustomViewController alloc] initWithStyle:UITableViewStylePlain];
    rootViewController.window = window;
    window.rootViewController = rootViewController;

    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];

    [window addSubview:[navigationController view]];

    [window makeKeyAndVisible];
}

感谢任何帮助。

【问题讨论】:

  • 如果没有更多代码就无法诊断。初始化 rootViewController 时可能出现问题。

标签: ios xcode uinavigationcontroller uiapplicationdelegate


【解决方案1】:

你的applicationDidFinishLaunching方法怎么用很奇怪。

如果您想为您的window 添加一个UINavigationController 作为rootViewController,然后使用MyCustomViewController 的实例初始化该导航控制器,请执行以下操作:

- (void)applicationDidFinishLaunching:(UIApplication *)application {

    // code for creating a window
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

    MyCustomViewController* myCustomViewController = [[[MyCustomViewController alloc] initWithStyle:UITableViewStylePlain] autorelease];

    UINavigationController *navigationController = [[[UINavigationController alloc] initWithRootViewController:myCustomViewController] autorelease];

    self.window.rootViewController = navigationController;

    [self.window makeKeyAndVisible];
}

您的应用程序委托中的window .h 就像

@property (nonatomic, strong) UIWindow* window; // using ARC

@property (nonatomic, retain) UIWindow* window; // using not ARC

该属性也合成在你的应用程序委托.mlike

@synthesize window; 

一些注意事项:

当您使用window.rootViewController 时,您无需致电[window addSubView:someview]。 iOS 4 已经为您处理了。

您确定您的代码可以在较旧的 sdks 中运行吗?

希望对你有帮助。

【讨论】:

  • 你为什么在void方法中做return YES
  • @Eimantas 我已经修好了,谢谢。我犯了一个错误,因为我以为它是写的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions。干杯。
【解决方案2】:

初始化window的“正常”方式是这样的:

window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.rootViewController = [Myclass alloc] init...

你正在做相反的事情

rootViewController.window = window;

然后

window.rootViewController = rootViewController; ???

这真的适用于旧的 xcode 吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多