我想在我的應用程序正在檢查用戶身份驗證時顯示一個活動指示符來代替我的右邊UIBarButtonItem。用戶點擊後立即顯示活動指示燈代替登錄按鈕
目前發生了什麼,用戶是否單擊登錄按鈕,然後用戶界面暫停並等待網絡電話返回。已建議我使用一個調度隊列在不同的線程運行這個,但是我不知道如何將這些兩段代碼結合起來:
調度隊列:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue,^{
//show your indicator button
dispatch_async(dispatch_get_main_queue(),^{
//stop indicator when user has logged in
});
});
登錄方法:
- (void) loginAction{
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]
initWithFrame:CGRectMake(0, 0, 20, 20)];
[activityIndicator startAnimating];
UIBarButtonItem *activityItem = [[UIBarButtonItem alloc]
initWithCustomView:activityIndicator];
self.navigationItem.rightBarButtonItem = activityItem;
if ([userNameTextField.text isEqualToString:@""] || [passwordTextField.text
isEqualToString:@""]) {
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login"
style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Login" message:@""
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
if (loginSuccess== true) {
//continue
}
}else
{
UIBarButtonItem *btnGo = [[UIBarButtonItem alloc] initWithTitle:@"Login"
style:UIBarButtonItemStyleBordered target:self action:@selector(loginAction)];
self.navigationItem.rightBarButtonItem = btnGo;
[self.navigationItem.rightBarButtonItem setTintColor:[UIColor colorWithRed:44.0/255.0
green:160.0/255.0 blue:65.0/255.0 alpha:1.0]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Invalid Login" message:@""
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
return;
}
}
我會在哪裏地方?在viewDidLoad中? – Brandon 2013-03-07 08:52:32
你把它放在那裏你想要顯示你的指標按鈕,所以goog的地方將viewWillAppear方法。 – edzio27 2013-03-07 08:54:13
嗨,我更新了我的問題。我無法將這一切放在一起。有任何指針? – Brandon 2013-03-07 09:31:05