Несколько UIAlertView; каждый со своими кнопками и действиями - программирование
Подтвердить что ты не робот

Несколько UIAlertView; каждый со своими кнопками и действиями

Я создаю представление в Xcode 4.3 и не знаю, как указать несколько UIAlertView, которые имеют свои собственные кнопки с отдельными действиями. В настоящее время у моих оповещений есть свои кнопки, но те же действия. Ниже мой код.

-(IBAction)altdev {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
[alert show];
}

-(IBAction)donate {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]];
         }
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]];
    }
}  

Спасибо за любую помощь!

4b9b3361

Ответ 1

Существует полезное свойство tag для UIView (из подкласса UIAlertView). Вы можете установить разные теги для каждого вида предупреждения.

UPDATE:

#define TAG_DEV 1
#define TAG_DONATE 2

- (IBAction)altdev {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
   alert.tag = TAG_DEV;
   [alert show];
}

- (IBAction)donate {
    UIAlertView *alert = [[UIAlertView alloc] 

                          initWithTitle:@"titleGoesHere"
                          message:@"messageGoesHere"
                          delegate:self
                          cancelButtonTitle:@"Cancel"
                          otherButtonTitles:@"Continue", nil];
    alert.tag = TAG_DONATE;
    [alert show];
}

-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == TAG_DEV) { // handle the altdev
      ...
    } else if (alertView.tag == TAG_DONATE){ // handle the donate

    }
}

Ответ 2

проще и новее

UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 1;

UIAlertView *alert = [[UIAlertView alloc] init...
alert.tag = 2;



- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(alertView.tag == 1) {
        // first alert...
    } else  {
        // sec alert...
    }
}

все сделано!

Ответ 3

Если вам посчастливилось использовать методы делегата для разного определения вида предупреждения, то вы также можете использовать класс Category для использования блока завершения для каждого AlertView.

Alert_ActionSheetWithBlocks

Например,

UIAlertView* alert1 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 1" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];

[alert1 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView1 Stuff here }];

UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"AlertView+Block 2" message:@"WithBlocks" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil];
[alert2 showWithFinishBlock:^(UIAlertView *alertView, NSInteger buttonIndex){ //--AlertView2 Stuff here }];

Надеюсь, это самый простой способ сравнить с методом tag + delegate.

Ответ 4

Он прав, но вам нужно добавить это:

-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (alertView.tag == TAG_DEV && buttonIndex == 1) { // handle the altdev
      ...
    } else if (alertView.tag == TAG_DONATE && buttonIndex == 1){ // handle the donate

    }
}

if buttonIndex == 1, то вы используете FIRST otherbutton. 0 будет отменено. Но ничего не делать при 0

Ответ 5

Или вы могли бы это сделать (проверьте название названия), это всего лишь еще один вариант... Помните, что хотя бы опознаваемые оповещения, хотя!

-(IBAction)altdev {
UIAlertView *alert = [[UIAlertView alloc] 

                      initWithTitle:@"titleOneGoesHere"
                      message:@"messageGoesHere"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"Continue", nil];
[alert show];
}

-(IBAction)donate {
UIAlertView *alert = [[UIAlertView alloc] 

                      initWithTitle:@"titleTwoGoesHere"
                      message:@"messageGoesHere"
                      delegate:self
                      cancelButtonTitle:@"Cancel"
                      otherButtonTitles:@"Continue", nil];
[alert show];
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {

if (buttonIndex == 1)
{
    if([[alertView title] isEqualToString:@"titleOneGoesHere"])
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.examplesite1.com"]];
    }
    else
    {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"examplesite2.com"]];
    }
}