2012-07-14 186 views
1

我正在創建一個iOS應用程序,當分數達到100時,此警報將顯示,並且它一切正常,但按鈕(共享,蘋果,評價這個應用程序)。警報視圖按鈕

 - (void) buttonAction { 
       counter++; 
       if(counter == 100) 
        [self showAlert]; 
      } 

      - (void) showAlert { 

       UIAlertView *alert = [[UIAlertView alloc] 

             initWithTitle:@"hello" 
             message:@"whats you name" 
             delegate:nil 
             cancelButtonTitle:@"Dismiss" 
             otherButtonTitles:@"share", @"apple" , @"rate this app", nil]; 


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
     if (buttonIndex == 0) { // means share button pressed 
      // write your code here to do whatever you want to do once the share button is pressed 
     } 
     if(buttonIndex == 1) { // means apple button pressed 
      // write your code here to do whatever you want to do once the apple button is pressed 
     } 
     // and so on for the last button 
    } 

       [alert show]; 



      } 

      -(IBAction)plus { 
       counter=counter + 1; 
       count.text = [NSString stringWithFormat:@"%i",counter]; 
       if(counter == 100) 
        [self showAlert]; 

      } 

      -(IBAction)zero { 
       counter=0; 
       count.text = [NSString stringWithFormat:@"%i",counter]; 
      } 



     - (void)viewDidLoad { 
      counter=0; 
      count.text = @"0"; 
       [super viewDidLoad]; 

     } 

我想沒有我在哪裏可以添加鏈接等什麼謝謝

+0

我不明白你想要什麼。 – 2012-07-14 11:52:36

+0

@PrasadG只是一個方法真的如何我可以使按鈕(蘋果等去蘋果網站例如 – Picm 2012-07-14 12:08:50

回答

11

請嘗試以下...

UIAlertView *alert = [[UIAlertView alloc] 

         initWithTitle:@"hello" 
         message:@"whats you name" 
         delegate:self 
         cancelButtonTitle:@"Dismiss" 
         otherButtonTitles:@"share", @"apple" , @"rate this app", nil]; 

[alert show]; 

,然後在你的代碼添加下面的方法:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ 
     if (buttonIndex == 0) { // means share button pressed 
       // write your code here to do whatever you want to do once the share button is pressed 
     } 
     if(buttonIndex == 1) { // means apple button pressed 
       // write your code here to do whatever you want to do once the apple button is pressed 
     } 
     // and so on for the last button 
} 

一個建議,你可能會得到更有幫助的答案,如果你更清楚你的問題究竟是什麼你想做的事......

希望它有助於

+0

我剛剛添加它,我得到以下錯誤:使用未聲明的標識符'alertView'你的意思UIAlertView?和感謝您的建議,我會記住這一點,也是我在哪裏添加http://www.apple.com謝謝你 – Picm 2012-07-14 12:37:35

+0

,我編輯了我的答案,這就是我把你的代碼查找 – Picm 2012-07-14 12:38:21

+0

我編輯你的代碼現在它應該工作...隨時標記我的答案爲正確的,一旦它適用於你:) – 2012-07-14 12:49:12

0

從你的問題我的理解是,你需要確定哪些警報按鈕,用戶按下一個方法。爲此,您可以使用此代理方法:

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

} 

在此方法中,您需要檢查用戶按下的按鈕索引。 並據此,你可以做其他的東西。

對於使用委託方法,你需要警惕鑑於delegate設置爲self

更多參考: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertViewDelegate_Protocol/UIAlertViewDelegate/UIAlertViewDelegate.html#//apple_ref/doc/uid/TP40007548

0

您需要實現UIAlertViewDelegate方法:– alertView:didDismissWithButtonIndex: 當你初始化時將你的控制器設置爲UIAlertViewDelegate,然後在你的實現– alertView:didDismissWithButtonIndex:中調用各種方法pertai寧到相關按鈕索引。