iOS改变了动态UIActionsheet按钮的颜色

时间:2014-04-25 11:43:36

标签: ios uiactionsheet uicolor

我制作了动态uiactionsheet,如:

- (IBAction)selectCity
{
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:kCityActionSheetTitle
                                                             delegate:self
                                                    cancelButtonTitle:nil
                                               destructiveButtonTitle:nil
                                                    otherButtonTitles:nil];
    for (NSString * cityNameNSS in [_cityNamesAndUrlsNSMD allKeys]) {
        [actionSheet addButtonWithTitle:cityNameNSS];
    }
    [actionSheet addButtonWithTitle:kCancellButtonTitle];
    actionSheet.cancelButtonIndex = [[_cityNamesAndUrlsNSMD allKeys] count];
    actionSheet.tag = 2;
    [actionSheet showInView:[UIApplication sharedApplication].keyWindow];

}

我想更改uiactionsheet按钮的色调。因此我写道:

- (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
    for (UIView *subview in actionSheet.subviews) {

        if ([subview isKindOfClass:[UIButton class]]) {
            UIButton *button = (UIButton *)subview;
            [button setTitleColor:[UIColor colorWithRed:0.0/255.0 green:150.0/255.0 blue:94.0/255.0 alpha:1.0]
                         forState:UIControlStateNormal];
        }
        else if([subview isKindOfClass:[UILabel class]]){
            UILabel *label = (UILabel *)subview;
            [label setTextColor:[UIColor colorWithRed:0.0/255.0 green:150.0/255.0 blue:94.0/255.0 alpha:1.0]];

        }
    }
}

但是,仅在这种情况下(在动态uiactionsheet中),按钮颜色不会改变。我在同一个控制器中有其他uiactionsheet,他们正在改变颜色。

什么是可能的问题/解决方案?

3 个答案:

答案 0 :(得分:1)

请在按钮上添加标签

 - (void)willPresentActionSheet:(UIActionSheet *)actionSheet
{
   for (UIView *subview in actionSheet.subviews)
   {
     if ([subview isKindOfClass:[UIButton class]])
     {
       if (subview.tag == 3)
        {
         NSLog(@"ButtonIndex===%d",subview.tag);
        UIButton *button = (UIButton *)subview;
        [button setTitleColor:[UIColor colorWithRed:0.0/255.0 green:150.0/255.0 blue:94.0/255.0 alpha:1.0]
                     forState:UIControlStateNormal];


        }
      }
  }
}

答案 1 :(得分:1)

SEL selector = NSSelectorFromString(@"_alertController");
if ([actionSheet respondsToSelector:selector])
{

    UIAlertController *alertController = [actionSheet valueForKey:@"_alertController"];


    if ([alertController isKindOfClass:[UIAlertController class]])
    {
        alertController.view.tintColor = [UIColor redColor];
    }
}
else
{
    // use other methods for iOS 7 or older.
}

答案 2 :(得分:0)

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

Actionsheet代表中,只需编写此代码。

如果您想了解更多信息,请点击以下链接:https://github.com/ianb821/IBActionSheet

相关问题