More iPhone Controls




CS185c

Chris Pollett

Sep. 14, 2009

Outline

Active, Static, and Passive Controls

An Image View and Text Field Example

What it should look like

An example of a iPhone UI with an image and two textfields

Text Input Traits

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];
}

Click Background To Close Keyboard

Quiz

@property is to @interface as ?

  1. @protocol is to @implementation
  2. Activity is to Intent.
  3. @synthesize is to @implementation.

Action Sheets and Alerts

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