gpt4 book ai didi

ios - UIButton 执行选择器 :withObject:withObject: not making call

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:30:41 26 4
gpt4 key购买 nike

这是我的代码。

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(110, 270, 100, 40);
button.backgroundColor = [UIColor grayColor];
[button setTitle:@"next" forState:UIControlStateNormal];
[self.view addSubview:button];

我想通过这个方法设置按钮标题颜色但它不起作用!

    [button performSelector:@selector(setTitleColor:forState:)
withObject:[UIColor blackColor]
withObject:@(UIControlStateNormal)];

有人知道为什么它不起作用吗???


感谢大家的帮助。昨晚,我找到了一种让它起作用的方法。无论参数是 UIControlStateNormal(特例)还是其他类似 UIControlStateHighlighted 的。这是代码:

     [button performSelector:@selector(setTitleColor:forState:)
withObject:[UIColor blueColor]
withObject:(__bridge id)((void *)UIControlStateNormal)];
[button performSelector:@selector(setTitleColor:forState:)
withObject:[UIColor blackColor]
withObject:(__bridge id)((void *)UIControlStateHighlighted)];
[button performSelector:@selector(setTitleColor:forState:)
withObject:[UIColor redColor]
withObject:(__bridge id)((void *)UIControlStateSelected)];

那么问题就解决了。


我发现它很有用!所以我想用它做更多的事情,这是代码:

    [button.layer performSelector:@selector(setMasksToBounds:) withObject:(__bridge id)((void *)YES)];
[button.layer performSelector:@selector(setCornerRadius:) withObject:(__bridge id)((void *)5)];
[button.layer performSelector:@selector(setBorderWidth:) withObject:(__bridge id)((void *)5)];
[button.layer performSelector:@selector(setBorderColor:) withObject:(__bridge id)(__bridge CGColorRef)[UIColor greenColor]];

我测试的时候发现又不行了!!!太失望了:(。如果有人对这个问题感兴趣,你可以测试一下。

最佳答案

如果我理解你的意图是正确的,你要求让框架进行相应的调用,所以你期望

[button performSelector:@selector(setTitleColor:forState:)
withObject:[UIColor blackColor]
withObject:@(UIControlStateNormal)];

打电话

[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

您在这里遗漏了一件重要的事情:performSelector:withObject:withObject: 需要两个对象。您将纯整数值(因为 UIControlStateNormal 是一些 int/enum 值)包装在 NSNumber 中。现在该对象(或指针、地址,或任何您调用它的东西)作为第二个参数传递给 setTitleColor:forState:,这显然不是值本身,但因为该方法不知道this,它将值解释为某种整数值。

所以稍后会调用这样的东西:

NSNumber *number = ...; // your number object
[button setTitleColor:[UIColor blackColor] forState:number];

正如在另一个答案中所说,它可能通过省略此处的 @(...) 运算符来工作,因此该值不会包含在 NSNumber 中。这就是将要发生的事情:NSControlStateNormal 被定义为 0。所以编译器会将其解释为 nil,这是一个有效的对象,或者缺少对象。 nil 稍后将被重新解释为整数值作为 UIControlState,因此您的 0 (== UIControlStateNormal) 再次。

这是一个可行的特殊情况,因为 0 可能被视为 nil。它不会与任何其他值(即 UIControlStateFocused)一起工作(通常甚至不会编译)。你会得到一个“无法使用类型为‘(匿名枚举...)’的右值初始化类型为‘id’的参数”错误。

如果你坚持走 performSelector: 路线,你可以添加这样的方法:

-(void)setProperties:(NSDictionary *)props onButton:(UIButton *)button {
UIColor *color = props[@"color"];
NSNumber *stateWrapper = props[@"state"];
UIControlState state = (UIControlState)stateWrapper.unsignedIntegerValue;
[button setTitleColor:color forState:state];
}

并像这样使用它:

[self performSelector:@selector(setProperties:onButton:) 
withObject:@{@"color" : [UIColor blackColor],
@"state" : [NSNumber numberWithUnsignedInteger:UIControlStateNormal]}
withObject:button];

将值包装到 NSValue 中甚至可能更清晰,或者为您的参数使用专用类等。

但是,无论如何,我更喜欢 dispatch_* 方法,即

dispatch_sync(dispatch_get_main_queue(), ^{
[button setTitleColor:[UIColor blackColor] forState:number];
})

或使用 dispatch_async 或您当时的意图。

关于ios - UIButton 执行选择器 :withObject:withObject: not making call,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37962462/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com