xcode 在 iphone sdk 4.3 中单击按钮的警报消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8906127/
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 message for the button click in iphone sdk 4.3
提问by rani
I am beginner to xcode programming.Please tell me how to display the alert message when we are going to click the button in xcode-iphone-4.3
我是 xcode 编程的初学者。请告诉我如何在 xcode-iphone-4.3 中单击按钮时显示警报消息
My Code is as follows,
我的代码如下,
- (IBAction)buttonPressed:(id)sender{
UIAlertView* mes=[[UIAlertView alloc] initWithTitle:@"Hello World!!!!!!"
message:@"This is the Iphone app" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
[mes show];
[mes release];
Please help me regarding this.
请帮我解决这个问题。
回答by Suraj Mirajkar
-(IBAction)buttonOnePressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Clicked button 1"
message: @"Alert Message here"
delegate: self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
[alert setTag:1];
[alert show];
}
-(IBAction)buttonTwoPressed:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc]initWithTitle: @"Clicked button 2"
message: @"Alert Message here"
delegate: self
cancelButtonTitle:@"Cancel"
otherButtonTitles:@"OK",nil];
[alert setTag:2];
[alert show];
}
Below is the delegate method to track which button on Alertview is hit.
下面是跟踪 Alertview 上哪个按钮被点击的委托方法。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 1) { // UIAlertView with tag 1 detected
if (buttonIndex == 0)
{
NSLog(@"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(@"user pressed Button Indexed 1");
// Any action can be performed here
}
}
else if (alertView.tag == 2) { // UIAlertView with tag 2 detected
if (buttonIndex == 0)
{
NSLog(@"user pressed Button Indexed 0");
// Any action can be performed here
}
else
{
NSLog(@"user pressed Button Indexed 1");
// Any action can be performed here
}
}
}
You can set tag to UIAlertView
in case you have more than one UIAlertView
s and can determine which UIAlertView
button is clicked in its delegate method clickedButtonAtIndex
using its respective tag.
UIAlertView
如果您有多个UIAlertView
s 并且可以使用其各自的标签确定UIAlertView
在其委托方法中单击了哪个按钮,则可以将 tag 设置clickedButtonAtIndex
为。
回答by Tendulkar
In IBAction you have to write the code and give the Connections to the Button
在 IBAction 中,您必须编写代码并将连接提供给按钮
回答by Satyam
Create the IBAction for your button and add the code for alert view in that method.
为您的按钮创建 IBAction 并在该方法中添加警报视图的代码。