English 中文(简体)
Telling a UITextField to become first responder while embedded in a UITableViewCell
原标题:

I have a UITextField that is a subview of a UITableViewCell.

When my view loads, I want the text field to become first responder. I have a pointer to the text field in the table cell, so to do this I am trying:

[myTextField becomeFirstResponder];

This returns NO all the time, regardless of when it s called, which according to the docs means the text field refused to become first responder... What am I doing wrong?

Edit:

The textfield properly responds to being touched. It bring up the keyboard then. It s only when telling it to becomefirstresponder programmatically that the problem happens.

最佳回答

It turned out that the text field was nil when I was trying to send it first responder. Sending becomeFirstResponder later in the life cycle worked.

问题回答

I was having the same problem with a textfield I had added as a subview to a grouped table view cell - but figured out a solution after some poking around.

Assuming you have given the textfield a tag in your cellForRowAtIndexPath method, you can do something like this:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];

    myTextField = (UITextField *)[cell viewWithTag:kMyTextFieldTag];
    [myTextField becomeFirstResponder];
}

Calling it in the -(void)viewDidLoad would work.

-(void)viewDidLoad{
   [super viewDidLoad];
   // Do any additional setup after loading the view.
   [self.myTextField becomeFirstResponder] 

   //... any other code you need 
}

I was not getting the cursor to show up when the UITextField was made first responder inside of the cellForRowAtIndexPath. The text field would work just fine and dandy, but it was not showing the blinking cursor inside of the field. When you would press and hold, the zoom magnify would also not show a cursor.

I got around this odd issue by setting my becomeFirstResponder inside of the didSelectRowAtIndex method.

Is your UITableView editing property set to YES?





相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签