如何让两个ViewControllers使用一个NSObject

时间:2011-07-07 23:23:07

标签: iphone ios viewcontroller nsobject

如果你有一个NSObject,你想要两个ViewControllers能够使用,你怎么知道哪个ViewController正在调用它,所以当你传递你正在计算的数据时,你将它传递回正确的ViewController。 / p>

1 个答案:

答案 0 :(得分:1)

如果希望根据调用者更改返回的数据,请在要从中检索数据的对象中创建方法,而不是直接访问属性。

在你的NSObject中,你可以有一个遵循这种格式的方法(我的例子是返回一个字符串):

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

}

然后,从视图控制器中,您只需使用适当的标识符作为输入调用该方法。