UIAlertView warning (code will never be executed)

Discussion in 'iOS Programming' started by abcdefg12345, Dec 4, 2014.

  1. abcdefg12345 macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #1
    Hi I'm new to iOS programming and trying to port my OS X Apps to IOS and I'm stuck in "UIAlertView" coding i keep on getting the warning "code will never be executed"

    Code:
    - (IBAction)popup:(id)sender { UIAlertView *alertView; alertView = [[UIAlertView alloc] initWithTitle:@"the title" message:@"" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"a", @"b" , @"c", nil]; [alertView show];
        if (@"a") {
            [sender setTitle: @"a"forState:UIControlStateNormal];
        }else if(@"b") { // this line is where  get the error
    
        }
    }
    any fix for it, i also cant do it without the "else" in the statement as it will do whatever the last "if statement" is
     
  2. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #2
    First are foremost, you shouldn't use UIAlertView as it's been deprecated in iOS 8 in favor of UIAlertController. It's much more straight forward.

    What exactly are you intending your if-statement to do?

    As it currently reads, you're creating a UIAlertView, then showing it. Then, after it is shown, you run an if block. Do you mean to handle feedback from the Alert? If so, you'll have to use the delegate pattern, which you can read about, and other common patterns, here. Setting the delegate class (not to nil, most likely the one that creates the alert), and then implementing the correct method:

    Code:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    When a button is pressed, the alert view will fire that method to your delegate, letting you know which button was pressed. (0 = a, 1 = b, 2 = c, in this case).

    Now, as to why you are getting that error... because it's important for you to see what is happening here.

    Code:
    if (@"a") {
    This will always return true, therefore code written in your else brackets will never run. That is what the error is telling you. Now why:

    @"a" returns a string containing the character "a", which is true because it exist. If blocks evaluate their statements based on true or false. See the issue here?

    Using a comparison allows you to check against values:
    Code:
    if(@"a" == stringVariable) {
    Now, just tie these two concepts together - compare for the buttonIndex in alertView:clickedButtonAtIndex: to determine which buttons are pressed, and do what accordingly.
     
  3. abcdefg12345 thread starter macrumors regular

    abcdefg12345

    Joined:
    Jul 10, 2013
    #3
    Thanks for the help

    thats what worked for me

    Code:
    - (IBAction)popup:(id)sender { 
    UIAlertView *alertView; alertView = [[UIAlertView alloc] initWithTitle:@"the title" message:@"" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:@"a", @"b" , @"c", nil]; [alertView show];
    }
    
    -(void) alertView:(UIAlertView *) alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        
        switch (buttonIndex) {
                
            case 0:
                NSLog(@"Cancel!");
                break;
                 //cancel button
            case 1:
            NSLog(@"a!");
                // do something
           break;
           case 2:
           NSLog(@"b!");
                // do something else 
           break;
    }
    }
     
  4. Sean7512 macrumors 6502a

    Joined:
    Jun 8, 2005
    #4
    Yes, this is true; however that is not a real possibility yet as most devs need to support pre-iOS 8. If you are only targeting iOS 8+ then you should absolutely not be using UIAlertView.
     
  5. Mascots macrumors 65816

    Mascots

    Joined:
    Sep 5, 2009
    #5
    Thanks for the addition, I should have mentioned that.

    Assuming that abc is building a new app, I'd suggest targeting iOS 8+ only. But then again, I'm always pushing for advancement :cool:
     

Share This Page