开发者

Having Trouble With UITextField and NSString

I have the following code for UI开发者_Go百科TextField. The problem I'm having is that the text does not persist. For example, when I present a modal view, then dismiss it, the UITextField does not have the text anymore in it. I want the text to remain there until I dimiss that view with the text field.

I am displaying the UITextField like this:

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

Then I have:

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


Instead of editingEnded, Implement the UITextFieldDelegate protocol. Go to the textFieldDidEndEditing method and reassign the value of text in it.

Like,

-(void) textFieldDidEndEditing:(UITextField *)textField{

if (textField.tag ==0){
    self.myTextField = textField; 

  // myTextField is a property
    }

Now in the in the viewDidLoad method or the viewWillAppear method, go ahead and assign this value back to the textField.

If necessary use [tableView reloadData] if this is used in a tableView or use [self reloadInputViews] (if necessary).

Then again, its all logical. Nothing too complex in code.


Use UITextFieldDelegate method that is textFieldDidEndEditing, which gets called when editing ends


In my opinion:

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

You haven't own tempRoutineName to release. Comment out the release and check.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜