More iPhone Controls




CS185c

Chris Pollett

Feb. 15, 2012

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

//
//  SecondPhone2012ViewController.h
//  SecondPhone2012
//
//  Created by Chris Pollett on 2/15/12.
//  Copyright (c) 2012 San Jose State University. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface SecondPhone2012ViewController : UIViewController
@property (retain, nonatomic) IBOutlet UITextField *nameField;
@property (retain, nonatomic) IBOutlet 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

Sliders

Slider Screenshot

Slider Demo Screenshot

Segmented Controls and Switches

Segmented Control and Switch Header

#import 
#define kShowSegmentIndex 0

@interface SecondPhone2012ViewController : UIViewController 

@property (retain, nonatomic) IBOutlet UITextField *nameField;
@property (retain, nonatomic) IBOutlet UITextField *numberField;
@property (retain, nonatomic) IBOutlet UILabel *sliderLabel;
@property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;
@property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;
@property (retain, nonatomic) IBOutlet UIView *switchView;

-(IBAction)textFieldDoneEditing: (id)sender; 
-(IBAction)backgroundClick:(id)sender;
-(IBAction)sliderChanged:(id)sender;
-(IBAction)switchChanged:(id)sender;
-(IBAction)toggleShowHide:(id)sender;
@end

Segmented Control and Switch Implementation

@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize switchView;

-(IBAction)switchChanged:(id)sender
{
	UISwitch *whichSwitch = (UISwitch *)sender;
	BOOL setting = whichSwitch.isOn;
	[leftSwitch setOn:setting animated:YES];
	[rightSwitch setOn:setting animated:YES];
}

-(IBAction)toggleShowHide:(id)sender
{
	UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
	NSInteger segment = segmentedControl.selectedSegmentIndex;
	
	if(segment == kShowSegmentIndex) [switchView setHidden:NO];
	else [switchView setHidden:YES];
	
}

After writing this code we need to set up each of the outlets and actions in Interface Builder.

Switch Segmented Control Screenshot

Demo of Switch and Segmented Controls

Action Sheets and Alerts

How we modify our Controller's Header

#import 
#define kShowSegmentIndex 0

@interface SecondPhone2012ViewController : UIViewController 
<UIActionSheetDelegate>

@property (retain, nonatomic) IBOutlet UITextField *nameField;
@property (retain, nonatomic) IBOutlet UITextField *numberField;
@property (retain, nonatomic) IBOutlet UILabel *sliderLabel;
@property (retain, nonatomic) IBOutlet UISwitch *rightSwitch;
@property (retain, nonatomic) IBOutlet UISwitch *leftSwitch;
@property (retain, nonatomic) IBOutlet UIView *switchView;
@property (retain, nonatomic) IBOutlet UIButton *doStuffButton;

-(IBAction)textFieldDoneEditing: (id)sender; 
-(IBAction)backgroundClick:(id)sender;
-(IBAction)sliderChanged:(id)sender;
-(IBAction)switchChanged:(id)sender;
-(IBAction)toggleShowHide:(id)sender;
-(IBAction)doStuff:(id)sender;
@end

Code for doStuff

-(IBAction)doStuff:(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