0

我正在尝试在名为“ClockView”的 UIView 类中启动一个 NSTimer,并使用一个方法作为选择器来操作在 ViewController“ClockViewController”中声明的初始浮点数。

我的 ClockViewController 声明int timerIntWhite为整数(例如 500)。我的 ClockView 需要这个值用于每秒- (void)start运行一个方法的方法- (void)updateWhiteClock

- (void)start {

    timerIntWhite = PLEASE HELP ME AT THIS POINT!;
    randomTimerWhite = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0)target:self selector:@selector(updateWhiteClock) userInfo:nil repeats:YES];
}

是否可以在 ClockView 中访问 ClockViewController 的整数?

4

1 回答 1

0

Try the following:

In your ClockView also declare the variable (property) int timerIntWhite and set this variable from your View Controller after the View gets created, for example, in viewDidLoad.

-(void)viewDidLoad {

    [super viewDidLoad];

    self.view.timerIntWhite = self.timerIntWhite;
}

After doing this, ClockView can access it's own timerIntWhite variable:

- (void)start {

    timerIntWhite = self.timerIntWhite;
    randomTimerWhite = [NSTimer scheduledTimerWithTimeInterval:(1.0/1.0)target:self selector:@selector(updateWhiteClock) userInfo:nil repeats:YES];
}

I'm assuming that your ClockViewController class knows that its view IS-A ClockView. This is very important! Otherwise you'll get a warning.

I also want to mention that according to the MVC rules it's a better idea if your ClockViewController class takes care of the NSTimer. Views should be used to display information to the user only.

Hope this helps!

于 2013-11-13T00:31:52.957 回答