Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call Delegate method as a selector in same delegate class

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.

like image 536
AFTAB MUHAMMED KHAN Avatar asked Nov 08 '16 18:11

AFTAB MUHAMMED KHAN


People also ask

Can a class have multiple delegates?

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.

How do you call a delegate method in Objective C?

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.

What is invoking a delegate?

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.

How to call delegate in c#?

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.


2 Answers

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;
like image 124
Nayab Khan Avatar answered Sep 30 '22 05:09

Nayab Khan


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.

like image 40
rmaddy Avatar answered Sep 30 '22 06:09

rmaddy