简体   繁体   中英

popViewController doesn't work with UIAlertView

I am having problem with AlertView. I am trying to use the UIAlertView and after click ok it will return back to the previous screen but it do not seems to work any advice ?

if (xGPSCoordinate==0 && yGPSCoordinate == 0) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" 
                                                    message:@"Failed to load the to get your current location"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil, nil];
    [alert show];
    [alert release];

    return;
    [self.navigationController popViewControllerAnimated:YES];
} 

or

if (xGPSCoordinate==0 && yGPSCoordinate == 0) {

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"iPoly" 
                                                    message:@"Failed to load the to get your current location"
                                                   delegate:self 
                                          cancelButtonTitle:@"Ok" 
                                          otherButtonTitles:nil, nil];
    [alert show];
    [alert release];
    [self.navigationController popViewControllerAnimated:YES];
    return;

}   

both doesn't work

For this purpose you've to use UIAlertView 's delegate method.

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

First use this in your @interface <UIAlertViewDelegate>

Then set the delegate, self.yourAlertView.delegate=self ;

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

       if(buttonIndex==0)//first button which should be the OK button
       {

              [self.navigationController popViewControllerAnimated:YES];

       }

 }

use the delegate method of UIAlertView, see the answer given by iNoob. It does not make a sense if you write anything after the "return;" statement as the code below "return;" statement will never get executed.

refer apple developer link for more details on UIAlertView delegate http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html

or a simple tutorial on alert view http://mobile.tutsplus.com/tutorials/iphone/uialertview/

You just need to implement UIAlerView Delegate Methods.

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

write your code for Pop to previous Controller here.You can the clicked button index and on the basis of that you can use.Don't for Conform UIAlertViewDelegate to Interface.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM