开发者

how to let two ViewControllers use one NSObject

开发者 https://www.devze.com 2023-03-18 08:13 出处:网络
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 compu

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

精彩评论

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