我在一个方法中有 2 个 alertView,我想检查第二个 alertView 的按钮索引。我该怎么做?我想获取 else 条件的 buttonIndex。下面是我正在尝试执行的代码。
-(IBAction)buttonClick{
NSString *connect = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.xyz.com"] encoding:NO error:nil];
if(connect == NULL) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"No Internet Connection" message:@"Please check your internet connection" delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
} else {
UIAlertView *alert1=[[UIAlertView alloc]initWithTitle:@"Upload to the server?" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"CANCEL",nil];
[alert1 show];
}
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
My functionality
}
最佳答案
一种方法是让警报 View 的实例全局化并在警报 View 的委托(delegate)方法中检查它们:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
// assuming the myAlert2 is your alert's instance
if (alertView == myAlert2)
{
if (buttonIndex == 0)
{
...
}
}
}
或者您可以只给警报添加标签,然后在 alertView 的委托(delegate)方法中检查标签,例如:
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == 2)
{
if (buttonIndex == 0)
{
...
}
}
}
关于ios - 检查第二个 UIAlertView 的 buttonIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201242/