[英]How to call Delegate method as a selector in same delegate class
I have my custom delegate class for UINavigationController
. 我有
UINavigationController
自定义委托类。
Currently i am facing issue in my UIBarButtonItems
. 目前我在
UIBarButtonItems
面临问题。
i have to define @Selector
for each BarItem. 我必须定义
@Selector
每个BarItem。
So Far i define selector for each bar item like this: 所以我为每个条形项定义选择器,如下所示:
SEL selector = (menu == MyMenu) ? @selector(leftSelected:) : @selector(rightSelected:);
the are working perfect because they are local method in my delegate class so there is no problem. 这是完美的,因为它们是我的委托类中的本地方法,所以没有问题。
but one point i need to declare my delegate method as @Selector because its define in other viewcontroller. 但有一点我需要将我的委托方法声明为@Selector,因为它在其他viewcontroller中定义。
here i have to define my delegate method as selector. 在这里,我必须将我的委托方法定义为选择器。
SEL selector = (menu == MenuLeft) ? @selector(leftMenuSelected:) : @selector(righttMenuSelected:);
if (menu == MenuTwo) {
selector = @selector(arrowMenuSelectedOut);
}
if ( menu == MenuTwo ){
UIImage *image = [UIImage imageNamed:MENU_ARROW];
UIBarButtonItem *btn= [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:selector];
btn.imageInsets = UIEdgeInsetsMake(0, 0, 0, -25.0);
return btn;
}
else
{
UIImage *image = [UIImage imageNamed:MENU_IMAGE];
return [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:selector];
}
this is my delegate method. 这是我的委托方法。
@protocol myNavigationViewControllerDeleget<NSObject>
- (void)arrowMenuSelectedOut;
@end
which is placed in other viewcontroller. 放在其他viewcontroller中。
- (void)arrowMenuSelectedOut
{
NSLog(@"button press");
}
i want to use this arrowMenuSelectedOut method as selector from same delegate class on button press. 我想在按下按钮时使用此arrowMenuSelectedOut方法作为同一代理类的选择器。
if you like to call delegate try it like this. 如果你想打电话给委托试试这样的话。
[self.mydelegate arrowMenuSelectedOut];
or in you case 或者在你的情况下
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self.mydelegate action:@selector(arrowMenuSelectedOut)];
also check did you define you delegate properly it should not be nil. 还检查你是否正确定义了你的委托它不应该是零。 try this
试试这个
self.mydelegate=self;
Based on the errors you put in the comments (they should be in the question), the problem is you are passing the wrong target
when creating the button. 根据您在注释中输入的错误(它们应该在问题中),问题是您在创建按钮时传递了错误的
target
。
Change: 更改:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self action:selector];
to: 至:
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStyleDone target:self.mydelegate action:selector];
The only thing that is changed is the target
parameter value. 唯一改变的是
target
参数值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.