代码之家  ›  专栏  ›  技术社区  ›  Tom A

设置文本字段文本无效

  •  -1
  • Tom A  · 技术社区  · 8 年前

    我有一个屏幕,用户可以将电子邮件输入文本字段。其想法是,如果在输入电子邮件时,如果它尚未在存储的电子邮件中,它将提示用户将新的电子邮件/联系人输入到存储中。由于我在弹出联系人创建屏幕之前检查电子邮件是否有效,因此我希望将输入的文本直接放入新联系人创建页面的“电子邮件”字段中,并且不允许编辑。我尝试了许多方法,但无法让电子邮件显示在文本字段中。有人能解释为什么会这样吗?

    用户输入电子邮件的初始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];
    

    这是实际ContactCreatorVC的代码:

    -(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;
        }
    
    .....
    
    }
    

    即使问这么简单的问题,我也觉得自己很愚蠢,但我显然不明白幕后发生了什么,因为我什么都试过了。有人能解释发生了什么,并从最佳实践的角度提出理想的解决方案吗?

    编辑:我想我的问题可以更简洁。。。这基本上可以归结为:为什么当我设定自我。当我访问self时,init中的电子邮件不会粘住。用cellForRow方法发送电子邮件(变为零)?

    (textfield位于tableview的单元格中)

    1 回复  |  直到 8 年前
        1
  •  1
  •   matt    8 年前

    问题是这一行:

    [contactCreationVC.emailField setText:@"asdsadasd"];
    

    contactCreationVC . 其视图尚未加载,因此 emailField nil (通过一些基本的测井很容易证明)。发送给的消息 什么都不做。

    正确的方法是:切勿触摸其他视图控制器的插座。设置a 所有物 另一个视图控制器的 与自己的门店打交道。在这种情况下,另一个视图控制器需要使用该属性来设置 电子邮件字段 text 在its中 viewDidLoad

    至于为什么 那个 这种方法不适合你们,据我所知,并没有足够的信息来回答这个问题。如果你能证明事情是按这个顺序进行的:

    1. ContactCreatorVC init 使用实际电子邮件值调用,并设置属性。

    2. 该物业有一项强有力的(保留)政策,因此保留了价值。

    3. 同样的ContactCreatorVC实例现在有了 cellForRowAtIndexPath 调用,此时属性为 .

    如果你能证明这一切,那么唯一合乎逻辑的结论是,同时出现了一些其他代码,并将属性设置为 .