我理解(我认为)为什么这不起作用,但我不知道如何让它起作用。
我有一个viewWillAppear
像这样调用的方法(为了这个问题,我简化了代码):
- (void)viewWillAppear:(BOOL)animated
{
[self displayHour:0];
}
然后我有一个小的 for 循环,它在以下位置构建了一些按钮viewDidLoad
:
NSUInteger b = 0;
for (UIButton *hourButton in hourButtons) {
[hourButton setTag:b];
[hourButton setTitle:[NSString stringWithFormat:@"%lu", (unsigned long)b] forState:UIControlStateNormal];
[hourButton addTarget:self action:@selector(showHour:) forControlEvents:UIControlEventTouchUpInside];
b++;
}
然后对于我有这个动作:
- (IBAction)showHour:(id)sender
{
// Logs 0, 1, etc. depending on which button is tapped
NSLog(@"Button tag is: %lu", (long int)[sender tag]);
// This currently throws this error:
// No visible @interface for 'UIView' declares the selector 'displayHour:'
// What I want to do is be able to call this method on
// the main view and pass along the button tag of the
// button that was tapped.
[mainView displayHour:(long int)[sender tag]];
}
问题是我如何displayHour
从动作中调用主视图showHour:
?
如果需要,displayHour 方法看起来像这样(简化了,基本上更新了主视图上的一些标签和图像视图):
- (void)displayHour:(NSUInteger)i
{
// The temperature
hourTemp = [[hourly objectAtIndex:i] valueForKeyPath:@"temperature"];
// The icon
hourIcon = [[hourly objectAtIndex:i] valueForKeyPath:@"icon"];
// The precipitation
hourPrecip = [[hourly objectAtIndex:i] valueForKeyPath:@"precipProbability"];
// SET DISPLAY
[hourTempField setText:hourTemp];
[hourIconFrame setImage:hourIcon];
[hourPrecipField setText:hourPrecip];
}
谢谢!