【问题标题】:self.navigationController == nil after awaking from LocalNotification从 LocalNotification 唤醒后 self.navigationController == nil
【发布时间】:2013-01-01 04:44:22
【问题描述】:

我正在使用 Xcode 4.3.2

我实现了Local Notifications,它在预定时间提醒用户有新事件。因此,当我的应用处于后台并且时钟敲响时(例如)上午 8 点,用户将收到来自我的应用的通知。

当用户决定从后台查看应用程序时,我会加载一个 nib。目前,这个笔尖工作正常:它显示了它在笔尖中排列的视图。但是,在向用户显示笔尖后,我想将用户转发到 LocalNotificationsHandler.m 中的不同视图。当我尝试推送第二个视图时,我的应用程序失败。因此,虽然没有错误消息,但似乎第二个笔尖不会加载。

简而言之,流程如下:

  • 当我的应用在后台运行时,用户会收到通知
  • 用户选择查看应用程序
  • LocalNotificationsHandler nib 将加载
  • self.navigationController == nil(在 LocalNotificationsHandler.m 中)
  • self.navigationController 不会“[pushViewController: "new view" animated:YES]" 获取新视图

我想知道我的 AppDelegate.m 文件中是否缺少某些内容,所以我已经包含了 我的 AppDelegate.m 文件中的“didFinishLaunchingWithOptions”:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // Override point for customization after application launch.
    // Add the navigation controller's view to the window and display.


    NSLog(@"did finish launching with options");
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];

    [[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound)];

  if (self.locationManager == nil)
  {
    locationManager = [[CLLocationManager alloc] init];
    locationManager.purpose = @"We will try to use you location";
  }

  if([CLLocationManager locationServicesEnabled])
  {
    [self.locationManager startUpdatingLocation];
  }

  self.navigationController.navigationBar.tintColor = nil;

  return YES;
}

【问题讨论】:

  • 如果您正常加载应用程序(即,不是来自本地通知),UINavigationController 是否非零?根据我的经验,加载过程应该是相同的,但对于 application:didFinishLaunchingWithOptions: 中的选项。我的直觉是 UINavigationController 可能根本没有被实例化,即使是正常的?
  • 不,通常不是零。我应该如何检查 UINavigationController 是否正在实例化?
  • 我使用故事板而不是笔尖,所以我不是 100% 确定。我只是认为可能发生的事情是您正在从 nib 加载,它加载单个 VC,而不是后台 UINavigationController。如果丢失,您是否尝试过创建它? (即 if (!self.navigationController) { self.navigationController = [[UINavigationController alloc] initWithRootViewController:yourVCInstance] })
  • 我们能看到创建本地通知处理程序的代码吗?这看起来比 didFinishLaunching 方法更有可能是罪魁祸首。本质上,问题出在导航堆栈上不存在本地通知处理程序。因此,要解决此问题,您首先需要弄清楚将其推入堆栈的意义(它是程序化的吗?它应该由情节提要完成吗?可能是 XIB 吗?等等)。

标签: objective-c cocoa-touch uinavigationcontroller uilocalnotification


【解决方案1】:

您正在使用过时的(自 iOS 3 起)将 viewcontroller 的视图添加到主 UIWindow 的方法。应该是这样的:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    // create properly sized window
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // create instance of root VC and assign to window
    MainViewController *vc = [[MainViewController alloc] init];
    self.window.rootViewController = vc;
    [vc release];

    [self.window makeKeyAndVisible];

    return YES;
}

视图控制器的 navigationController 属性只有在它实际上是从 UINavigationController 呈现时才设置。

查看这篇文章了解更多信息:http://www.cocoanetics.com/2012/11/revisited/

【讨论】:

  • 好文章。不确定它是否能解决问题,但值得一读。
猜你喜欢
  • 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
相关资源
最近更新 更多