设置文本字段文本不起作用
Setting textfield text does not work
初学者程序员试图理解为什么我无法完成这个简单的任务。我知道这很可能是一个非常简单的解决方案,但希望有人能解释原因。
我有一个屏幕,用户可以在其中将电子邮件输入文本字段。这个想法是,如果在输入电子邮件时,如果它不在存储的电子邮件中,它将提示用户将新的 email/contact 输入商店。由于我在弹出联系人创建屏幕之前检查电子邮件是否有效,我想将输入的文本直接放入新联系人创建页面的 "Email" 字段中,不允许编辑.我尝试了多种方法,但无法让电子邮件显示在文本字段中。有人可以解释这是为什么吗?
来自初始 VC 用户输入电子邮件的代码。如果商店中不存在该电子邮件,则会触发创建联系人创建者页面的代码:
//I created this custom initializer since setting the text field (as I did below) would not work
ContactCreationViewController *contactCreationVC = [[ContactCreationViewController alloc] initWithEmail:trimmedText];
contactCreationVC.delegate = self;
//initially I tried setting the text here but it did not work, I now understand why
//[contactCreationVC.emailField setText:@"asdsadasd"];
[contactCreationVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[contactCreationVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:contactCreationVC animated:YES completion:nil];
这是实际 ContactCreator 的代码VC:
-(instancetype) initWithEmail:(NSString*)email{
self = [super init];
//tried setting email here which works as I check with breakpoints
[self setEmail:email];
//self.email = email here when I check
return self;
}
....
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
switch (indexPath.row) {
....
case 3: {
ContactCreationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactCreationCellIdentifier];
cell.titleLabel.text = @"E-Mail";
cell.userEntryTextfield.tag = 3;
cell.userEntryTextfield.delegate = self;
cell.userEntryTextfield.keyboardType = UIKeyboardTypeEmailAddress;
//I try setting the email here but self.email = nil (don't understand why)
cell.userEntryTextfield.text = [NSString stringWithFormat:@"%@", self.email];
cell.userEntryTextfield.userInteractionEnabled = NO;
cell.userEntryTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.emailField = cell.userEntryTextfield;
return cell;
}
.....
}
我什至觉得问这样一个简单的问题很愚蠢,但我显然不了解幕后发生的事情,因为我已经尝试了一切。任何人都可以从最佳实践的角度解释发生了什么并提出理想的解决方案吗?
编辑:我想我的问题可以更简洁......它基本上归结为:为什么当我在 init 中设置 self.email 时,当我在cellForRow 方法(变为 nil)?
(文本字段位于表格视图的单元格中)
问题出在这一行:
[contactCreationVC.emailField setText:@"asdsadasd"];
您刚刚创建了 contactCreationVC
。它的视图尚未加载,所以 emailField
是 nil
(通过一些基本的日志记录很容易证明)。发给 nil
的消息不执行任何操作。
正确的做法是:永远不要接触另一个视图控制器的插座。设置另一个视图控制器的属性,让它处理它自己的插座。在这种情况下,另一个视图控制器需要使用 属性 在其 viewDidLoad
.
中设置 emailField
text
至于为什么 那个 方法对你不起作用,据我所知,没有足够的信息来回答它。如果你能证明事情是按这个顺序进行的:
使用实际电子邮件值调用 ContactCreatorVC init
,并设置 属性。
属性 具有强大的(保留)策略,因此保留该值。
同一个 ContactCreatorVC 实例现在调用了 cellForRowAtIndexPath
,那一刻 属性 是 nil
。
如果你能证明这一切,那么唯一合乎逻辑的结论是同时出现了一些其他代码并将 属性 设置为 nil
。
初学者程序员试图理解为什么我无法完成这个简单的任务。我知道这很可能是一个非常简单的解决方案,但希望有人能解释原因。
我有一个屏幕,用户可以在其中将电子邮件输入文本字段。这个想法是,如果在输入电子邮件时,如果它不在存储的电子邮件中,它将提示用户将新的 email/contact 输入商店。由于我在弹出联系人创建屏幕之前检查电子邮件是否有效,我想将输入的文本直接放入新联系人创建页面的 "Email" 字段中,不允许编辑.我尝试了多种方法,但无法让电子邮件显示在文本字段中。有人可以解释这是为什么吗?
来自初始 VC 用户输入电子邮件的代码。如果商店中不存在该电子邮件,则会触发创建联系人创建者页面的代码:
//I created this custom initializer since setting the text field (as I did below) would not work
ContactCreationViewController *contactCreationVC = [[ContactCreationViewController alloc] initWithEmail:trimmedText];
contactCreationVC.delegate = self;
//initially I tried setting the text here but it did not work, I now understand why
//[contactCreationVC.emailField setText:@"asdsadasd"];
[contactCreationVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[contactCreationVC setModalPresentationStyle:UIModalPresentationOverCurrentContext];
[self presentViewController:contactCreationVC animated:YES completion:nil];
这是实际 ContactCreator 的代码VC:
-(instancetype) initWithEmail:(NSString*)email{
self = [super init];
//tried setting email here which works as I check with breakpoints
[self setEmail:email];
//self.email = email here when I check
return self;
}
....
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
switch (indexPath.row) {
....
case 3: {
ContactCreationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:contactCreationCellIdentifier];
cell.titleLabel.text = @"E-Mail";
cell.userEntryTextfield.tag = 3;
cell.userEntryTextfield.delegate = self;
cell.userEntryTextfield.keyboardType = UIKeyboardTypeEmailAddress;
//I try setting the email here but self.email = nil (don't understand why)
cell.userEntryTextfield.text = [NSString stringWithFormat:@"%@", self.email];
cell.userEntryTextfield.userInteractionEnabled = NO;
cell.userEntryTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
self.emailField = cell.userEntryTextfield;
return cell;
}
.....
}
我什至觉得问这样一个简单的问题很愚蠢,但我显然不了解幕后发生的事情,因为我已经尝试了一切。任何人都可以从最佳实践的角度解释发生了什么并提出理想的解决方案吗?
编辑:我想我的问题可以更简洁......它基本上归结为:为什么当我在 init 中设置 self.email 时,当我在cellForRow 方法(变为 nil)?
(文本字段位于表格视图的单元格中)
问题出在这一行:
[contactCreationVC.emailField setText:@"asdsadasd"];
您刚刚创建了 contactCreationVC
。它的视图尚未加载,所以 emailField
是 nil
(通过一些基本的日志记录很容易证明)。发给 nil
的消息不执行任何操作。
正确的做法是:永远不要接触另一个视图控制器的插座。设置另一个视图控制器的属性,让它处理它自己的插座。在这种情况下,另一个视图控制器需要使用 属性 在其 viewDidLoad
.
emailField
text
至于为什么 那个 方法对你不起作用,据我所知,没有足够的信息来回答它。如果你能证明事情是按这个顺序进行的:
使用实际电子邮件值调用 ContactCreatorVC
init
,并设置 属性。属性 具有强大的(保留)策略,因此保留该值。
同一个 ContactCreatorVC 实例现在调用了
cellForRowAtIndexPath
,那一刻 属性 是nil
。
如果你能证明这一切,那么唯一合乎逻辑的结论是同时出现了一些其他代码并将 属性 设置为 nil
。