gpt4 book ai didi

objective-c - 覆盖-performSelector : in UIBarButtonItem Subclass

转载 作者:可可西里 更新时间:2023-11-01 05:23:12 26 4
gpt4 key购买 nike

我正在尝试子类化 UIBarButtonItem 以添加一些特殊功能。我需要 barButtonItem 在触摸时切换其外观,因此我试图覆盖 performSelector:

当我使用下面的代码时,我得到一个 EXC_BAD_ACCESS (code=2 ...)

-(id)performSelector:(SEL)aSelector withObject:(id)object1 withObject:(id)object2
{
// Do something

return [super performSelector:aSelector withObject:object1 withObject:object2];
}

我的猜测是,我要么错误地尝试覆盖 performSelector:(还有其他方法吗?),要么错误地调用了 super 上的方法。

在搜索解决方案 3 个多小时后,我一无所获。非常感谢任何帮助。

更新:以下作品:

@implementation CustomBarButtonItem

- (void)setTarget:(id)target
{
_realTarget = target;
super.target = self;
}

- (void)setAction:(SEL)action
{
_realAction = action;
super.action = @selector(pressed);
}

- (void)pressed
{
[self doCustom]; // implement this somewhere
[_realTarget performSelector:_realAction withObject:nil afterDelay:0];
}

不幸的是,我想通过设置 self.customView = nilcustomView 和正常的 UIBarButtonItem 外观之间切换,这只是有时有效。但这是一个完全不同的问题。谢谢大家。

我再等一会再选一个最佳答案,看看有没有更好的方案。

最佳答案

使用简单的 UIView 实现此功能可能更好,您可以通过任何给定方式对其进行简单修改。

UIBarButtonItem 确实提供了一种基于自定义 UIView 进行初始化的方法:

- (id)initWithCustomView:(UIView *)customView

然后您可以告诉 UIView 根据 UIBarButtonItem 触发的触摸事件更改其外观。

关于objective-c - 覆盖-performSelector : in UIBarButtonItem Subclass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090738/

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