Controlling Multiple alertview actions

Discussion in 'iOS Development' started by johnmabassa, Jun 29, 2009.

  1. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    Hi,
    I have two alert views in my view controller file. I just wanted to handle the button presses according to the alertview which is getting popped up.

    Let my alertViews be like this
    Code:
    		UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Warning" 
    														message: @"Do it"
    													   delegate: self
    								 			  cancelButtonTitle: @"Proceed"
    											  otherButtonTitles: nil];
    		
    		[alert show];
    		[alert release];
    and another
    Code:
    		UIAlertView* alert2 = [[UIAlertView alloc] initWithTitle:@"Go" 
    														message: @"Continue"
    													   delegate: self
    											  cancelButtonTitle: @"Go"
    											  otherButtonTitles: nil];
    		
    		[alert2 show];
    		[alert2 release];
    Now in my clickedButtonAtIndex method I amdoing like this
    Code:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
            if ( [[alertView title] isEqualToString:@"Warning"])
             {
                 //Do Something.
            }
            else
           {
         //Do something.
           }
    }
    but the problem here is that each time if I need to change the title, I need to change here also. I tried using tag, but when the control comes to clickedButtonAtIndex, I am not getting any tag value.

    Code:
    if(alertView.tag==1)
    {
        //Do something.
    }
    it never enters this condition even after I set my alert view's tag to 1.

    Any ideas?

    -John
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    If tagging isn't working for you, and you still want to go with titles, then just define an nsstring macro. Something like #define WARNING @"Warning, b*tches!" Then when you set the title, instead of entering the string, enter WARNING. Likewise, check the title against warning in the method you use to get the correct action. if ([alert.title isEqualToString:WARNING]) [self dontFail:YES]; That way, you only have to change the text in one spot, the original macro.

    Of course, using titles, like you want to do, completely destroys any chances you have of localizing your app. So I wouldn't do it.
  3. Spreche

    Spreche Member

    Joined:
    Dec 27, 2007
    Messages:
    164
    Likes Received:
    0
    Device:
    iPhone
    I'm not quite sure if this is proper, but I would do something like this.

    First I would create my instance variables in my header file.
    Code:
    UIAlertView *alertOne;
    UIAlertView *alertTwo;
    
    Then I would create my alerts just like you would. The only thing I would do different would be to check what alertView is being used first, then check the buttons.
    Code:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
            if(alertView == alertOne) {
                    //Check for button that was pressed in alertOne then execute action
             }
             if(alertView == alertTwo) {
                    //Check for button that was pressed in alertTwo then execute action
             }
    }
    
    I'm pretty sure this would eliminate having to change the string in both places.
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    The problem with your solution, Spreche, is that both the alert views need to be Ivars, and you must always hang on to them until the object they belong to is released. That's a bad solution.
  5. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    You could create two NSStrings in your header, and when you need to call them, initialize them and set them as the title for your alert.
    Then, in the buttonClicked method, check if the alert's title is the same as the string.

    Code:
    NSString *firstAlertTitle;
    NSString *secondAlertTitle;
    Code:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
            if(alertView.title == firstAlertTitle) {
                    //Check for button that was pressed in alertOne then execute action
             }
             if(alertView.title == secondAlertTitle) {
                    //Check for button that was pressed in alertTwo then execute action
             }
    }
  6. Cyanidepoison

    Cyanidepoison Active Member

    Joined:
    Jan 18, 2008
    Messages:
    1,076
    Likes Received:
    2
    err, nevermind on that.
  7. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    Thanks, Skylar.
  8. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    You don't need to use Ivars. I don't know why you all want to use Ivars. Just create macros if you want to do something like that. Ideally, track the alert views another way: tag them.

    I feel like you guys totally just ignored my post where I explained that he should have used a macro instead of using Ivars, or referencing by non localized strings, and whatnot.

    EDIT: This is exactly the reason why I don't really want to help you guys any more.

    EDIT II: And yes, you can localize the string in your macro. #define WARNING NSLocalizedString (@"Warning", @"comment") That would totally solve your dilemma and require next to no changes on your part. Again, tag instead.
  9. johnmabassa

    johnmabassa Member

    Joined:
    Mar 16, 2009
    Messages:
    87
    Likes Received:
    0
    Thanks for the help guys!! For time being I will continue with what Skylar suggested ie using #defines.


    -John

Share This Page