2014-12-31 45 views
1

在我的iOS 8應用程序中,我使用UIActionSheet。我試圖在willPresentActionSheet委託方法中更改按鈕標題的顏色,但它不會將按鈕識別爲其子視圖。UIActionSheetdelegate沒有得到子視圖

我構造UIActionSheet這樣的:

UIActionSheet *popup = [[UIActionSheet alloc] initWithTitle:@"Select an Option" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles: nil]; 
popup.tag = 2; 
[popup addButtonWithTitle:@"Add Tag To Chat"]; 
[popup addButtonWithTitle:@"Remove Tag From Chat"]; 
[popup addButtonWithTitle:@"Terminate"]; 
[popup addButtonWithTitle:@"Cancel"]; 
[popup showInView:self.view]; 

代表是:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 
{ 
    NSLog(@"%d",[actionSheet.subviews count]); 
    for (UIView *subview in actionSheet.subviews) { 
     if ([subview isKindOfClass:[UIButton class]]) { 
      UIButton *button = (UIButton *)subview; 
      [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 
     } 
    } 
} 

按鈕被示出,我可以點擊和動作執行。但它說count = 0.爲什麼?

+0

操作表不像iOS 7中那樣可定製。嘗試任何開源替換方法,因爲您只是試圖破解自己的方式,可能會使其在更多操作系統版本中崩潰。 – Shubhank

+1

在iOS 8下,'UIActionSheet'是用新的'UIAlertController'實現的,它是完全不同的,不能以任何方式定製。您需要創建或使用支持所需內容的自定義實現。 – rmaddy

+0

[這是最好的(和可定製的)開源行動表之一](https://github.com/fastred/AHKActionSheet) – MendyK

回答

-2

您試圖檢查子視圖數的代理方法:willPresentActionSheet似乎不合適。

willPresentActionSheet在動作表實際得到呈現之前調用。

如果您檢查didlPresentActionSheet中的子視圖數,那將更合適。

希望這有助於..

+0

不,這是行不通的。 – Rashad

+0

您可能正在檢查iOS 8 .. –

+0

是的,爲什麼我不應該? – Rashad

1

嘗試這種方式,改變子視圖的UIColor

UIActionSheet * action = [[UIActionSheet alloc] 
           initWithTitle:@"Title" 
           delegate:self 
           cancelButtonTitle:@"Cancel" 
           destructiveButtonTitle:nil 
           otherButtonTitles:@"",nil]; 
[[[action valueForKey:@"_buttons"] objectAtIndex:0] setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 

我已經試過了。它正在工作...