如何从UIButton addtarget验证到不同的视图控制器

时间:2017-03-23 03:48:27

标签: ios objective-c xcode

我将UIButton添加到其他UIViewController的视图中。我需要目标选择器从json值

指向该视图控制器
- (IBAction)timeline:(id)sender {

    if ([_NoTimeline isEqualToString:@"timeline not available :)"]) {
        NearmeNOViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"NONearmeViewController"];

        [self presentViewController:vc animated:YES completion:nil];
    } else {
        NearmeFrendViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"NearmeFrendViewController"];

        [self presentViewController:vc animated:YES completion:nil];
    }
}

2 个答案:

答案 0 :(得分:0)

如果时间轴不可用,您可以在此处将NSString "timeline not available :)"分配给变量self.NoTimeline。并将NSArray分配给变量self.NoTimeline。如果您可以正确编辑您的api添加状态密钥。如果不试试这个

- (IBAction)timeline:(id)sender{

if ([self.NoTimeline isKindOfClass:[NSString class]]) {
    NearmeNOViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"NONearmeViewController"];

    [self presentViewController:vc animated:YES completion:nil];
} 
else {
    NearmeFrendViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"NearmeFrendViewController"];

    [self presentViewController:vc animated:YES completion:nil];
   }
}

答案 1 :(得分:0)

这里的问题似乎是_NoTimeline并不总是NSString类型。因此isEqualToString:操作失败。

您可以使用以下方法检查NSString:

if ([_NoTimeline isKindOfClass:[NSString class]] {
   // Your code
}