【发布时间】:2023-03-07 13:30:01
【问题描述】:
我有一个教程中的工作代码,但不完全理解。
情况:
在我的 iPhone 应用程序中按下按钮后 出现一个带有三个按钮的 AlertView。 现在我想检查用户按下了什么按钮。
教程中的代码:
- (IBAction)infoButtonPressed:(id)sender {
UIAlertView *myAlert1 = [[UIAlertView alloc]initWithTitle:@"My Alert View 1"
message:@"Here we go"
delegate:self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"Option1", @"Option2", nil];
[alert show];
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Button: %i, was pressed.", buttonIndex);
}
代码有效,我在控制台中看到正确的输出为 NSLog,但怎么可能 那个方法:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Button: %i, was pressed.", buttonIndex);
}
指的是正确的警报视图。在这种情况下:myAlert1。
如果有多个警报视图呢? 例如第二个调用 myAlert2。
我知道下面的代码不正确,但它对我来说更有意义 如果我将方法编写如下:
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSLog(@"Button: %i, was pressed.", buttonIndex_FROM_myAlert1);
}
希望你能帮上忙,把我逼疯了。
问候, 马克
【问题讨论】:
标签: objective-c methods indexing uialertview