How to pass prepareForSegue: an object

后端 未结 10 2207
北荒
北荒 2020-11-21 11:18

I have many annotations in a mapview (with rightCalloutAccessory buttons). The button will perform a segue from this mapview to a tableview

10条回答
  •  没有蜡笔的小新
    2020-11-21 11:47

    Sometimes it is helpful to avoid creating a compile-time dependency between two view controllers. Here's how you can do it without caring about the type of the destination view controller:

    - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
    {
        if ([segue.destinationViewController respondsToSelector:@selector(setMyData:)]) {
            [segue.destinationViewController performSelector:@selector(setMyData:) 
                                                  withObject:myData];
        } 
    }
    

    So as long as your destination view controller declares a public property, e.g.:

    @property (nonatomic, strong) MyData *myData;
    

    you can set this property in the previous view controller as I described above.

提交回复
热议问题