繁体   English   中英

如何发送方法以从UIButton调用的操作进行查看

[英]How to send method to view from action called by UIButton

我了解(我认为)为什么这不起作用,但是我不知道如何使它起作用。

我有一个在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];
}

谢谢!

代替

   [mainView displayHour:(long int)[sender tag]];

做:

   [self displayHour:(long int)[sender tag]];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM