简体   繁体   中英

how to validate from UIButton addtarget to a different view controller

I'm adding a UIButton to a view on a different UIViewController . I need the target selector to point to that view controller from json value

- (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];
    }
}

Here you are assigning NSString "timeline not available :)" to variable self.NoTimeline if timeline isnt available. And assigning an NSArray to variable self.NoTimeline . If you can edit your api add status key properly. If not try this

- (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];
   }
}

The problem here seems to be that _NoTimeline is not always of type NSString. Therefore the isEqualToString: operation fails.

You can check for NSString by using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM