且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

执行代码块代替@selector

更新时间:2023-11-30 13:46:28

进一步pgb的注释,编写这样的内容可以解决问题:

Further to pgb's comment, writing something like this would solve the problem:

@interface PJBlockHolder

+ (id)blockHolderWithBlock:(dispatch_block_t)block;
- (void)invoke;

@end

/* obvious implementation; copy the block, issue it upon invoke */

并且:

[[UIBarButtonItem alloc] initWithTitle: @"Edit"
    style: self.navigationController.navigationItem.leftBarButtonItem.style
    target: [PJBlockHolderWithBlock:^{ /* your code here */ }]
    action:@selector(invoke) ];

因此,您已经创建了一个自定义对象,该对象包装了一个块并将其发布到特定的选择器上.

So you've created a custom object that wraps a block and issues it upon a particular selector.

如下所述,UIControl不保留其目标.因此,最简单的方法可能是将块保持器的寿命与控件的寿命联系起来.这不一定是理想的,因为如果您随后在保持控件有效的情况下随后将其作为目标删除,那么持有人将无法发挥其作用,但是它可能适用于大多数情况.

as noted below, UIControls don't retain their targets. So probably the easiest thing is to tie the lifetime of the block holder to the lifetime of the control; that's not necessarily ideal because then the holder will outlive its usefulness if you subsequently remove it as a target while keeping the control alive, but it's probably suitable for the majority of cases.

选项要么是使用Objective-C的内置关联对象,要么是使用UIControl继承自UIView的事实,从而赋予它CALayer,它可以存储任意键对象.

Options are either to use Objective-C's built in associated objects, or to use the fact that UIControl inherits from UIView, giving it a CALayer, which can store arbitrary keyed objects.

Justin Spahr-Summers在下面的评论中链接到前者的一个有据可查的,公共领域的实现,因此出于讨论的目的,我将展示后者的一个示例,即使它很笨拙.

Justin Spahr-Summers links to a well documented, public domain implementation of the former in his comment below so I'll show an example of the latter, even though it's hacky, for the purposes of discussion.

PJBlockHolderWithBlock *blockHolder = [PJBlockHolderWithBlock:^{ /* your code here */ }];
UIBarButtonItem *barButtonItem =
    [[UIBarButtonItem alloc] initWithTitle: @"Edit"
        style: self.navigationController.navigationItem.leftBarButtonItem.style
        target: blockHolder
        action:@selector(invoke) ];
[barButtonItem.layer setValue:blockHolder forKey:@"__myBlockHolderKey__"];