开发者

how to let two ViewControllers use one NSObject

If you have one NSObject that you want two ViewControllers to be able to use, how do yo开发者_StackOverflow中文版u know which ViewController is calling it so when you pass what ever data you are computing back, you pass it back to the correct ViewController.


If you want the returned data to change based on the caller, create a method in the object you are retrieving data from rather than directly accessing a property.

In your NSObject, you could have a method that follows this format (my example is returning a string):

- (NSString *)getDataFor:(NSInteger)callingController {

    NSString *outputString = nil;

    if (callingController == 1) {
        // set value of output string for controller 1
    } else if (callingController == 2) {
        // set value of output string for controller 2
    }

    return outputString;

}

Then, from your view controller, you just call the method with the appropriate identifier as input.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜