【问题标题】:Why is my delegate method being sent to its parent view after being sent to the delegate?为什么我的委托方法在被发送到委托后被发送到其父视图?
【发布时间】:2012-07-19 21:51:33
【问题描述】:

我提前为问题标题道歉。我不太清楚如何简洁地描述我的问题。

我设置的这个委托方法有问题。我正在尝试以模态方式呈现一个简单的登录屏幕。我的问题是我的登录视图在我关闭它后得到了一个调用它的方法。

具体来说,Xcode 会记录这一点:

*** 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];              
    }
}

委托:(TestViewController.m)

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

}

我不明白为什么将委托方法发送回已关闭的视图。

任何帮助将不胜感激!

编辑: 我很抱歉遗漏了这个。这发生在调用 LoginViewController 的视图控制器中。委托方法也存在于此。

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

【问题讨论】:

  • 你在哪里以及如何设置你的委托?
  • 您没有显示委托值的设置位置。此外,在操作方法中间关闭 viewController 是一个非常糟糕的主意。为什么不使用调度到主队列并在那里执行(这样登录控制器将处于静止状态。)
  • 委托方法未发送回已关闭的视图。 login: 方法是。通常 IBAction 方法在头文件中声明,例如:-(IBAction)login:(id)sender。您是如何将您的 loginButton 连接到 login: 方法的?还有什么叫login:吗?
  • 在发送委托的方法中,我创建了一个新的 LoginViewController 实例,将它连接到 nib,并将 te 委托设置为 self。我使用界面生成器中的 ctrl-drag 选项连接按钮。没有其他东西调用这个方法。我会尝试重新连接笔尖中的所有内容。

标签: objective-c ios delegates


【解决方案1】:

代表的事情是红鲱鱼。

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

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

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

- (IBAction)login

- (IBAction)login:(id)sender

【讨论】:

  • 嗯,就是这样。我检查了我的笔尖,两者都存在。我删除了不应该的那个并且它有效。谢谢!
【解决方案2】:

你似乎在做两件事之一:

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

    modalviewcontroller.delegate = self

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

【讨论】:

  • “双重”协议声明不是问题。恰恰相反。它告诉编译器委托实际上是一个有效类型,稍后会定义。
  • 在开始尝试回答问题之前,您可能想了解更多 Obj-C - 2 被称为 forward declaration 并且在使用中很常见 - 事实上,对于这段代码,它是必要的,因为他的协议是在声明委托之后创建的。
  • 我很抱歉,我忘了包括我设置它的方法。我会用这些信息更新我的问题。
猜你喜欢
  • 2018-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多