2010-12-16 39 views
0

我需要彈出警報,當我的應用程序加載的......我把它叫做didfinished後單擊確定按鈕需要顯示另一個警報消息我使用clickedButtonAtIndex發射後.. ...檢測其UIAlertView中被點擊

現在當我點擊確定按鈕時,它的呼叫一次又一次.. alertview ..

我只需要撥打一次...該怎麼辦?

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

} 

@提前感謝。

+2

組代表:無在第二alertView – Saawan 2010-12-16 04:35:34

+0

@感謝ranjeet.sajwan – 2010-12-16 04:36:44

+0

k總是歡迎........ – Saawan 2010-12-16 04:38:18

回答

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

} 
+0

@Kiran這是您編碼警報按鈕代碼時的方式(如果您需要對第二警報點擊執行任何操作)。 – Ishu 2010-12-16 04:45:12

4

設置delegate:nil在第二alertView
我的意思是

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]; 
} 
+0

+1爲正確的地方捕捉正確的東西。 – Ishu 2010-12-16 04:43:00

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

同爲推送通知。

0

雖然你已經得到了很多實現方案......但我認爲更好的做法是爲每個AlertView分配一個標籤,並在檢測到按鈕點擊之前檢查調用AlertView.Hope的標籤,這有助於。 @Gerard:雖然位置管理器和推送通知服務都會引發系統生成的消息,但用戶不會再次查看這些消息。所以更好的HIG投訴方法是無論何時需要推送或位置管理器,應用程序生成消息。

3

您還可以添加標籤,就可以AlertView後來TE檢查標籤上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 
    } 
}