尝试传递CGrect到performSelector withObject [英] Trying to pass CGrect to performSelector withObject

查看:305
本文介绍了尝试传递CGrect到performSelector withObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递一个CGRect:

I'm trying to pass a CGRect:

SEL frameSel = NSSelectorFromString(@"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];

但这不编译

也试过:

SEL frameSel = NSSelectorFromString(@"setFrame:");
CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];

实际上,这是编译,但是当我调试时,框架未正确设置:

Actually, this does compile but when I debug, the frame is not setted correctly:

po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>

但应该是frame =(10 10; 200 100)

But it should be frame = (10 10; 200 100)

我如何解决这个问题?

提前感谢!

推荐答案


CGRect rect = CGRectMake(10, 10, 200, 100);
[object performSelector:frameSel withObject:rect ];

但这不会编译

正确,因为 rect 是一个结构体,而不是指向对象的指针。

Correct, because rect is a structure, not a pointer to an object.


CGRect rect = CGRectMake(10, 10, 200, 100);
NSValue * value = [NSValue valueWithCGRect:rect];
[object performSelector:frameSel withObject:value ];

实际上,这是编译,但是当我调试时,框架未正确设置:

Actually, this does compile but when I debug, the frame is not setted correctly:

po object
<UILabel: 0x39220f0; frame = (0 0; 200 100); text = 'reflectionLabel'; clipsToBounds = YES; userInteractionEnabled = NO; layer = <CALayer: 0x3922240>>


好吧,这不是垃圾,所以它似乎工作,即使原点不知何故保持为零。

Well, it's not garbage, so it would seem that it worked, even though the origin somehow remained zero. You may want to ask a separate question about that.

无论如何,你为什么要使用 performSelector:withObject:?你不是针对一个特定的线程,你不会把它延迟;为什么不简单地说 object.frame = rect

Anyway, why are you using performSelector:withObject:? You're not addressing it to a specific thread and you're not putting it on a delay; why not simply say object.frame = rect?

这篇关于尝试传递CGrect到performSelector withObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆