gpt4 book ai didi

ios - 如何发送方法以从 UIButton 调用的操作中查看

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:39:52 24 4
gpt4 key购买 nike

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

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

然后我有这个 Action :

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

问题 是如何从 showHour: 操作将 displayHour 调用到主视图?

如果你需要它,displayHour 方法看起来像这样(简化,基本上更新主视图上的一些标签和 ImageView ):

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

关于ios - 如何发送方法以从 UIButton 调用的操作中查看,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19203281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com