C.Johns
C.Johns

Reputation: 10245

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 you 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.

Upvotes: 0

Views: 205

Answers (1)

Katfish
Katfish

Reputation: 698

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.

Upvotes: 1

Related Questions