【问题标题】:prepareForSegue for TabBarController用于 TabBarController 的 prepareForSegue
【发布时间】:2015-12-25 10:39:16
【问题描述】:

我正在尝试使用 Objective-C 将数据从 ViewController 传递到 TabBarController。我正在尝试将一些数据分配给“bottomTabEventList”(这是我的自定义 TabBarController 类的属性)。不幸的是,我的程序因给出无法识别的选择器实例错误/警告而崩溃。

在 TabBarController 类的自定义 header 中,命名为 BottomTabview:

@interface BottomTabView : UITabBarController <UITabBarControllerDelegate>

@property(strong,nonatomic)EventList *bottomTabEventList;

@end

ViewController.m 中的 prepareForSegue 方法

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    BottomTabView *btw = [segue destinationViewController];

    //checking
    if(btw == nil)
        NSLog(@"btw in viewController is nil");
    else
        NSLog(@"btw in viewController is NOT nil");

    if(self.eventList.eventList == nil)
        NSLog(@"eventList in viewController is nil");
    else
        NSLog(@"eventList in viewController is NOT nil"); //end of checking

    btw.bottomTabEventList = self.eventList; //This is where crash appears
}

确切的崩溃日志是:

-[ViewController setBottomTabEventList:]:无法识别的选择器发送到实例 0x7fe923c6ba00 *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[ViewController setBottomTabEventList:]:无法识别的选择器发送到实例 0x7fe923c6ba00”

Segue 是从 ViewController 到 BottomTabView,它的类型是“Present Modally”。如果您能帮助/指导我,我将不胜感激。提前致谢。

【问题讨论】:

  • 尝试像这样投射:BottomTabView *btw = (BottomTabView *)[segue destinationViewController];
  • 这只是强制转换,有助于消除编译器警告。如果destinationViewController 不是一开始的,它就不会神奇地使一个BottomTabView 类。

标签: ios objective-c xcode uiviewcontroller uitabbarcontroller


【解决方案1】:

问题似乎是 btw 实际上不是 BottomTabView 类型,而只是 UIViewController 因此崩溃日志给出:

[ViewController setBottomTabEventList:]: 无法识别的选择器发送到实例

因为UIViewController 不知道BottomTabEventList 是什么。

您需要确保btw 实际上是BottomTabView 实例。

做一点内省,我敢打赌它不会进入这个陈述:

  if ([btw isKindOfClass:[BottomTabView class]]){
 btw.bottomTabEventList = self.eventList; 
}

【讨论】:

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