参照IPhone4 基础开发教程第四章:更丰富的用户界面
XCode版本:4.0.2
//
// test04ViewController.h
// test04
//
// Created by lily on 12-1-27.
// Copyright 2012年 lily. All rights reserved.
//
#import<UIKit/UIKit.h>
#define kSwithesSegmentIndex 0
@interface test04ViewController :UIViewController <UIActionSheetDelegate>{
//以下部分是指向实例的变量
UITextField *nameField;
UITextField *numberField;
UILabel *sliderLabel;
UISwitch *leftSwitch;
UISwitch *rightSwitch;
UIButton *doSomethingButton;
}
//声明的IBOutlet的属性,该属性用于连接Interface Builder中的控件
//再次声明的属性,需要在对应的.m文件中,声明为@synthesis
@property (nonatomic,retain)IBOutletUITextField *nameField;
@property (nonatomic,retain)IBOutletUITextField *numberField;
@property (nonatomic,retain)IBOutletUILabel *sliderLabel;
@property (nonatomic,retain)IBOutletUISwitch *leftSwitch;
@property (nonatomic,retain)IBOutletUISwitch *rightSwitch;
@property (nonatomic,retain)IBOutletUIButton *doSomethingButton;
//以IBAction开始的是连接输入口, 将Interface Builder中的控件的事件连接到这些操作上,可以引发响应的操作
- (IBAction)textFieldDoneEditing:(id)sender;
- (IBAction)backgroundTap:(id)sender;
- (IBAction)sliderChanged:(id)sender;
- (IBAction)toggleControls:(id)sender;
- (IBAction)switchChanged:(id)sender;
- (IBAction)buttonPressed;
//actionSheet操作表用于在IPhone下放弹出警告信息
//要使该操作有效需要时当前的ViewController实现<UIActionSheetDelegate>协议
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex;
@end
//
// test04ViewController.m
// test04
//
// Created by lily on 12-1-27.
// Copyright 2012年 lily. All rights reserved.
//
#import"test04ViewController.h"
@implementation test04ViewController
//对应头文件.h中的属性@property申明
@synthesize nameField;
@synthesize numberField;
@synthesize sliderLabel;
@synthesize leftSwitch;
@synthesize rightSwitch;
@synthesize doSomethingButton;
//在TextField控件的检查器中如果回车键改为Done键后,可以在事件中找到Did End On Exit
//将改事件连接到此操作,即可在键盘按下Done键后关闭键盘
- (IBAction)textFieldDoneEditing:(id)sender{
//关闭键盘的方法实际上是注销当前的TextField控件为第一响应者。
[senderresignFirstResponder];
}
//在键盘没有Done键功能的情况下,实现触摸背景空白处,关闭键盘的功能
//
- (IBAction)backgroundTap:(id)sender{
//注销所有TextField为第一响应者
[nameFieldresignFirstResponder];
[numberFieldresignFirstResponder];
}
//Slider移动时触发此操作,触发事件为Value Changed。
- (IBAction)sliderChanged:(id)sender{
//通过sender获取Slider的值
UISlider *slider = (UISlider *)sender;
int progressAsInt = (int)(slider.value +0.5);
NSString *newText = [[NSString alloc] initWithFormat:@"%d", progressAsInt];
sliderLabel.text = newText;
//由于alloc了新的String,所以再结束时需要对其释放。
[newText release];
}
//Segmented Control控件触发的操作,触发事件为Value Changed
- (IBAction)toggleControls:(id)sender{
//kSwithcesSegmentIndex是个define常量,在.h文件中定义如下:
//#define kSwithesSegmentIndex 0
//目的是判断按下了Segment Control控件中的第几个键。
if([sender selectedSegmentIndex] == kSwithesSegmentIndex){
//hidden变量用于控制控件是否显示在屏幕上
leftSwitch.hidden = NO;
rightSwitch.hidden = NO;
doSomethingButton.hidden =YES;
}
else{
leftSwitch.hidden = YES;
rightSwitch.hidden = YES;
doSomethingButton.hidden =NO;
}
}
//Switch 控件触发的操作,触发事件为Value Changed
- (IBAction)switchChanged:(id)sender{
//获取传进来的Switch控件的值
UISwitch *whichSwitch = (UISwitch *)sender;
BOOL setting = whichSwitch.isOn;
//将左右2个Switch控件都修改为我们得到的输入值
[leftSwitchsetOn:settinganimated:YES];
[rightSwitchsetOn:settinganimated:YES];
}
//Button 控件触发的操作,触发事件为Touch Up Inside
- (IBAction)buttonPressed{
/*创建ActionSheet
initWithTile: 操作表的标题
delegate: 指定操作表的代理,表示处理的实例对象为自己即test04ViewController,实际上将处理指派给了(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex操作函数
cancelButtonTitle:撤销按钮的标题
destructiveButtonTitle: 确认按钮的标题
otherButtonTitles:其他按钮标题,为NSString Array,以nil作为结束*/
UIActionSheet *actionSheet = [[UIActionSheetalloc]
initWithTitle:@"Are you sure?"
delegate:self
cancelButtonTitle:@"No Way!"
destructiveButtonTitle:@"Yes, I'm Sure!"
otherButtonTitles:nil];
//显示ActionSheet, showInView指定显示的父视图
[actionSheet showInView:self.view];
//alloc后需要对actionSheet对象进行释放
[actionSheet release];
}
//对 <UIActionSheetDelegate>实现,和实际actionSheet操作的实现
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
//判断是否为撤销按钮, 如果不是则执行子程序
if(buttonIndex != [actionSheet cancelButtonIndex])
{
NSString *msg = nil;
if(nameField.text.length >0)
msg = [[NSStringalloc]initWithFormat:@"You can breathe easy, %@, everything went OK. ",nameField.text];
else
msg =@"You can breathe easy, everything went OK. ";
//警告视图初始化,和actionSheet的初始化雷同,只是警告视图是出现在IPhone屏幕中间的警告窗口而已
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Something was done"
message:msg
delegate:self
cancelButtonTitle:@"Phew!"
otherButtonTitles:nil];
//警告视图的显示不需要指定父视图
[alert show];
//释放alert对象
[alert release];
//释放msg对象
[msg release];
}
}
- (void)dealloc
{
//对所有的声明的对象进行释放
[nameFieldrelease];
[numberFieldrelease];
[sliderLabelrelease];
[leftSwitchrelease];
[rightSwitchrelease];
[doSomethingButtonrelease];
[superdealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[superdidReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
// 在viewLoad结束后对视图进行修改
- (void)viewDidLoad
{
//载入图像whiteButton.png, 该图像已经被实现导入到工程中
UIImage *buttonImageNormal = [UIImage imageNamed:@"whiteButton.png"];
//设置图像为可拉伸图像,左右不可拉伸像素为12px, 上下不可拉升像素为0px
UIImage *strechableButtonImageNormal = [buttonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
//设置按钮在普通状态下显示为whiteButton.png图像
[doSomethingButtonsetBackgroundImage:strechableButtonImageNormalforState:UIControlStateNormal];
UIImage *buttonImagePressed = [UIImage imageNamed:@"blueButton.png"];
UIImage *strechableButtonImagePressed = [buttonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
//设置按钮在高显状态下,即按钮被按下的状态下,显示为blueButton.png图像
[doSomethingButtonsetBackgroundImage:strechableButtonImagePressedforState:UIControlStateHighlighted];
//[super viewDidLoad];
}
- (void)viewDidUnload
{
//在图像被卸载时,将所有的对象声明为nil
//在多视图应用中,需要尤其注意,执行此操作
self.numberField =nil;
self.numberField =nil;
self.sliderLabel =nil;
self.leftSwitch =nil;
self.rightSwitch =nil;
self.doSomethingButton =nil;
[superviewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end