2014-10-02 117 views
0

由於某些原因,此代碼不會按順序執行。 Xcode似乎正在評估if-else語句,然後在顯示if-else語句之前的UIAlertView之前顯示if-else語句中的UIAlertView代碼不會按順序執行

下面是我供你參考代碼:

- (IBAction)btnLogin:(id)sender; { 
    //self.tbxUsername.text = [NSString stringWithFormat:@"vmcv"]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" 
                message: [NSString stringWithFormat: @"%@", acc.Email] 
                delegate:nil 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [alert show]; 


    if([self.tbxUsername.text isEqualToString:acc.Email ]) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication" 
                 message: @"Success" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication" 
                 message: @"Fail" 
                 delegate:nil 
               cancelButtonTitle:@"OK" 
               otherButtonTitles:nil]; 
     [alert show]; 
    } 
} 

回答

0

不應該像這樣按順序顯示多個UIAlertView。您需要等到上一個被解散,然後在委託回調中呈現新的。

您將希望能夠識別哪個UIAlertView正在調用委託方法,因此設置一些屬性來保存您的UIAlertViews。

@property (nonatomic, strong) UIAlertView *emailAlertView, *successAlertView, *failureAlertView; 

當您按下按鈕時,您將只啓動第一個UIAlertView詢問他們的電子郵件。確保你更新了你的代碼,將代理設置爲self,並且你的視圖控制器實現了UIAlertViewDelegate協議。

- (IBAction)btnLogin:(id)sender; { 
    self.tbxUsername.text = [NSString stringWithFormat:@"vmcv"]; 

    self.emailAlertView = [[UIAlertView alloc] initWithTitle:@"Email" 
                message: [NSString stringWithFormat: @"%@", acc.Email] 
                delegate:self 
              cancelButtonTitle:@"OK" 
              otherButtonTitles:nil]; 
    [self.emailAlertView show]; 
} 

現在實施委託方法,只有在第一個退出後才顯示下一個UIAlertView。

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex { 
    if(alertView == self.emailAlertView) { 
     if([self.tbxUsername.text isEqualToString:acc.Email]) 
     { 
      self.successAlertView = [[UIAlertView alloc] initWithTitle:@"Authentication" 
                  message: @"Success" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
      [self.successAlertView show]; 
     } 
     else 
     { 
      self.failureAlertView = [[UIAlertView alloc] initWithTitle:@"Authentication" 
                  message: @"Fail" 
                  delegate:nil 
                cancelButtonTitle:@"OK" 
                otherButtonTitles:nil]; 
      [self.failureAlertView show]; 
     } 
    } 
    else if(alertView == self.successAlertView) { 
     //handle dismiss event of success alert view 
    } 
    else if(alertView == self.failureAlertView) { 
     //handle dismiss event of failure alert view 
    } 
} 

這應該達到您要查找的「順序」效果。

+0

感謝您的啓發。 objective-c似乎與我以前的語言不同,比如c#和java。 – fuzionist 2014-10-02 17:48:12

+0

上面顯示的委託模式是在Objective-C和整個iOS SDK中使用的非常常見的設計模式。優秀的iOS開發人員熟悉它並將其用於自己的代碼中。要獲得更多有關委託的經驗,請嘗試實施和定製UITableView。 UITableView有一個委託和一個數據源協議,可以幫助你更多地瞭解這種設計模式。 – dfmuir 2014-10-02 18:09:08

0

繪圖不會發生在實時性。您發出繪圖命令,並且在您脫離方法後,屏幕實際上會在事件循環中繪製。你無法通過並看到你的屏幕更新。因此,您需要顯示第一個警報,退出您的方法,而不是第一個警報中的退出方法(按下按鈕)調用的第二個警報。

+0

嗨,抱歉,但我不熟悉objective-c。我想知道你是否可以通過一些骨架代碼來說明你的意思?十分感謝! – fuzionist 2014-10-02 17:44:36