01 Dec, 2009
More advanced UI concepts (still day 2 of iPhone learning)
Posted by: Chris In: Programming
Using the techniques described in the previous post I was able to make a simple interface which had two buttons, when you clicked them it updating a label describing which button was pressed.
Utterly useless but its the foundations of basic iPhone development, responding to interactions from the user.
In this post I continue on to chapter 4 of the book which handles more complex UIs. A lot of the chapter is to do with describing what properties do in the attributes inspector, such as changing textbox keyboards to be numeric rather than a full keyboard. All very simple stuff.
Making the keyboards go away
When the user taps the done button a “DidEndOnExit” event is generated which needs to be handled by the controller class (like all events). Make an action method like in the previous post:
-(IBAction)textFieldDoneEditing:(id)sender;
In the implementation in the .m file simply call [sender resignFirstResponder]. First responder is the control that the user is currently interacting with, resignFirstResponder is a method on all controls.
Once you have done this attach the action to the textfield(s) did End On Exit event
What if you have a number field which doesnt have a done button? When using the iPhone to escape number pads you press any area outside of the keyboard area. To achieve this you have to make the background respond to taps.
The controller has a property called view which is inherited from UIViewController . This property corresponds to the view icon in the nib file of your UI. This property points to an instance of UIView which acts as a container for all items in the interface. UIControl is a subclass of UIView so if we change it to that we gain UIcontrol's ability to trigger action methods in the controller.
Simply create a method in your controller similar to the one before except this one will resign first responder on all controls on the page. Then go to interface inspector, left click on the view icon in the nib, press apple 4 to bring up the identity inspector and change it’s class. You can then add the action to the Touch Down event as normal.
Action sheets and alerts
Action sheets are the modal dialogs that appear on the iPhone that require a choice from the user. Alerts are the blue rounded rectangle alerts you recieve from various applications, including the SMS app. Conventionally these are usually just to inform users of something important, rather than requiring a user’s input
For the controller class to act as a delegate for an action sheet it needs to conform to a protocol called UIActionSheetDelegate. Just add this to the end of the name of the controller in it’s header file.
To make an action sheet appear, use the UIActionSheet class (rtfm). The only area which might be slightly confusing will be setting the delegate. In the simple example I am doing it, you set it to self. By doing this the delegate will be notified and a method called didDismissWithButtonIndex:(NSInteger)buttonIndex in your controller will be called. In that method you’ll decide what to do based on the button the user pressed.
To create an alert use UIAlertView (very simple again, rtfm).





