简体   繁体   English

UIAlertView按钮事件问题

[英]UIAlertView Button Event Problem

alright so i have a small problem here, i have this here. 好吧所以我这里有一个小问题,我在这里。

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        //YES clicked ...do your action
        [self.parentViewController dismissModalViewControllerAnimated:YES];
    }
    else if (buttonIndex == 1)
    {
        //NO clicked
        return;
    }
}

This Allows me to capture events triggered from UIAlertView Buttons but i have assigned it a value and on the same page i need another value to be assigned to that class so: 这允许我捕获从UIAlertView按钮触发的事件,但我已经为它分配了一个值,并且在同一页面上我需要为该类分配另一个值,因此:

if(buttonIndex == 2){//Proceed}

i mainly want it so that when the button is pushed on my second alert it will go back to the processes it was doing and not proceed with the event of (buttonIndex == 0). 我主要想要这样,当按下按钮时,我的第二个警报将返回到它正在进行的过程,而不是继续事件(buttonIndex == 0)。

So Does anyone know Where i could start? 那么有谁知道我可以从哪里开始?

Just store a reference in you .h file of the 2 UIAlertView 's, and then do a check. 只需在2 UIAlertView.h文件中存储引用,然后进行检查。 For instance, in your .h file: 例如,在.h文件中:

UIAlertView * alertView1; UIAlertView * alertView1; UIAlertView * alertView2; UIAlertView * alertView2;

In your .m file, set up your UIAlertView 's in you viewDidLoad method, and change the alertView:clickedButtonAtIndex: method to: .m文件中,在viewDidLoad方法中设置UIAlertView ,并将alertView:clickedButtonAtIndex:方法更改为:

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; {
  if (alertView == alertView1) {
    // Do what you want the alertView1 to do here.
    if (buttonIndex == 0) {
    //YES clicked ...do your action
     [self.parentViewController dismissModalViewControllerAnimated:YES];
    }
    else if (buttonIndex == 1) {

    }// etc.
  }
  else if (alertView == alertView2) {
    // Do what you want the alertView2 to do here.
  }
}

Hope that Helps! 希望有帮助!

Something else you can do is use the alertView tag in case you have multiple alertviews in your app. 您可以执行的其他操作是使用alertView标记,以防您的应用中有多个alertview。 For instance, you could do something like this: 例如,你可以这样做:

    UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"Title" message:@"AThe message."  delegate:self cancelButtonTitle:@"button 1" otherButtonTitles: @"button 2", nil];
    alert1.tag = 0;
    [alert1 show];
    [alert1 release];

Then in your delegate method, simply put the following if clause: 然后在您的委托方法中,只需输入以下if子句:

if (alertView == alertView1) 

before your code above. 在你的代码之前。

如果你没有想到这一点,你可以在程序中放置一个计数器来计算触发警报视图的次数,然后根据该值进行操作。

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

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