Ios 用户按了我的UIAlertView上的哪个按钮?
我已经创建了一个Ios 用户按了我的UIAlertView上的哪个按钮?,ios,objective-c,cocoa-touch,uialertview,uialertviewdelegate,Ios,Objective C,Cocoa Touch,Uialertview,Uialertviewdelegate,我已经创建了一个UIAlertView,现在我想检查用户按下了哪个按钮 我的代码是: - (IBAction)button1 { { UIAlertView *alert1 = [[UIAlertView alloc] init]; [alert1 setTitle:@"Hello"]; [alert1 setMessage:@"Do you like smoking?"]; [alert1 addButtonWithTit
UIAlertView
,现在我想检查用户按下了哪个按钮
我的代码是:
- (IBAction)button1 {
{
UIAlertView *alert1 = [[UIAlertView alloc] init];
[alert1 setTitle:@"Hello"];
[alert1 setMessage:@"Do you like smoking?"];
[alert1 addButtonWithTitle:@"Yes"];
[alert1 addButtonWithTitle:@"No"];
[alert1 show];
}
}
如何使用if-else语句检查它?在视图控制器中实现
UIAlertViewDelegate
协议,将其设置为UIAlertView
的委托,然后等待alertView:ClickedButtonIndex:
事件,如下所示:
@interface MyViewController : UIViewController <UIAlertViewDelegate>
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@implementation MyViewController
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Button %d clicked...", (int)buttonIndex);
}
@end
@接口MyViewController:UIViewController
-(void)alertView:(UIAlertView*)alertView
单击按钮索引:(NSInteger)按钮索引;
@结束
@MyViewController的实现
-(void)alertView:(UIAlertView*)alertView
单击按钮索引:(NSInteger)按钮索引{
NSLog(@“按钮%d已单击…”,(int)按钮索引);
}
@结束
更改显示警报视图的代码,如下所示:
UIAlertView *alert1 = [[UIAlertView alloc] init];
[alert1 setTitle:@"Hello"];
[alert1 setMessage:@"Do you like smoking?"];
[alert1 addButtonWithTitle:@"Yes"];
[alert1 addButtonWithTitle:@"No"];
alert1.delegate = self; // <<== Add this line
[alert1 show];
UIAlertView*alert1=[[UIAlertView alloc]init];
[alert1 setTitle:@“你好”];
[alert1 setMessage:@“你喜欢吸烟吗?”;
[alert1 AddButton,标题:@“是”];
[alert1 AddButton,标题为@“否”];
alert1.delegate=self;// 在视图控制器中实现UIAlertViewDelegate
协议,将其设置为UIAlertView
的委托,然后等待alertView:ClickedButtonIndex:
事件,如下所示:
@interface MyViewController : UIViewController <UIAlertViewDelegate>
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex;
@end
@implementation MyViewController
-(void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"Button %d clicked...", (int)buttonIndex);
}
@end
@接口MyViewController:UIViewController
-(void)alertView:(UIAlertView*)alertView
单击按钮索引:(NSInteger)按钮索引;
@结束
@MyViewController的实现
-(void)alertView:(UIAlertView*)alertView
单击按钮索引:(NSInteger)按钮索引{
NSLog(@“按钮%d已单击…”,(int)按钮索引);
}
@结束
更改显示警报视图的代码,如下所示:
UIAlertView *alert1 = [[UIAlertView alloc] init];
[alert1 setTitle:@"Hello"];
[alert1 setMessage:@"Do you like smoking?"];
[alert1 addButtonWithTitle:@"Yes"];
[alert1 addButtonWithTitle:@"No"];
alert1.delegate = self; // <<== Add this line
[alert1 show];
UIAlertView*alert1=[[UIAlertView alloc]init];
[alert1 setTitle:@“你好”];
[alert1 setMessage:@“你喜欢吸烟吗?”;
[alert1 AddButton,标题:@“是”];
[alert1 AddButton,标题为@“否”];
alert1.delegate=self;// 您可以做的最好的事情是使显示警报视图的类符合UIAlertViewDelegate
协议,并实现方法–alertView:ClickedButtonIndex:
。这样您就知道单击了什么按钮
希望这有帮助 您能做的最好的事情是使显示警报视图的类符合UIAlertViewDelegate
协议,并实现方法–alertView:ClickedButtonIndex:
。这样您就知道单击了什么按钮
希望这有帮助 必须将UIAlertView
的委托设置为将处理来自UIAlertView
本身的回调的类
例如
其中,self
是实现协议的当前UIViewController
当用户点击按钮时,UIAlertView
将回调您设置为代理的任何对象,在我的示例中,我们使用创建UIAlertView
的UIViewController
。一旦我们得到回调,我们可以检查哪个按钮索引被点击,并相应地采取行动。这是一种基本的委托模式,在整个iOS开发过程中使用,尤其是UIKit
示例
@interface MyViewController : UIViewController() <UIAlertViewDelegate>
@end
@implementation MyViewController
- (IBAction)button1 {
{
UIAlertView *alert1 = [[UIAlertView alloc] init];
[alert1 setTitle:@"Hello"];
[alert1 setMessage:@"Do you like smoking?"];
[alert1 addButtonWithTitle:@"Yes"];
[alert1 addButtonWithTitle:@"No"];
[alert1 setDelegate:self];
[alert1 show];
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Handle interaction
switch (buttonIndex)
{
case 0:
NSLog(@"Yes was pressed");
break;
case 1:
NSLog(@"No was pressed");
break;
}
}
@end
@接口MyViewController:UIViewController()
@结束
@MyViewController的实现
-(iAction)按钮1{
{
UIAlertView*alert1=[[UIAlertView alloc]init];
[alert1 setTitle:@“你好”];
[alert1 setMessage:@“你喜欢吸烟吗?”;
[alert1 AddButton,标题:@“是”];
[alert1 AddButton,标题为@“否”];
[alert1 setDelegate:self];
[警报1显示];
}
}
#pragma标记-UIAlertViewDelegate
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
{
//处理交互
开关(按钮索引)
{
案例0:
NSLog(按下“是”);
打破
案例1:
NSLog(@“未按下”);
打破
}
}
@结束
在头文件或类接口扩展名中指定类实现
,这一点非常重要
我建议您查看以了解更多信息,对于许多其他UIKit组件,您可以采用这种方法。您必须将UIAlertView
的委托设置为将处理来自UIAlertView
本身的回调的类
例如
其中,self
是实现协议的当前UIViewController
当用户点击按钮时,UIAlertView
将回调您设置为代理的任何对象,在我的示例中,我们使用创建UIAlertView
的UIViewController
。一旦我们得到回调,我们可以检查哪个按钮索引被点击,并相应地采取行动。这是一种基本的委托模式,在整个iOS开发过程中使用,尤其是UIKit
示例
@interface MyViewController : UIViewController() <UIAlertViewDelegate>
@end
@implementation MyViewController
- (IBAction)button1 {
{
UIAlertView *alert1 = [[UIAlertView alloc] init];
[alert1 setTitle:@"Hello"];
[alert1 setMessage:@"Do you like smoking?"];
[alert1 addButtonWithTitle:@"Yes"];
[alert1 addButtonWithTitle:@"No"];
[alert1 setDelegate:self];
[alert1 show];
}
}
#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// Handle interaction
switch (buttonIndex)
{
case 0:
NSLog(@"Yes was pressed");
break;
case 1:
NSLog(@"No was pressed");
break;
}
}
@end
@接口MyViewController:UIViewController()
@结束
@MyViewController的实现
-(iAction)按钮1{
{
UIAlertView*alert1=[[UIAlertView alloc]init];
[alert1 setTitle:@“你好”];
[alert1 setMessage:@“你喜欢吸烟吗?”;
[alert1 AddButton,标题:@“是”];
[alert1 AddButton,标题为@“否”];
[alert1 setDelegate:self];
[警报1显示];
}
}
#pragma标记-UIAlertViewDelegate
-(无效)alertView:(UIAlertView*)alertView单击按钮索引:(NSInteger)按钮索引
{
//处理交互
开关(按钮索引)
{
案例0:
NSLog(按下“是”);
打破
案例1:
NSLog(@“未按下”);
打破
}
}
@结束
在头文件或类接口扩展名中指定类实现
,这一点非常重要
我建议您查看以了解更多信息,您可以按照
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == 0) {
// i love smoking!
} else if (buttonIndex == 1) {
// i hate smoking!
}
}