警报视图按钮

时间:2012-07-14 11:43:41

标签: ios xcode button xcode4.3 uialertview

我正在创建一个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];

        }

我想不想在哪里添加链接等等。谢谢

3 个答案:

答案 0 :(得分: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
 }

一个建议,如果你在问题中更清楚你想要做什么,你可能会得到更多有用的答案......

希望有所帮助

答案 1 :(得分: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

答案 2 :(得分:0)

您需要实施UIAlertViewDelegate方法:– alertView:didDismissWithButtonIndex: 在启动它时将控制器设置为UIAlertViewDelegate,然后在– alertView:didDismissWithButtonIndex:的实现中,调用与相关按钮索引相关的各种方法。