xcode 警报视图按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11483465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alert view buttons
提问by Picm
I'm creating an iOS application and when the score reaches 100 this alert will show and it all work fine but the buttons (share,apple,rate this app).
我正在创建一个 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];
}
what i would like to no where do i add the link etc. thank you
我想要什么,我在哪里添加链接等。谢谢
回答by Obaid Maroof
Try the following...
尝试以下...
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"hello"
message:@"whats you name"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:@"share", @"apple" , @"rate this app", nil];
[alert show];
and then add the following method in your code:
然后在您的代码中添加以下方法:
-(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
}
one advise, you might get more helpful answers if you were more clear in your question what exactly you wanted to do...
一个建议,如果您更清楚自己的问题究竟想做什么,您可能会得到更多有用的答案......
hope it helps
希望能帮助到你
回答by Midhun MP
From your question I understand that, you need a method for identifying which alert button is pressed by the user. For that you can use this delegate method:
从您的问题我了解到,您需要一种方法来识别用户按下了哪个警报按钮。为此,您可以使用此委托方法:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
}
Inside this method, you need to check the button index that was pressed by the user. And according to that, you can do the other stuffs.
在此方法中,您需要检查用户按下的按钮索引。据此,您可以做其他事情。
For using the delegate method, you need to set the delegate
of alert view to self
要使用委托方法,您需要将delegate
警报视图设置为self
For further reference: http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
进一步参考:http: //developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UIAlertView_Class/UIAlertView/UIAlertView.html
回答by MattyG
You need to implement the UIAlertViewDelegatemethod: – alertView:didDismissWithButtonIndex:
Set your controller as the UIAlertViewDelegate when you init it, then inside your implementation of – alertView:didDismissWithButtonIndex:
, call the various methods pertaining to the relevant button index.
您需要实现UIAlertViewDelegate方法:– alertView:didDismissWithButtonIndex:
在初始化时将控制器设置为 UIAlertViewDelegate,然后在您的实现中– alertView:didDismissWithButtonIndex:
,调用与相关按钮索引相关的各种方法。