【发布时间】:2014-12-10 21:21:37
【问题描述】:
我阅读了不同的委托方法帖子,但仍然无法弄清楚问题所在。
我想从我的主 ViewController(称为 mainVC)显示一个自定义弹出窗口(一个带有关联弹出视图控制器的 xib 文件,称为 popoverVC)。在我的弹出窗口中有一个按钮,用于在 mainVC 内的文本字段中显示文本。我的问题如下:
- 我在 Xib 文件中的按钮调用的 popoverVC 中的方法“firstMethod:”正在运行(NSLog 消息“firstMethod Called!”显示在控制台上)
- 此方法“firstMethod”调用位于 mainVC 中的另一个方法“secondMethod”。此方法在 popoverVC 内的协议声明中定义,并且也在工作(在控制台上:NSLog 消息“secondMethod Called!”除了之前的“firstMethod Called”)。
我不明白的是,我在 mainVC 的第二个方法中添加了一个代码行以在 textField 中显示文本(也在 mainVC 中),这是不工作(虽然 NSLog 命令的前一行工作)。
我有一种感觉,因为我正在创建一个新的 mainVC 实例来调用 popoverVC 中的第二个方法,所以我指的是一个与之前不同的文本字段出现在一开始就调用 popoverVC 的 mainVC 中。而且由于 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];
}
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
}
【问题讨论】:
-
登录
self.textfield看看它说了什么。 -
是(null)...不知道怎么访问...
标签: ios class methods action delegation