0

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
4

2 回答 2

1

代表的事情是红鲱鱼。

*由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[LoginViewController login:]: unrecognized selector sent to instance [memory address]”

表示类的对象LoginViewController已发送消息login:,它不知道如何处理。

您已将该操作连接到一个名为 的方法login:,并且在您的控制器中您的操作定义为login。正如任何医生都会告诉你的那样,结肠非常重要。在界面生成器中断开并重新连接操作,或者将登录方法签名从

- (IBAction)login

- (IBAction)login:(id)sender
于 2012-07-20T13:23:23.730 回答
0

您似乎正在做以下两件事之一:

  1. 当您混合模态视图控制器时,您似乎没有在任何地方声明您的委托。

    modalviewcontroller.delegate = self

  2. 我不确定这是否会导致问题,但您在 LoginViewController.h 中有双重协议声明。这在过去弄乱了我的代表的东西,它可能会也可能不会导致您的问题。

于 2012-07-19T22:02:38.330 回答