- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我的代码。
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/
这是我的代码。 UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRe
它们似乎执行相当相似的任务:启动一个新线程来快速轻松地执行该选择器。但有什么区别吗?也许与内存管理有关? 最佳答案 两者是相同的。 在 iOS 和 Mac OS X v10.5 及更高版本中,所有对象
通过使用 Xcode 5 并启用自动引用计数。 在基于非 NavigationController 的应用程序中,我使用了一个 UIButton,并且正确定义和连接了 IBAction 和 IBOut
我刚刚在 Three20 中看到一些代码,如下所示: SEL sel = @selector(textField:didAddCellAtIndex:); if ([self.delegate
所以我想做的是我有一个 NSMutableArray 数据需要传递给另一个 UITableViewController。这个 NSMutableArray 是一个 NSDictionaries 数组,
我想做performSelector:withObject:,但对象是CGFloat。所以它实际上不是一个对象。我怎样才能做到这一点? 我正在执行选择器的对象不是我的,我无法修改它。 例如 [xyz
我有一个按钮,如果密码正确,我想在触发 segue 之前使用密码实现。当您输入错误的密码时,一切看起来都很好,我已经实现了另一个alertView来告诉用户密码是错误的。 当警报 View 弹出并在延
如题。调用 performSelector:withObject:afterDelay 后需要调用 NSAutoReleasePool 吗? 我觉得performSelector:withObject
我有一个 View Controller ,它调用performSelector:withObject:afterDelay。但是,如果我在调用此方法后立即删除该 View Controller ,那
我正在尝试使用这段代码在 NSMutableArray 中分配对象 - (IBAction)variablePressed:(UIButton *)sender { NSString *variabl
我正在尝试使用这段代码在 NSMutableArray 中分配对象 - (IBAction)variablePressed:(UIButton *)sender { NSString *variabl
我有一个可变数组,最初我在使用范围时遇到了问题。我得到了一些关于如何解决这个问题的建议(这很有效),但现在数组不会替换给定索引处的对象。 这是代码: .h 文件: @interface myViewC
我已经设置了一个块以在不同的队列上运行,并在延迟后调用另一个方法: piemanQ = dispatch_queue_create(PIEMAN_QUEUE_NAME, NULL); dispatch
我不理解代码中的某些行为。我在 .m 文件的顶部定义了一个枚举: typedef NS_ENUM (NSInteger, connectionState) { tryingToConnect, con
我的 iOS 应用程序中有一个异步服务器请求: [self performSelectorInBackground:@selector(doSomething) withObject:nil]; 如何
我想在后台线程中运行一个方法,第一个方法将在几秒钟后在同一(后台)线程上运行另一个方法。我写了这个: - (IBAction)lauch:(id)sender { [self performS
这是一个已经在 SO 中回答的问题,但我无法在任何地方的 Apple 文档中找到它。你能给我指出正确的方向吗? 在以下主题中 Do I have to retain an object before
我在释放一个对象时遇到了 EXC_BAD_ACCESS,该对象具有很少的 performSelector:withObject:afterDelay,其中选择器方法正在调用另一个对象,而我正在释放该对
这个问题在这里已经有了答案: dispatch_after - GCD in Swift? (26 个回答) 关闭8年前。 我有一个 Objective-C 中的应用程序,我正在过渡到 Swift。在
我的问题是,在我的应用程序中的某个时刻,我调用 [performSelector: withObject: afterDelay:] 但在长达 1 秒的延迟期间,会发生一些事情来决定是否延迟方法应该或
我是一名优秀的程序员,十分优秀!