【问题标题】:Having Trouble With UITextField and NSStringUITextField 和 NSString 遇到问题
【发布时间】:2023-03-11 20:50:01
【问题描述】:

我有以下UITextField 的代码。我遇到的问题是文本不会持续存在。例如,当我呈现一个模态视图,然后将其关闭时,UITextField 中不再包含文本。我希望文本保留在那里,直到我用文本字段关闭该视图。

我正在像这样显示UITextField

UITextField *nameTextField = [[UITextField alloc] initWithFrame:CGRectMake(110, 10, 185, 30)];
nameTextField.delegate = self;
[nameTextField addTarget:self action:@selector(editingEnded) forControlEvents:UIControlEventEditingDidEnd];
[nameTextField setEnabled: YES];
self.myTextField = nameTextField;
[nameTextField release];

那么我有:

- (void)editingEnded
{
    NSString *tempRoutineName = self.myTextField.text;
    self.routineName = tempRoutineName;
    [tempRoutineName release];
 }

【问题讨论】:

    标签: iphone objective-c cocoa-touch uitextfield


    【解决方案1】:

    实现UITextFieldDelegate 协议,而不是editedEnded。转到textFieldDidEndEditing 方法并重新分配其中文本的值。

    喜欢,

    -(void) textFieldDidEndEditing:(UITextField *)textField{
    
    if (textField.tag ==0){
        self.myTextField = textField; 
    
      // myTextField is a property
        }
    

    现在在viewDidLoad 方法或viewWillAppear 方法中,继续将此值分配回textField。

    如果在 tableView 中使用,请使用 [tableView reloadData] 或使用 [self reloadInputViews](如果需要)。

    再一次,这一切都是合乎逻辑的。没有什么太复杂的代码。

    【讨论】:

    • 我贴的第一块代码其实是在configureCell方法中,因为UITextField在一个UITableViewCell中,所以我还是应该按照你在viewDidLoad中说的,还是在这个方法中呢?
    • 没有。在 cellForRowAtIndexPath 方法中,您可以在其中初始化文本字段。
    • 我不能做self.myTextField = textField,因为textField is declared in the configureCell`方法。
    • 实现 UITextFieldDelegate 并使用我粘贴的方法即可。
    • 我有那个方法。一秒钟让我给你更多代码。
    【解决方案2】:

    使用 UITextFieldDelegate 方法,即 textFieldDidEndEditing,编辑结束时调用该方法

    【讨论】:

      【解决方案3】:

      在我看来:

      NSString *tempRoutineName = self.myTextField.text;
      self.routineName = tempRoutineName;
      [tempRoutineName release];
      

      您还没有要释放的 tempRoutineName。注释掉发布并检查。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-28
        相关资源
        最近更新 更多