【问题标题】:Delegation method for calling other class调用其他类的委托方法
【发布时间】:2014-12-10 21:21:37
【问题描述】:

我阅读了不同的委托方法帖子,但仍然无法弄清楚问题所在。

我想从我的主 ViewController(称为 ma​​inVC)显示一个自定义弹出窗口(一个带有关联弹出视图控制器的 xib 文件,称为 popoverVC)。在我的弹出窗口中有一个按钮,用于在 mainVC 内的文本字段中显示文本。我的问题如下:

  • 我在 Xib 文件中的按钮调用的 popoverVC 中的方法“firstMethod:”正在运行(NSLog 消息“firstMethod Called!”显示在控制台上
  • 此方法“firstMethod”调用位于 mainVC 中的另一个方法“secondMethod”。此方法在 popoverVC 内的协议声明中定义,并且也在工作(在控制台上:NSLog 消息“secondMethod Called!”除了之前的“firstMethod Called”)。

我不明白的是,我在 ma​​inVC 的第二个方法中添加了一个代码行以在 textField 中显示文本(也在 ma​​inVC 中),这是不工作(虽然 NSLog 命令的前一行工作)。

我有一种感觉,因为我正在创建一个新的 ma​​inVC 实例来调用 popoverVC 中的第二个方法,所以我指的是一个与之前不同的文本字段出现在一开始就调用 popoverVCma​​inVC 中。而且由于 NSLog 仅显示在控制台中,因此我在不同的视图控制器上。

我担心我的解释不是很清楚......以防万一我在下面添加我的代码。

(我的 xib 文件(及其 popoverVC 类)在 mainVC 中使用 textfield beginEditing 调用)

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

ma​​inVC.h:

@interface mainVC : UIViewController <mainVCDelegate>

ma​​inVC.m:

- (void)insertValue {

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

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

}

【问题讨论】:

  • 登录 self.textfield 看看它说了什么。
  • 是(null)...不知道怎么访问...

标签: ios class methods action delegation


【解决方案1】:

您似乎遗漏了有关委派的重要部分。我建议阅读Apple's guide 的委托。但与此同时,这就是您所缺少的。

委托允许 popoverVC 不知道 mainVC。您正在创建一个 mainVC 实例并直接调用它的方法 insertValue,这不是委托。

在popoverVC.h中

@protocol mainVCDelegate <NSObject>

@optional
- (void)insertValue;
@end

@interface popoverVC : UIViewController

@property (weak) id <mainVCDelegate> delegate;

@end

在popoverVC.m中

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

在 mainVC.h 中

@interface mainVC : UIViewController <mainVCDelegate>

在 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";
}

【讨论】:

  • 编辑后的回答非常棒。可能应该在编辑之前删除这些东西。
  • 感谢您的帮助,我会阅读此内容。但是您的解决方案似乎不起作用。在 popoverVC 中不使用 [delegate insertValue] 调用 insertValue 方法。尽管我把 popover.delegate = self 放在 mainVC 的 didLoad 方法中......没有更多的第二个 NSLog 语句:-(
  • 我试图通过 [self.delegate insertValue] 更改 [delegate insertValue] 但它也不起作用......
  • @Trichophyton - 答案对我来说是正确的。关于调试的一个众所周知的技术是你几乎可以 NSLog 任何东西。所以我会,在弹出框调用 [self.delegate insertValue] 的地方,NSLog 委托是这样的: NSLog(@"%@",self.delegate);它应该记录一个 mainVC 实例。
  • @danh - 我在 [self.delegate insertValue] 之后做了 NSLog,我注意到消息没有发送,即 [self.delegate respondsToSelector:@selector(insertValue)] 应该返回 No。我接受了您的建议来测试 self.delegate 并返回(null),没有创建 mainVC 的实例。我不知道为什么...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-11-02
  • 1970-01-01
  • 2015-04-22
  • 2018-07-23
  • 2011-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多