Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

whitehexagon

macrumors regular
Original poster
May 12, 2007
147
0
objective-c newbie

I'm using pushViewController with a manually loaded xib/nib file. In my view controller I implement viewDidLoad to change the button labels on a navigation bar. Next I want to the focus to be the one and only textfield defined in my view. I've tried a couple of approaches from googling, but nothing seems to work. What I want is that after the view loads, to place the focus on the textfield and display the popup keyboard (btw any easy way to limit the contents to numbers and letters [a-zA-Z0-9]).

I've tried in the view controller to implement canBecomeFirstResponder returning YES, no effect.

I've tried calling becomeFirstResponder on the view, no effect.

So what am I missing here? It sounds like from apple docs that this should just detect the first textfield and give it focus, so is my xib file somehow wrong?

Also this whole exercise was an attempt to get a single value from the user, the navigation contains a save & cancel button. Is there no way to pop up a simple Dialog for this info? ie a simple API based approach?
 
Make sure you have hooked up the UITextField properly to the right outlets in Interface Builder. Once you have it hooked up to the File Owner in Interface Builder, you should be able to control it programmatically and then ssetting it as the first responder should work:

Code:
[myTextField becomeFirstResponder]

This method only works if it is hooked up properly to the view hierarchy. You could test that it is not returning nil by using the following code, which prints a YES or NO to the console log:
Code:
BOOL wasSuccessful = [myTextField becomeFirstResponder];
NSLog(@"Text field becoming first responder was successful? : %i",wasSuccessful);

UITextField adopts the UITextInputTraits protocol. You can use the keyboardType property to change the keyboard to a key pad or email or whatever version you want.

e.g.
Code:
myTextField.keyboardType = UIKeyboardTypeNumberPad

You can also specify other properties such as autocapitalisation and auto-correction using the UITextInputTraits protocol.
 
IBoutlet is connected to the textfield and if I write an accessor for the value, I can read the value. But where would I then implement your example code? in the view or the view controller? If it's the view controller than I will need access to the textfield instance somehow (see other post).

But also the example I was following for loading my xib file. Was aying the FileOwner should be set to UIViewController rather than my ABCViewController. And to be honest I haven't found a good explanation for what the Interface builder first responder means in this context. Is this what I'm missing. Or will it be enough to use your example once I work out how to access the textfield?

Code:
	ABCViewController *viewController = [[ABCViewController alloc] initWithNibName:@"ABCView" bundle:nil];
	[[self navigationController] pushViewController:viewController animated:YES];
 
In your ABCNib, you would have the File Owner set to ABCViewController. This tells it to link up with your view controller code.

Accessing the text field in ABCViewController is then simple. Firstly, declare a UITextField instance variable called myTextField (or whatever) as an IBOutlet:

In ABCViewController.h:

Code:
IBOutlet UITextField *myTextField;

blah blah

@property (nonatomic, retain) IBOutlet UITextField *myTextField;

Now save the file in Xcode and then switch to Interface Builder. Control click from the file owner to the text field that you have placed on your main view and select myTextField, which should appear in the pop-up.

Now, they are linked and in ABCViewController myTextField will now access your text field object.

Note that because you are using Interface Builder to initiliase the object, you do not need to alloc/init the text field object in your code, as it has already been done for you. You will however have to release myTextField in the dealloc method so that you don't leak memory.
 
Well ow that I finally have the textfield defined in the right file, the focus/keyboard handling code was much easier. Although I still get 0 back from the succesful check, the keyboard non the less pops up ready for entry.

Thanks for taking some of the pain out a long learning day :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.