【问题标题】:During prepareForSegue get [UIViewController <null selector>] when try to assign value to property?在prepareForSegue期间尝试为属性赋值时得到[UIViewController <null selector>]?
【发布时间】:2012-05-09 22:18:02
【问题描述】:

在 prepareForSegue 期间,我尝试为属性赋值并得到错误

2012-05-09 23:14:22.122 LeakSpotter[17709:15b03] * 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIViewController setCoordLong:]:发送了无法识别的选择器到实例 0xa86e930'

标题:

@property (nonatomic, retain) NSString *CoordLong;
@property (nonatomic, retain) NSString *CoordLat;

它们是合成的。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{

NSLog(@"Fired segue %@", [segue identifier]);
if([[segue identifier] isEqualToString:@"returnToLeakSpotter"]){
    NSLog(@"Return to leak spotter segue...");
    //ContactsViewController *cvc = (ContactsViewController *)[segue destinationViewController];

}else if ([[segue identifier] isEqualToString:@"gotoTakePhoto"]) {
    NSLog(@"Go to photo view");
    takePhotoViewController *takePhotoC = [segue destinationViewController];

    takePhotoC.CoordLong = @"-9.88";
    takePhotoC.CoordLat = @"45.45";

我是否遗漏了一些明显的东西?

谢谢

【问题讨论】:

    标签: objective-c ios exception segue


    【解决方案1】:

    看起来您没有在情节提要中的 takePhotoViewController 上设置视图控制器子类,因此它正在实例化一个不知道 CoordLat 是什么的基本 UIViewController(如错误所示)。

    顺便说一句,按照惯例,类名以大写字母开头,变量/属性以小写字母开头,您似乎反过来。

    【讨论】:

    • 我意识到我正在尝试向上转换到 takePhotoViewController,这就是为什么这些附加属性会导致运行时异常。我被引导理解这是通过 segue 与子视图“交流”的方式。但现在我想知道如何实际做到这一点。
    • 你的代码没问题。我认为问题出在你的故事板中。找到 te segue 链接到的视图控制器。选择它,然后在身份检查器中,确保将类设置为正确的子类。
    【解决方案2】:

    您可能需要转换您的 viewController,因为destinationViewController 默认返回 UIViewController。

    takePhotoViewController *takePhotoC = (takePhotoViewController *)[segue destinationViewController];
    

    【讨论】:

    • 这将有助于编译警告,但问题是关于运行时错误。 destinationViewController 返回目的地的任何类 - 这将是 UIViewController 或子类。
    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    相关资源
    最近更新 更多