2014-07-17 26 views
0

我有一個UIButton,當用戶單擊按鈕時,我添加了一個UIActivtyIndi​​catorView。用戶點擊標記爲「GO」的UIButton,然後activityIndi​​cator應該進入並覆蓋「GO」標題並旋轉,直到在後臺停止任何事情。將UIActivityIndi​​catorView添加到UIButton的頂視圖

- (void)going:(UIButton *)button { 

NSLog(@"going button pressed"); 

goingActivity = [[UIActivityIndicatorView alloc] 
       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
goingActivity.center = button.center; 
[button.superview insertSubview:goingActivity aboveSubview:button]; 
[goingActivity startAnimating]; 

if ([button.titleLabel.text isEqualToString:@"Go"]) { // Select 

    [PFObject saveAllInBackground:save block:^(BOOL success, NSError *error) { 

     //Do stuff 

     if (success) { 

      [self selectGoingButton:button]; // Toggle Button 
      selectedGoingToButton = button; 

      [defaults synchronize]; 
      [self reloadData]; // Refresh tableview 
     } 
    }]; 
} 
else { // Deselect 



     if (success) { 

      //Toggle button 
      selectedGoingToButton = nil; 
      [self deselectGoingButton:button]; 
      [self reloadData]; 
     } 
    }]; 
} 


} 

- (void)selectGoingButton:(UIButton *)button { 

NSLog(@"GoingButtonselected"); 
[button setTitle:@"Going" forState:UIControlStateNormal]; 
button.layer.borderColor = neonBlue.CGColor; 
[button setNeedsDisplay]; 
} 

這是處理問題的代碼。我刪除了代碼的肉,但所做的是更新解析上的用戶信息,並在磁盤上,因此它不相關。問題仍然是,當activityIndi​​cator正在運行時,按鈕標題不會消失(標題和微調器都可以看到),然後標題切換到「GOING」,然後微調器停止。

我想要這樣,當用戶點擊按鈕時,activityIndi​​cator啓動,覆蓋按鈕標題,然後在後臺活動停止時按鈕標題切換到「GOING」。我如何讓activityIndi​​catorView覆蓋標題圖層?

編輯:澄清,activityIndi​​cator被正確定位。它在按鈕的中間,可以在那裏看到很好。問題在於它沒有覆蓋標題爲「GO」的按鈕的頂層。當我添加activityIndi​​cator子視圖時,我希望標題消失,但這不會發生在我當前的代碼中。

+0

當活動指示器旋轉時,你不需要按鈕標題嗎? –

+0

正確@KathiravanG – mufc

回答

2

試試這個

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
CGFloat halfButtonHeight = btnLogin.bounds.size.height/2; 
CGFloat buttonWidth = btnLogin.bounds.size.width; 
indicator.center = CGPointMake(buttonWidth - halfButtonHeight , halfButtonHeight); 
[btnLogin addSubview:indicator]; 
[indicator startAnimating]; 
+0

問題不在指示器所在的位置。在我的代碼中,它顯示在按鈕的中心。我的問題是,它開始旋轉時沒有覆蓋按鈕標題「GO」。我可以看到指示器正在工作,但它不包括按鈕標題,我希望它覆蓋按鈕標題,以便標題無法看到。 – mufc

+1

根據按鈕邊界獲取Ttitle座標。然後製作title.hidden – Anton

0

不知道這是你想在這裏做什麼是最好的方式,但你可以使用你的代碼,如果你加上「selectGoingButton」 檢查測試標題等於'去'。如果是,請設置一個空標題 - @「」,如果它是@「」,則將其設置爲@「去」等。

0

我想通了。這實際上很簡單,實際上我只是在推翻它。

- (void)going:(UIButton *)button { 



goingActivity = [[UIActivityIndicatorView alloc] 
       initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
goingActivity.center = button.center; 
[button.superview insertSubview:goingActivity aboveSubview:button]; 


if ([button.titleLabel.text isEqualToString:@"Go"]) { // Select 

[button setTitle:@"" forState:UIControlStateNormal]; 
[goingActivity startAnimating]; 

    [PFObject saveAllInBackground:save block:^(BOOL success, NSError *error) { 

     //Do stuff 

     if (success) { 

      [self selectGoingButton:button]; // Toggle Button 
      selectedGoingToButton = button; 

      [defaults synchronize]; 
      [self reloadData]; // Refresh tableview 
     } 
    }]; 
} 
else { // Deselect 

[button setTitle:@"" forState:UIControlStateNormal]; 
[goingActivity startAnimating]; 


     if (success) { 

      //Toggle button 
      selectedGoingToButton = nil; 
      [self deselectGoingButton:button]; 
      [self reloadData]; 
     } 
    }]; 
} 


} 

我所做的只是當我在if-else時,將按鈕的標題更改爲無。然後在每種情況下啓動activityIndi​​cator。

相關問題