Search code examples
iosclassmethodsactiondelegation

Delegation method for calling other class


I read different delegation method posts but still I cannot figure out what is wrong.

I would like to show a custom popup (a xib file with its associated popover view controller called popoverVC) from my main ViewController (called mainVC). Inside my popup there is a button in order to display text in a textfield inside mainVC. My problem is the following:

  • the method "firstMethod:" in popoverVC called by my button in Xib file is working (NSLog message "firstMethod Called!" displayed on the console)
  • this method "firstMethod" calls an other method "secondMethod" located in mainVC. This method is defined in the protocol declaration inside popoverVC and is also working (on the console: NSLog message "secondMethod Called!" in addition to the previous "firstMethod Called").

What I don't understand is that I added a code line in the second method in mainVC to display text in a textField (also in mainVC) and this is not working (though the previous line with the NSLog command is working).

I have the feeling that since I am creating a new instance of mainVC to call the second method in popoverVC I am referring to a textfield that is different from the one present in the mainVC calling the popoverVC at the very beginning. And since NSLog only displays in the console I am on a different viewcontroller.

I fear my explanation is not very clear... Just in case I add my code below.

(My xib file (with its popoverVC class) is called with textfield beginsEditing in mainVC)

popoverVC.h:

@protocol mainVCDelegate <NSObject>

@optional
- (void)insertValue;
@end

#import...

popoverVC.m:

//method called by the button in Xib file and supposed to call the method in mainVC to then display text inside a textfield in mainVC

- (IBAction)updateTextFieldInMainVC:(id)sender {

    NSLog(@"firstMethod called!");

    mainVC *mvc = [[mainVC alloc] init];
    [mvc insertValue];
    }

mainVC.h:

@interface mainVC : UIViewController <mainVCDelegate>

mainVC.m:

- (void)insertValue {

 NSLog(@"secondMethod called!"); ---> this is displayed on the console

self.textfield.text = @"sometext"; ---> this is not displayed in the textfield

}

Solution

  • It looks like you're missing the important part about delegation. I would recommend reading Apple's guide on delegation. But in the meantime, here is what you are missing.

    Delegation allows popoverVC to be unaware of mainVC. You are creating an instance of mainVC and calling it's method insertValue directly, which is not delegation.

    In popoverVC.h

    @protocol mainVCDelegate <NSObject>
    
    @optional
    - (void)insertValue;
    @end
    
    @interface popoverVC : UIViewController
    
    @property (weak) id <mainVCDelegate> delegate;
    
    @end
    

    In popoverVC.m

    - (IBAction)updateTextFieldInMainVC:(id)sender {
        NSLog(@"firstMethod called!");
        if ([delegate respondsToSelector:@selector(insertValue)]) {
            [delegate insertValue];
        }
    }
    

    In mainVC.h

    @interface mainVC : UIViewController <mainVCDelegate>
    

    In mainVC.m

    // In init/viewDidLoad/etc.
    popoverVC *popover = [[popoverVC alloc] init];
    popover.delegate = self;  // Set mainVC as the delegate
    
    - (void)insertValue {
        NSLog(@"secondMethod called!"); 
        self.textfield.text = @"sometext";
    }