Preston
Preston

Reputation: 1059

Grab Text From Dynamically Created UITextField

I am trying to grab the text from a dynamically created text field. I use this to make the text field become and resign first responder:

[(UITextField *)[self.view viewWithTag:0] becomeFirstResponder];

That works fine, but when I try to get the text, the app crashes.

[(UITextField *)[self.view viewWithTag:0] text];

What am I doing wrong?

Upvotes: 0

Views: 182

Answers (2)

RyanR
RyanR

Reputation: 7758

Since the default tag for every UIView is 0, I'm going to guess that there are multiple UIViews with the same tag (tags aren't guaranteed to be unique). Instead, choose an arbitrary high value like 1000, then increment that with each view added.

Also, it would help if you included your UITextField creation code.

Upvotes: 0

Deepak Danduprolu
Deepak Danduprolu

Reputation: 44633

Do not use the tag 0. viewWithTag: searches the view hierarchy starting from itself and since all views start with tag 0, it will identify itself as the view to be returned.

I suggest that you use a different tag on the text field.

Upvotes: 2

Related Questions