单击uialertview中的按钮时如何关闭警报视图

时间:2012-05-11 08:15:39

标签: iphone objective-c ios

我是iOS的新手。

我正在处理alertviews。这是我的代码。这里有2个警报视图:successfulallertunsuccessfulallert用于登录页面。我也在这里使用alertview委托,它将适用于两个警报视图,但我想只为成功的alertview工作,并且只应为成功的alertview进行导航。如果有人知道这一点,请帮助我。

NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
    NSRange match;
    //  NSLog(@"string= %@", str);
    match = [responseOfResult rangeOfString: @"successful"];
    if(match.location == NSNotFound)
    {
        UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
                               initWithTitle:@"Alert"
                               message:responseOfResult
                               delegate:self
                               cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [unsuccessfulAllert show];

    }
    else {
        UIAlertView *successfulAllert = [[UIAlertView alloc]
                               initWithTitle:@"Message" message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [successfulAllert show];
     }
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0){
        [[self navigationController]pushViewController:registerUserScreen animated:YES];
    }
}

7 个答案:

答案 0 :(得分:1)

为什么不将“ON”作为取消按钮标题?一切都将自动处理。

UIAlertView *successfulAllert = [[UIAlertView alloc]
                               initWithTitle:@"Message" message:@"Login successful." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [successfulAllert show];

答案 1 :(得分:0)

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0){
        //POP here with this:
        [self.navigationController pushViewController:addItemView animated:NO];

    }
}

答案 2 :(得分:0)

将标记添加到两个警报视图并检查警报视图委托中的标记。

示例代码:

NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
//  NSLog(@"string= %@", str);
match = [responseOfResult rangeOfString: @"successful"];
if(match.location == NSNotFound)
{
    UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
                           initWithTitle:@"Alert"
                           message:responseOfResult
                           delegate:self
                           cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [unsuccessfulAllert setTag:1];
    [unsuccessfulAllert show];

}
else {
    UIAlertView *successfulAllert = [[UIAlertView alloc]
                           initWithTitle:@"Message" message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
    [successfulAllert setTag:2];
    [successfulAllert show];
 }

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag==2 && buttonIndex == 0){
    [[self navigationController]pushViewController:registerUserScreen animated:YES];
}

答案 3 :(得分:0)

是的,委托对两个alertview都有效,但你可以为每个alertview对象分配一个标签,并在委托中检查标签,然后如果特定的AlertView onject的标签匹配则执行事件。如果你需要代码,我将提供。

答案 4 :(得分:0)

NSString *responseOfResult = [[NSString alloc]initWithString:[result response]];
NSRange match;
//  NSLog(@"string= %@", str);
match = [responseOfResult rangeOfString: @"successful"];
if(match.location == NSNotFound)
{
    UIAlertView *unsuccessfulAllert = [[UIAlertView alloc]
                           initWithTitle:@"Alert"
                           message:responseOfResult
                           delegate:self
                           cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

    [unsuccessfulAllert setTag:1];

    [unsuccessfulAllert show];

}
else {
    UIAlertView *successfulAllert = [[UIAlertView alloc]
                           initWithTitle:@"Message" message:@"Login successful." delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK", nil];

    [successfulAllert setTag:2];
    [successfulAllert show];
 }
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(alertView.tag == 2)
{
    [[self navigationController]pushViewController:registerUserScreen animated:YES];
}
 else
 {
    //[[self navigationController]pushViewController:registerUserScreen animated:NO];
     // OR
     return;
 }
}

答案 5 :(得分:0)

您可以通过多种方式更正代码,第一种也是非常常见的方法是使用tag的{​​{1}}属性(整数)。由于UIView继承自UIAlertview,因此它具有UIView属性,因此每次要创建警报(或视图)时,请设置标记并检查您的条件如下:

tag

然后知道警报正在调用回调:

...
alert.tag=1;
[alert show];
另一种方式,在你的情况下,可能是:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
  if(alertView.tag==theTagOfYourAlert){
     //do your stuff
   }
}

答案 6 :(得分:0)

对于登录状态更新等内容,您可能希望“登录成功”消息自动消失。试试这个:

https://github.com/camclendenin/flashbox

这很好用,对于像这样的情况派上用场。此外,您无需处理UIAlertViews所涉及的所有混乱。