UIAlertView Issue (Solved!)

Discussion in 'iOS Programming' started by matthew858, Apr 17, 2009.

  1. macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #1
    I have a button that I have placed in Interface Builder. The code that is uses is:

    Header File
    Code:
    - (IBAction)openBrowser;
    
    Main File
    Code:
    -(IBAction)openBrowser {	
    	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://macrumors.com"]];
    }
    
    The button is connected to File's Owner, which is linked to the main file that the above code comes from.

    It seems alright, but when I run it in on a device, I get the following:

    What have I done wrong?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Have you set the class of the FileOwner correctly in IB? Also do IBActions not usually take a single argument of type id?
     
  3. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #3
    I have set the class to "SettingsViewController" which is where my data is coming from. I thought you didn't need so set the argument of type id when using openURL?

    Edit:

    I fixed this issue, but I have another one. I can now get the alert to show, but when I click "OK" the app freezes and I need to quit it. The code I'm using is:
    Code:
    -(IBAction)aboutPopup 
    {
    	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"UIAlertView" message:@"<Alert message>"
    												   delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    	[alert show];
    	[alert release];
    }
    
    Any ideas?
     
  4. macrumors regular

    Joined:
    Apr 9, 2009
    #4
    pretty simple: you show the alert, then you release it. so when your button is pressed, the alert-object is already destroyed.

    you should set a delegate that conforms to the "UIAlertViewDelegate" Protocol. then you can use alertView:clickedButtonAtIndex: to get a clicked button (in this case you only have one so that's pretty simple :D) and if you don't need the alertView after that you should be able to release it inside that method (I didn't test it, it could be possible that the alert view is still need because of the animation that dismisses it, but I guess releasing it in alertView:clickedButtonAtIndex: should work. try it and let me know! I'm not on my mac right now so I can't test it unfortunatly.)

    If that doesn't work you can release the view in alertView:didDismissWithButtonIndex: btw

    PS: I made that very same mistake when I first used an alertview :D
     
  5. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #5
    I only have one issue with that, and that's I already have another UIAlertView declared in the same file. How do I tell the two apart?
     
  6. macrumors regular

    Joined:
    Apr 9, 2009
    #6
    several ways to do it.
    for example, you could store your alertviews in instance variables and then just see if the alertView variable in alertView:clickedButtonAtIndex: is the same as your instance variable.

    or you could, for example, examine the alertview's message. if the message is <Alert message> then you have one view, and when the message is something else it's the other view.
    or you can inspect the title if your view's have one, or the number of buttons ... just take something that's different in the two views and check for it.
     
  7. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #7
    What is the best way of doing this? Setting the value of a variable when executing the alert, then checking for the value of the variable when running clickButtonAtIndex?
     
  8. macrumors 68010

    Joined:
    Aug 23, 2008
    #8
    delegate should just be nil. As for releasing it, that's the right way to do it, since the alert is only relevant within the method that is being called, retaining it is pointless.

    Other than that, I don't see any other reason why your app is frozen, could be something else entirely. Is there another line of code under the alert that may prompt the app to crash?
     
  9. macrumors 68000

    Joined:
    Feb 12, 2008
    Location:
    Illinois
    #9
    You actually should release the alert after you show it. I don't think that's the problem as I do it all the time without fail (note that the documentation also does that as well as the sample code). It's probably what you said afterwards...how they don't have the delegate.

    You could put both alertviews in the header file:

    PHP:
    @interface RootViewController UIViewController {
        
    UIAlertView *firstAlert;
        
    UIAlertView *secondAlert;
    }

    @
    end
    Then

    PHP:
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
      if (
    alertView == firstAlert) {
    }
    else { 
    // or else if (alertView == secondAlert)
    }
    }
     
  10. thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
  11. macrumors regular

    Joined:
    Apr 9, 2009
    #11
    but where does the alertview in alertView:clickedButtonAtIndex: come from if you already released the view?
    maybe the alertview is retained when you show it, I don't know about that. I will check that if I have the time on my mac later ...
     

Share This Page