Outline
- More on iPhone Controls: TextFields
- Quiz
- Protocols
- Action sheets and Alerts
Active, Static, and Passive Controls
- On the iPhone UI controls come in three types: active (ex, button), static (ex, label), and passive (ex text field).
- All iPhone controls are a subclass of UIControl.
- Each control can trigger multiple actions depending on how they are touched. For example, a finger swipe might do something different than a tap.
- For some passive controls, like a text field we'll want to bring up the iPhone keyboard.
- The iPhone Keyboard is implemented as a view filled with a series of button controls.
- You never directly interact with the keyboard, but you instead write auxiliary code to control how it behaves.
An Image View and Text Field Example
- We're going to build a simple app, with a view consistent of an Image at the top, then a pair of label and textfields.
- First, to create the image we, as we did last day, create and add an image to our Resources folder
- Then add an Image View to our View Panel in Interface Builder.
- Using the Inspector on the Image View Attributes, we select our image for the Image attribute.
- Next we add two Labels and two Text Fields to our view, these can be found in IB's Library under Inputs& Values
- For the Text Fields, we edit their Placeholder attribute to hint at what the user should
be entering in that text field.
- Make sure the Clear When Editing check box beneath this attribute is checked so the hint text vanishes when people start typing.
What it should look like
Text Input Traits
- Beneath the Font Attributes group of Text Field Attributes their is a collection of Text Input Traits.
- These controls how, when the Text Field is selected, auto-capitilization, spell-checking, and other things are done.
- One useful Text Input Trait is the Keyboard trait. We can select this to be Number Pad if we want the keyboard to default to be the numeric keypad.
- There is another Text Input Trait with which you can also select the text of the return key to be Done, if you like.
- Next we set up the Outlets for each of the Text Fields by Ctrl-dragging the File's Owner to each of them.
- If we did Build and Run at this point you would notice that once you had entered a Text Field the keyboard would come up but not go away.
Interface Code for Getting Keyboard To Go Away
//
// MoreControlsViewController.h
// MoreControls
//
// Created by Chris Pollett on 9/14/09.
// Copyright San Jose State University 2009. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface MoreControlsViewController : UIViewController {
IBOutlet UITextField *nameField;
IBOutlet UITextField *numberField;
}
@property (nonatomic, retain) UITextField *nameField;
@property (nonatomic, retain) UITextField *numberField;
- (IBAction)textFieldDoneEditing: (id)sender;
// This is the action we want to handle to get rid of the keyboard
@end
Modified Controller Code for Getting Keyboard To Go Away
@synthesize nameField;
@synthesize numberField;
-(IBAction)textFieldDoneEditing:(id)sender
{
[sender resignFirstResponder];
}
- This tells the Text Field to no longer behave as first responder for the UI
input.
- To hook this action with the Text Fields, we click on a given text field then
open the Connection Inspector. The Event we want to handle is Did End on Exit. So for textfield click and drag it to File's Owner and select the action textFieldDoneEditing.
Click Background To Close Keyboard
Quiz
@property is to @interface as ?
- @protocol is to @implementation
- Activity is to Intent.
- @synthesize is to @implementation.
Action Sheets and Alerts
- Chapter 4 in the book describes setting up Sliders and putting a sub View on a given view, as well as segmented controls. These are relatively straightforward so we'll skip it.
- We are now going to discuss adding a button and making an action sheet
appear after clicking it.
- Action sheets are modal dialogs -- when an action screen is shown, the user cannot interact with any other part of the application.
- To implement an action sheet we need to say our class conforms to the protocol.
UIActionSheetDelegate.
- Protocols are a little bit like Java interfaces. They can be defined in a header file using the syntax:
@protocol MyDelegate
// list optional methods
-(void) optionalMethod1;
// ...
@required
//list required methods to implement protocol
-(void) requiredMethod1:(id)myThing;
//...
@end;
- A class that uses this protocol says so with a declaration like:
@interface MyClass : NSObject <MyDelegate>
//...
@end;
How we modify our Controller's Header
#import <UIKit/UIKit.h>
@interface MoreControlsViewController : UIViewController
<UIActionSheetDelegate>
{
IBOutlet UITextField *nameField;
IBOutlet UITextField *numberField;
IBOutlet UIButton *doSomethingButton;
}
@property (nonatomic, retain) UITextField *nameField;
@property (nonatomic, retain) UITextField *numberField;
@property (nonatomic, retain) UIButton *doSomethingButton;
- (IBAction)textFieldDoneEditing: (id)sender;
- (IBAction)backgroundClick:(id)sender;
- (IBAction)doSomething:(id)sender;
@end
Code for doSomething
-(IBAction)doSomething:(id)sender
{
UIActionSheet *actionSheet = [[UIActionSheet alloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way!"
destructiveButtonTitle:@"Yes, I'm Sure!"
otherButtonTitles:nil];
[actionSheet showInView:self.view];
[actionSheet release];
}
Code for Protocol Method
-(IBAction)actionSheet:(UIActionSheet *)actionSheet
didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if(!(buttonIndex == [actionSheet cancelButtonIndex]))
{
NSString *msg = nil;
if(nameField.text.length > 0)
{
msg = [[NSString alloc] initWithFormat:
@"You can breathe easy, %@, everything went okay."
, nameField.text ];
}
else
{
msg = @"You can breathe easy, everything went okay.";
}
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Something was done"
message:msg
delegate:self
cancelButtonTitle:@"Phew!"
otherButtonTitles:nil];
[alert show];
[alert release];
[msg release];
}
}
To Complete the Project
- In IB we then add a new button with Do Something on it.
- We then connect its Touch Up Inside event to our doSomething method