I have my custom delegate class for UINavigationController
.
Currently i am facing issue in my UIBarButtonItems
.
i have to define @Selector
for each 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.
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.
- (void)arrowMenuSelectedOut
{
NSLog(@"button press");
}
i want to use this arrowMenuSelectedOut method as selector from same delegate class on button press.
The delegate pattern is about a 1 to 1 relationship between a class and it's delegate. Whilst it is possible to achieve some level of multiple delegation through switching the delegates in and out, it's more likely to lead to unpredictable behaviour and bugs.
To create one, you define a class that implements the delegate methods you're interested in, and mark that class as implementing the delegate protocol. Then you could create an instance of MyClass and assign it as the web view's delegate: MyClass *instanceOfMyClass = [[MyClass alloc] init]; myWebView.
Calling the method referenced by a delegate is called invoking the delegate. We can do this with the Invoke method: 1var result = add. Invoke(2, 3); csharp.
Delegates can be invoke like a normal function or Invoke() method. Multiple methods can be assigned to the delegate using "+" or "+=" operator and removed using "-" or "-=" operator. It is called multicast delegate. If a multicast delegate returns a value then it returns the value from the last assigned target method.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With