darius
darius

Reputation: 3

Why is my delegate method being sent to its parent view after being sent to the delegate?

I apologise ahead of time for the question title. I'm not quite sure how to succinctly describe my issue.

I'm having trouble with this delegate method I set up. I'm trying present a simple login screen modally. My issue is my login view gets a method called to it after I dismiss it.

Specifically, Xcode logs this:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LoginViewController login:]: unrecognized selector sent to instance [memory address]'

LoginViewController.h

#import <UIKit/UIKit.h>

@protocol LoginViewControllerProtocol;

@interface LoginViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *userPasswordTextField;
@property (weak, nonatomic) IBOutlet UIButton *loginButton;
@property (nonatomic, weak) NSObject<LoginViewControllerProtocol> *loginViewControllerDelegate;

@end


@protocol LoginViewControllerProtocol <NSObject>
- (void)loginViewController:(LoginViewController *)controller didLogin:(NSString *)userName;
@end

LoginViewController.m

- (IBAction)login
{
   if ([self.loginViewControllerDelegate respondsToSelector:@selector(loginViewController:didLogin:)]) {
        [self.loginViewControllerDelegate loginViewController:self didLogin:self.userNameTextField.text];              
    }
}

The delegate: (TestViewController.m)

- (void)loginViewController:(LoginViewController *)controller didLogin:(NSString *)userName
{
    [self dismissModalViewControllerAnimated: YES]; 
    didLogIn = YES;

}

I can't see why the delegate method is being sent back to the dismissed view.

Any help would be greatly appreciated!

Edit: I apologise for leaving this out. This occurs in the view controller that calls LoginViewController. The delegate method also exists here.

TestViewController.m

if (!didLogIn) {
        //launch login view modally
        LoginViewController* loginController = [[LoginViewController alloc] initWithNibName: @"LoginViewController" bundle: nil]; 
        loginController.loginViewControllerDelegate = self;
        loginController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
        loginController.modalPresentationStyle = UIModalPresentationFormSheet;

        NSLog(@"LoginViewControllerDelegate: %@", self.description);
        NSLog(@"LoginViewController: %@", loginController.description);
        [self presentModalViewController:loginController animated: YES];

TestViewController.h

#import <UIKit/UIKit.h>
#import "LoginViewController.h"

@class NuanceGuidHandler;

@interface TestViewController : UIViewController <LoginViewControllerProtocol>

@property (weak, nonatomic) IBOutlet UILabel *partnerGuidLabel;
@property (weak, nonatomic) IBOutlet UILabel *userGuidLabel;
@property (strong, nonatomic) NuanceGuidHandler *nuanceGuidHandler;
@property (nonatomic) BOOL didLogIn;
@end

Upvotes: 0

Views: 173

Answers (2)

jrturton
jrturton

Reputation: 119242

The delegate thing is a red herring.

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LoginViewController login:]: unrecognized selector sent to instance [memory address]'

Means a object of class LoginViewController has been sent the message login:, which it doesn't know how to handle.

You've connected the action to a method called login:, and in your controller your action is defined as login. The colon is all-important, as any doctor will tell you. Either disconnect and reconnect the action in interface builder, or change the login method signature from

- (IBAction)login

To

- (IBAction)login:(id)sender

Upvotes: 1

Matt Meyers
Matt Meyers

Reputation: 143

You seem to be doing one of two things:

  1. You don't seem to be declaring your delegate anywhere when you mush your modal view controller.

    modalviewcontroller.delegate = self

  2. I'm not sure if this is causing an issue or not but you have a double protocol declaration in LoginViewController.h. This has messed with my delegate stuff in the past, and it may or may not be causing your problem.

Upvotes: 0

Related Questions