0

私はiOSが初めてです。

私はアラートビューに取り組んでいます。これが私のコードです。ここには 2 つのアラートビューがありsuccessfulallertますunsuccessfulallert。ログイン ページ用です。ここでもアラートビューデリゲートを使用しています。両方のアラートビューで機能しますが、アラートビューが成功した場合にのみ機能し、アラートビューが成功した場合にのみナビゲーションを実行する必要があります。誰かがこれを知っているなら、私を助けてください。

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

cancelButtonTitle に「OK」を入れてみませんか?すべてが自動的に処理されます。

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

はい、デリゲートは両方のアラートビューで機能しますが、各アラートビューオブジェクトにタグを割り当て、デリゲートでタグを確認してから、その特定のAlertViewプロジェクトのタグが一致する場合はイベントを実行できます。コードが必要な場合は、提供します。

于 2012-05-11T08:29:06.730 に答える
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

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];
}
于 2012-05-11T08:25:27.533 に答える
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 に答える
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 に答える