简体   繁体   English

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

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

I understand (I think) why this isn't working, but I don't know how to get it to work. 我了解(我认为)为什么这不起作用,但是我不知道如何使它起作用。

I have a method I call in viewWillAppear like so (I've simplified the code for the sake of this question): 我有一个在viewWillAppear调用的方法,如下所示(出于这个问题,我简化了代码):

- (void)viewWillAppear:(BOOL)animated
{
        [self displayHour:0];
}

I then have a little for loop that builds some buttons in viewDidLoad : 然后,我有一个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++;
}

Then for the action I have this: 然后对于这个动作我有:

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

THE QUESTION is how do I call displayHour to the main view from the showHour: action? 现在的问题是我怎么叫displayHour从主视图showHour:动作?

If you need it, the displayHour method looks like this (simplified, basically updates some labels and image views on the main view): 如果需要,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];
}

Thanks! 谢谢!

Instead of 代替

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

do: 做:

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

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

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