0

我是 iOS 新手。

我正在处理警报视图。这是我的代码。这里有 2 个警报视图:successfulallertunsuccessfulallert用于登录页面。我在这里也使用了 alertview 委托,它适用于两个 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];
    }
}
4

7 回答 7

1

你为什么不把 "OK" 作为 cancelButtonTitle?一切都会自动处理。

UIAlertView *successfulAllert = [[UIAlertView alloc]
                               initWithTitle:@"Message" message:@"Login successful." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [successfulAllert show];
于 2012-05-11T09:19:03.513 回答
0

您有很多方法可以更正您的代码,第一种也是非常常见的方法是tag使用UIView. 由于UIAlertview继承自UIView,它具有tag属性,因此每次您要创建警报(或视图)时,请设置标签并检查您的条件,例如:

...
alert.tag=1;
[alert show];

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

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

在您的情况下,另一种方式可能是:

if([alertView.title isEqualToString:@"Alert"]){
       //do your stuff
    }
}
于 2012-05-11T08:55:06.987 回答
0

对于诸如登录状态更新之类的事情,您可能希望“登录成功”消息自动消失。试试这个:

https://github.com/camclendenin/flashbox

这很好用,在这种情况下派上用场。另外,您不必处理与 UIAlertViews 相关的所有混乱。

于 2012-05-22T16:39:17.170 回答
0
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(buttonIndex == 0){
        //POP here with this:
        [self.navigationController pushViewController:addItemView animated:NO];

    }
}
于 2012-05-11T08:25:08.640 回答
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];
}
于 2012-05-11T08:25:27.533 回答
0

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

于 2012-05-11T08:29:06.730 回答
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;
 }
}
于 2012-05-11T08:51:16.453 回答