【问题标题】:UIActionSheetdelegate not getting subviewsUIActionSheetdelegate 没有获得子视图
【发布时间】:2014-12-31 04:38:13
【问题描述】:

在我的 iOS 8 应用程序中,我使用的是 UIActionSheet。我尝试在willPresentActionSheet委托方法中更改按钮标题的颜色,但它没有将按钮识别为其子视图。

我这样构造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。为什么?

【问题讨论】:

标签: ios objective-c uiactionsheet uiactionsheetdelegate


【解决方案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];

我试过了。它正在工作......

【讨论】:

    【解决方案2】:

    您尝试检查子视图数量的委托方法:willPresentActionSheet 似乎不合适。

    willPresentActionSheet 在实际呈现操作表之前调用。

    如果你在didlPresentActionSheet中查看子视图的数量,那会更合适。

    希望这会有所帮助..

    【讨论】:

    • 在 iOS 8 中,UIACtionSheet 的行为类似于 UIActivityViewController
    • 您没有阅读问题吗?我在第一行提到了“在我的 iOS 8 应用程序中”。我只是想知道是否有其他方法可以做到。
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-14
    • 1970-01-01
    相关资源
    最近更新 更多