检测单击了哪个UIAlertView

时间:2010-12-16 04:28:45

标签: iphone objective-c cocoa-touch uialertview

我需要在我的应用程序加载时弹出警报...我称它已完成启动.. 单击确定按钮后需要显示另一个警告消息我使用clickedButtonAtIndex ...

现在当我点击确定按钮时,它一次又一次地呼叫..警报视图..

我只需要打一次电话......该怎么办?

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    

    // Override point for customization after application launch.

    // Add the tab bar controller's view to the window and display.
    [window addSubview:tabBarController.view];
    [window makeKeyAndVisible];
    viewControllersList = [[NSMutableArray alloc] init];

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Alow this app to use your GPS location"
    delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
    [alert show];
    [alert release];

}

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


    if (buttonIndex==0) {
        NSLog(@"NO");
    }
    else    {

        NSLog(@"Yes");
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you want's to receive Push messages."
        delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
        [alert show];
        [alert release];
    }

}

@thanks提前。

5 个答案:

答案 0 :(得分:4)

在第二个alertView中设置delegate:nil 我的意思是

if (buttonIndex==0) { NSLog(@"NO"); } else {

NSLog(@"Yes");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you want's to receive Push messages."
delegate:nil cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
[alert show];
[alert release];
}

答案 1 :(得分:3)

定义每个UIAlertView并在委托中查找要响应的警报:

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

    if(alert1) {
        if (buttonIndex==0) { 

            NSLog(@"NO"); 
        } else {

            NSLog(@"Yes");
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" message:@"Do you want's to receive Push messages." delegate:self cancelButtonTitle:@"No" otherButtonTitles:@"Yes", nil];
            [alert show];
            [alert release];
        }
    } else {

        /*  the second alertview  using the same buttonIndex  */
    }

}

答案 2 :(得分:3)

您还可以在AlertView中添加标签,稍后在clickedButtonAtIndex方法中检查标签

        UIAlertView* alert = [[UIAlertView alloc]initWithTitle:@"" message:@"All local datawill be erased. Erase local data?" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Erase", @"Cancel",nil];
        alert.tag =123; // added the tag so we can prevent other message boxes ok button to mix up 
        [alert show];

然后

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex ==0 && alertView.tag==123){
       // Do what ever you wish
    }
}

答案 3 :(得分:1)

如果你想要的是接收位置,只需要它,系统会自动为你显示信息。请遵循以下示例:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
[window addSubview:tabBarController.view];
// Create a location manager instance to determine if location services are enabled. This manager instance will be
// immediately released afterwards.
CLLocationManager *manager = [[CLLocationManager alloc] init];
if (manager.locationServicesEnabled == NO) {
    UIAlertView *servicesDisabledAlert = [[UIAlertView alloc] initWithTitle:@"Location Services Disabled" message:@"You currently have all location services for this device disabled. If you proceed, you will be asked to confirm whether location services should be reenabled." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [servicesDisabledAlert show];
    [servicesDisabledAlert release];
}
[manager release];                                  

}

http://developer.apple.com/library/ios/#samplecode/LocateMe/Listings/Classes_AppDelegate_m.html%23//apple_ref/doc/uid/DTS40007801-Classes_AppDelegate_m-DontLinkElementID_4

推送通知相同。

答案 4 :(得分:0)

虽然你已经有了很多实现解决方案......但我认为更好的做法是为每个AlertView分配一个标签,然后在检测到按钮点击之前检查调用AlertView.Hope的标签这有帮助。 @Gerard:虽然位置管理器和推送通知服务都会引发系统生成的消息,但用户可以检查这些消息,以免再次显示。更好的HIG投诉方法是每当需要推送或位置管理器时应用程序生成消息。