iOS初学者:带3个按钮的UIAlertView窗口>检查按下了什么按钮 [英] iOS Beginner: UIAlertView Window with 3 Buttons > Check what button was pressed

查看:108
本文介绍了iOS初学者:带3个按钮的UIAlertView窗口>检查按下了什么按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个教程中的工作代码但完全不理解。

I have a working code from a tutorial but don't understand it completely.

情况:

在我的iPhone App
中按下按钮后,会出现一个带有三个按钮的AlertView。
现在我想查看用户按下了什么按钮。

After a button was pressed in my iPhone App an AlertView appears with three buttons. Now I like to check what button the user pressed.

来自指南的代码:

- (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但如何可能
方法:

Code works, I see the correct output in the console as a NSLog but how is it possible that the method:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"Button: %i, was pressed.", buttonIndex);
}

指的是正确的警报视图。在这种情况下:myAlert1。

refers to the correct alert view. In this case: myAlert1.

如何使用多个警报视图。
例如第二个调用myAlert2。

What about with more than one alert view. For example a second one calling myAlert2.

我知道下面的代码不正确但是如果我'我会更有意义
d编写如下方法:

I know the following code is not correct but it would make more sense to me if I'd write the method as follow:

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    NSLog(@"Button: %i, was pressed.", buttonIndex_FROM_myAlert1);
}

希望你能提供帮助,让我疯狂。

Hope you can help, drives me nuts.

问候,
Marc

Regards, Marc

推荐答案


该方法如何引用正确的警报视图?

how is it possible that the method refers to the correct alert view?

正是出于这个原因,委托方法 alertView:didDismissWithButtonIndex:实际上告诉你它引用了哪个警报视图。请注意,该方法有两个参数。第二个告诉你按钮索引,第一个指向此按钮索引引用的警报视图。

For exactly that reason, the delegate method alertView:didDismissWithButtonIndex: actually tells you which alert view it refers to. Note that the method has two arguments. The second one tells you the button index and the first one points to the alert view this button index refers to.

如果你有多个共享相同的警报视图委托,你将不得不检查第一个参数哪个警报视图。为了能够这样做,您必须将警报视图存储在ivar / property或其他数据结构中,以便在委托方法中记住它们。 (或者,因为 UIAlertView UIView 的子类,所以你可以使用标签用于区分多个视图的属性)。

If you have more than one alert view that share the same delegate, you will have to check against the first argument which alert view this is about. To be able to do that, you would have to store the alert views in an ivar/property or other data structure in order to remember them in the delegate method. (Or, since UIAlertView is a subclass of UIView, you could use the tag property to distinguish between multiple views).

这篇关于iOS初学者:带3个按钮的UIAlertView窗口>检查按下了什么按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆