2012-05-11 45 views
0

我是iOS新手。如何在點擊該警報視圖中的按鈕時關閉alertview

我正在使用alertviews。這是我的代碼。這裏有2個警報視圖:登錄頁面的successfulallertunsuccessfulallert。我也在這裏使用了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

添加標籤兩個警報意見,併爲您在警報視圖委託標籤。

示例代碼:

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

你在第二個if語句中缺少'.tag'。 – Mat

+0

你是對的墊子。我現在糾正了錯誤。 –

0

是委託將工作既alertviews,但你可以將代碼分配給每個alertview對象併爲您在這個代表的標籤,然後,如果執行事件,標籤特別AlertView onject matches.If你需要代碼,我會提供。

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屬性(整數)。由於從UIViewUIAlertview繼承,它具有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?一切都會自動處理。

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

對於登錄狀態更新等內容,您可能希望「登錄成功」消息自動消失。試試這個:

https://github.com/camclendenin/flashbox

這很好地工作,並就派上用場了這樣的情況。另外,您不必處理與UIAlertView相關的所有混亂情況。

相關問題