I am new to Mac, iOS, Xcode and objective-c. I am very familiar with the Windows world (.net, C#, Silverlight, etc), but none of that really applies to what I am doing here on the Mac.
So in short I am hoping that you would help me out with pointers and a code review of sorts. There are many articles on the web (many of those on stack overflow) and you can easily get lost in all the opinions of how to do things.
So I decided to try a few simple things, like making the iOS keyboard react to Next and Done, show a toolbar above the keyboard and make text fields move to allow room for the keyboard. First sample I have here is the simple one, three text fields, two with Next and the last with Done to close the keyboard.

I added UITextFieldDelegate and a IBOutlet for each text field to the header file. Also Ctrl-dragged the text field onto the IBOutlet to connect it.
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField* firstNameTextField;
@property (weak, nonatomic) IBOutlet UITextField* lastNameTextField;
@property (weak, nonatomic) IBOutlet UITextField* cityTextField;
@end
In the source file I added the synthesize statements for the text fields, the set to nil in the viewDidUnload to flag the memory for release (using the auto memory release mode) and the textFieldShouldReturn method to set the focus on the text fields.
#import "ViewController.h"
@implementation ViewController
@synthesize firstNameTextField = _firstNameTextField;
@synthesize lastNameTextField = _lastNameTextField;
@synthesize cityTextField = _cityTextField;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[self setFirstNameTextField:nil];
[self setLastNameTextField:nil];
[self setCityTextField:nil];
[super viewDidUnload];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField
{
if (theTextField == self.firstNameTextField)
{
[self.lastNameTextField becomeFirstResponder];
}
else if (theTextField == self.lastNameTextField)
{
[self.cityTextField becomeFirstResponder];
}
else
{
[theTextField resignFirstResponder];
}
return YES;
}
@end
Is this how you would do this?
From an objective-c perspective, would you put the * (for pointer) with the type or in front of the name?
@property (weak, nonatomic) IBOutlet UITextField* firstNameTextField;
vs.
@property (weak, nonatomic) IBOutlet UITextField *firstNameTextField;
Is the memory management correct? Will the memory be released correctly, stay alive etc?
Thanks.
As Anthony stated, the position of the asterisk is really a concern of style. Personally I prefer the * on the side of the instance (Class *classInstance) because it’s the instance that you’re pointing to, not the class. But opinions differ.
In general, the code is fine, except for that you’ve defined all your properties as weak. Because you want to retain these IBOutlet properties for the life of your view controller, they should be strong. Even though you’ve constructed your views in IB, your view controller still ‘owns’ those IB objects.
When you define a property type as:
That means your object will be retained when it’s assigned. In this case, the assignment happens by virtue of having wired your outlets, you don’t assign manually, but the retain still occurs:
And released when you nil it:
And if/when (as you have done) you do actually want a weak property (eg, to have a reference to an object you don’t own):
You wouldn’t be required to “release” it later by setting it to nil in your viewDidUnload. Setting it to nil would still be good practice, but not required to balance your memory.