【问题标题】:MainViewController pushViewController:animated:]: unrecognized selector sent to instance 0x1459e980MainViewController pushViewController:animated:]:无法识别的选择器发送到实例 0x1459e980
【发布时间】:2015-07-16 07:29:02
【问题描述】:

我正在尝试添加功能以在我的 iOS 应用上打开通知时打开特定的视图控制器。由于我是 iOS 新手,所以我有一些疑问。

我已经编写了后端,它将促销 id 作为自定义参数发送。我能够在 XCode 上接收 JSON 对象,将 ID 读取为 NSString。

现在,我想以促销 id 作为参数打开 PromotionDetailViewController。

到目前为止,我所做的是,

    PromotionDetailViewController *pVC = [[PromotionDetailViewController alloc] initWithPromotionID:promo_id[@"promo_id"]];
    [(UINavigationController *)self.window.rootViewController pushViewController:pVC animated:YES];

    [(UINavigationController *)[UIApplication sharedApplication].keyWindow.rootViewController popViewControllerAnimated:YES ];

我能够接收和读取 AppDelegate.m 文件中的促销 ID。 当我运行上面的代码时,我得到了下面的错误。

MainViewController pushViewController:animated:]: 无法识别 选择器发送到实例 0x1459e980

对正确方向的任何帮助都很棒!我做php和android,但是Obj-C大不一样!

更新 将代码更改为:

    PromotionDetailViewController *pvc = [[PromotionDetailViewController alloc] initWithPromotionID:promo_id[@"promo_id"]];
    UINavigationController *promoNav =
    [[UINavigationController alloc] initWithRootViewController:pvc];
    self.window.rootViewController = promoNav;

现在 API 调用成功,我可以看到正确的 NAV 栏,但是由于以下错误,应用程序崩溃了:

由于未捕获的异常“NSRangeException”而终止应用程序,原因: '*** -[__NSArrayI objectAtIndex:]: 索引 0 超出范围为空 数组'

【问题讨论】:

    标签: ios objective-c nsstring apple-push-notifications


    【解决方案1】:

    (UINavigationController *)self.window.rootViewController 好像不是 UINavigationController。

    如果 'self' 已经是导航控制器的子控制器,您可以使用 self.navigationController 获得正确的 NavigationController。

    在 obj-c 中无法识别的选择器意味着您正在向不响应的对象发送消息,因为您使用括号手动转换它,编译器不会抱怨并认为实际上是 NavgiationController,但不是不。

    【讨论】:

    • 谢谢,我已经更新了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多