Objective c IOS:UIAlert中的两个按钮控件
我在iAction中有以下代码:Objective c IOS:UIAlert中的两个按钮控件,objective-c,xcode,ios,uialertview,Objective C,Xcode,Ios,Uialertview,我在iAction中有以下代码: UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"NO!" message:@"danger" delegate:self
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"NO!"
message:@"danger"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Annul", nil];
[alertView show];
[alertView release];
现在,如果我按下OK,它必须做一件事,如果我按下环,它必须做另一件事。但这必须在IBAction内部完成 您需要实现UiAlertViewDelegate方法
因此,委托函数应该如下所示
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)//OK button pressed
{
}
else if(buttonIndex == 1)//Annul button pressed.
{
}
}
贾利亚的回答很好,只是它没有满足blackguardian在iAction方法中运行这个的请求。在CoCoatTouch中使用iAction方法块并等待UIAlertView的响应是错误的。iAction应该只显示UIAlertView。然后,Jhaliya答案中的委托函数应该解析继续进行的方式,OK或Inval。然后,此委托函数可以通过调用其他方法来执行操作。CocoaTouch的事件处理并不是为iAction方法设计的,以阻止等待进一步的用户输入
将链iAction->UIAlertView->alertView:ClickedButtonIndex:视为不使用UIAlertView时的iAction,并将当前代码放在该链之后的iAction中。必须在iAction中完成。这是什么意思?为什么不使用UIAlertviewDelegate回调ClickedButtonIndexi如果我使用此方法delegate在iAction内有效?我解释我的问题:如果我按OK,Ib操作将继续执行其操作,而如果我按Canal,iAction的指令将返回;
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex == 0)//OK button pressed
{
}
else if(buttonIndex == 1)//Annul button pressed.
{
}
}