2012-07-14 143 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宁到相关按钮索引。