How to add UITextfield on click of UIButton and make it editable

北战南征 提交于 2019-12-19 12:46:07

问题


I have a UIButton and I am trying to develop an editable UITextField onclick of UIButton. Basically, I want the user to click on the button and edit the text and save it for future use.

So currently I have:

[notes_button addTarget:self action:@selector(editNote) forControlEvents:UIControlEventTouchUpInside];

I am not sure how to get a popup like UITextField onclick and add UITextField in editNote function.


回答1:


Try this. First make your textfield hide on viewdidload - then on button push, fade it in.. make it editable, and pop up keyboard for user.

//viewDidLoad

yourTextField.alpha = 0;
yourTextField.userInteractionEnabled = FALSE;

-(void)editNote {

            //fade in text field after button push
            [UIView beginAnimations:nil context:NULL];
            [UIView setAnimationDelay:0];
            [UIView setAnimationDuration:0.75];
            yourTextField.alpha = 1;

            [UIView commitAnimations];

           //make text field editable
           yourTextField.userInteractionEnabled = TRUE;

          //pop up keyboard
           [yourTextField becomeFirstResponder];

}

------- Update --------

Now that you have described what you are trying to do a little better. You will want to create an entirely new ViewController with the fields (UITextFields etc) on this new view controller.

On button click you will code:

TextFieldViewController * vc = [(TextFieldViewController *)[[TextFieldViewController alloc] init] autorelease];
[self.navigationController presentModalViewController:vc animated:YES];
[vc release];

//you will need to create a button on this new ViewController and use this code to dismiss it when done.

[[self parentViewController] dismissModalViewControllerAnimated:YES];


来源:https://stackoverflow.com/questions/7066875/how-to-add-uitextfield-on-click-of-uibutton-and-make-it-editable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!