检测哪个 UIAlertView 被点击

发布于 2024-10-08 12:29:45 字数 1259 浏览 0 评论 0原文

我需要在加载应用程序时弹出警报...我称之为“完成启动”.. 单击“确定”按钮后需要显示另一条警报消息,我使用 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];
    }

}

@提前致谢。

i need to pop up alert when my application loaded... I called it didfinished launching..
after clicking ok button need to show another alert message i use clickedButtonAtIndex...

Now when I clicked the ok button its calling again and again.. the alertview..

I need to call only once... what to do?

-(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 in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

琉璃繁缕 2024-10-15 12:29:45

在第二个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];
}

set delegate:nil in second alertView
I mean

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];
}
我爱人 2024-10-15 12:29:45

您还可以向 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
    }
}

You can also add a tag to your AlertView and te check the tag later on the clickedButtonAtIndex method

        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];

then

    -(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex ==0 && alertView.tag==123){
       // Do what ever you wish
    }
}
恬淡成诗 2024-10-15 12:29:45

定义每个 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  */
    }

}

Define each UIAlertView and in the delegate look for which Alert to respond to:

-(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  */
    }

}
合久必婚 2024-10-15 12:29:45

如果您想要接收位置,只需请求,系统就会自动为您显示消息。请按照以下示例操作:

- (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

相同用于推送通知。

If what you want is to receive the location, just request it and the system will automatically show the message for you. Follow this example:

- (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

Same for push notifications.

单身狗的梦 2024-10-15 12:29:45

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

though u got many implementation solutions already ...but i think better practice would be to assign a tag to each of your AlertView and before detecting the button tapped check the tag of invoking AlertView.Hope this helps.
@Gerard:Though both location manager and push notification service raise system generated messages ,these can be checked off by users not to be shown again.So better HIG complaint method is application generating message whenever push or location manager are required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文