alertview help

Discussion in 'iOS Development' started by flyingguitar, Feb 7, 2010.

  1. flyingguitar

    flyingguitar Active Member

    Joined:
    May 14, 2008
    Messages:
    1,860
    Likes Received:
    11
    Device:
    iPhone 6
    how could I have a sequence of 3+ alert views.

    something like this:

    Code:
    -(IBAction)button1 {
    [INDENT]UIAlertView *test = [[UIAlertView alloc]initWithTitle:@"test" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"other", nil];
    [test show];
    [test release];[/INDENT]
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [INDENT]if (!buttonIndex == [alertView cancelButtonIndex]) {[/INDENT]
    [INDENT][INDENT]UIAlertView *test2 = [[UIAlertView alloc]initWithTitle:@"test2" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"other", nil];
    [test2 show];
    [test2 release];[/INDENT][/INDENT]
    [INDENT]}[/INDENT]
    }
    
    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    [INDENT]if (!buttonIndex == [alertView cancelButtonIndex]) {[/INDENT]
    [INDENT][INDENT]UIAlertView *test3 = [[UIAlertView alloc]initWithTitle:@"test3" message:nil delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"other", nil];
    [test3 show];
    [test3 release];[/INDENT][/INDENT]
    [INDENT]}[/INDENT]
    }
    now obviously that won't work, it's not supposed to, it's just meant to give you an idea of what I'm trying to go for...

    so I want the first view to appear, and if they hit the other button, the 2nd view appears, and if they hit the other button again then the 3rd view appears.
  2. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    That sounds super annoying, from a user perspective. Are you sure there is abolutely no better way to accomplish what you want to do?

    If you decide to continue this route, then just tag your UIAlertView with a unique tag. Possibly 1, 2, and 3.

    In your clickedButton method, set up a switch with the cases based on the alertView.tag. In each switch case, set up another switch based on the button's index. Too keep the code from getting too hard to read, set up each subswitch's case to include a method call that actually does what you want it to do.
  3. flyingguitar

    flyingguitar Active Member

    Joined:
    May 14, 2008
    Messages:
    1,860
    Likes Received:
    11
    Device:
    iPhone 6
    while in the actual code I'm righting there isn't going to be a third alerview, but the second one i going to fire off some code, I'm just using an alertView for now to make it visible that the code is functioning the way I want it to.

    I just want a way to find the buttinIndex of the second alertView
  4. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Okay, so do what I told you to do in my previous post in this thread.

    For the record, I still dislike the double alert.
  5. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    As Skylar said, it would be really annoying to the user (unless that's what you want?).

    Anyway, since I'm in a good mood, here's how I'd do it.

    [objc]...
    UIAlertView *alert1 = [blah blah init];
    [alert1 setTag:1];
    [alert1 show];
    [alert1 release];
    ...

    -(void)alertView

    Please Register or Log in to view images

    UIAlertView *)alertView clickedButtonAtIndex

    Please Register or Log in to view images

    NSInteger)buttonIndex {
    if(buttonIndex != [alertView cancelButtonIndex]) return;
    switch([alertView tag]){
    case 1:
    UIAlertView *alert2 = [blah blah init];
    [alert2 setTag:2];
    [alert2 show];
    [alert2 release];
    break;
    case 2:
    ... // you should get the point now.
    }
    }

    [/objc]
  6. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    [OBJC]if(buttonIndex != [alertView cancelButtonIndex]) return;[/OBJC]

    ; )
  7. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Actually, I just thought of a better more dynamic way.

    [objc]
    ...
    NSArray *titlesArray = [[NSArray alloc] initWithObjects

    Please Register or Log in to view images

    "Title 1", @"Title 2", nil];
    NSArray *bodiesArray = [[NSArray alloc] initWithObjects

    Please Register or Log in to view images

    "Body 1", @"Body 2", nil];
    NSUInteger maxNumberOfAlerts = 2;
    ...
    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:[titlesArray objectAtIndex:0] blah blah];
    [anAlert setTag:0];
    [anAlert show];
    [anAlert release];
    ...

    -(void)alertView

    Please Register or Log in to view images

    UIAlertView *)alertView clickedButtonAtIndex

    Please Register or Log in to view images

    NSInteger)buttonIndex{
    NSUInteger alertNumber = [alertView tag] + 1;
    if(buttonIndex == [alertView cancelButtonIndex] || alertNumber == maxNumberOfAlerts) return;

    UIAlertView *anAlert = [[UIAlertView alloc] initWithTitle:[titlesArray objectAtIndex:alertNumber] blah blah];
    [anAlert setTag:alertNumber];
    [anAlert show];
    [anAlert release];
    }
    [/objc]

    Disclaimer:
    Rather than telling you exactly how to do this annoying practice, I'm writing this as an example on how to reuse code. Don't copy and paste it, analyze it and use it ONLY if you understand what's happening.
  8. flyingguitar

    flyingguitar Active Member

    Joined:
    May 14, 2008
    Messages:
    1,860
    Likes Received:
    11
    Device:
    iPhone 6
    thanks guys, I figured that out, but thanks for the reply's.

    @ pelaez, I do understand what's happening. I never thought of adding 1 to the tag that's already being used, that's a great way to do it as it will save a ton of time
  9. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Although, why even use a new alert view? Why not, after hiding the current display, redisplay it with the new information. You're not thinking "as little memory as possible" enough.

    alertView.title = @"New Title";
    alertView.message = @"New Message";
    ++.alertView.tag;
    [alertView show]
  10. Pelaez-1

    Pelaez-1 New Member

    Joined:
    May 5, 2008
    Messages:
    822
    Likes Received:
    0
    Device:
    iPhone
    Didn't think of that.

    Now we have a super-efficient way of doing something neither of us wants to even think about.

    Please Register or Log in to view images



    I'd just change the tag first and then update the tittle and message with the one from the array, using the new tag.

Share This Page