2012-05-11 5 views
0

私はiOSを初めて使っています。そのalertviewのボタンをクリックするとalertviewを解除する方法

私はalertviewsに取り組んでいます。ここに私のコードです。ログインページには、successfulallertunsuccessfulallertという2つのアラートビューがあります。私はalertviewデリゲートをここでも使用していますが、両方のalertviewsで動作しますが、正常な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]; 
    } 
} 

答えて

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

    } 
} 
0

2つのアラートビューにタグを追加し、アラートビューのデリゲートでタグを確認してください。

サンプルコード:

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]; 
} 
+0

2番目の 'if'ステートメントに' .tag'がありません。 – Mat

+0

あなたは正しいマットでした。今私は間違いを訂正しました。 –

0

はいデリゲートがalertviewsの両方のために働くだろうが、あなたは、各alertviewオブジェクトにタグを割り当てると、デリゲートにタグをチェックし、そのためのタグ場合は、イベントを実行することができます特定のAlertViewが一致するかどうかを調べます。コードが必要な場合は、提供します。

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; 
} 
} 
0

あなたはあなたのコードを修正する多くの方法があり、第一及び非常に一般的には、UIViewtagプロパティ(整数)を使用することです。 UIViewからUIAlertview継承しているので、それは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 
    } 
} 
1

なぜないあなたcancelButtonTitleとして「OK」を入力?すべてが自動的に処理されます。

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

ログインステータスの更新のような場合は、「ログインに成功しました」というメッセージが自動的に消えてしまうことがあります。代わりにこれを試してみてください:

https://github.com/camclendenin/flashbox

これはうまく動作し、このような状況のために便利です。さらに、UIAlertViewsに関わるすべての混乱を処理する必要はありません。

関連する問題