使用控制器和视图
Model 应用程序的核心 负责计算与创建一个虚拟世界,它不依靠View与Controller就能存在。(一个没有外观接口的应用程序)
Controller在Xcode通常是指View controller。可以把它想成一座Model跟View之间的桥梁。
View则是一个让用户可以与程序沟通的窗口,大部分情况下,View都是用来显示Model提供的数据,除此之外也负责处理与用户的互动。用户都是透过View与应用程序间的互动,而Controller则负责捕捉互动讯息并传送给Model。
使用UIAlertView显示提示
UIAlertView *alertView = [ [ UIAlertView
alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK",nil];
[alertView show];
UIAlertView 有提供一个类型为UIAlertViewStyle的属性
tyledef enum{
UIAlertViewStyleDefault = 0,//默认样式
UIAlertViewStyleSecureTextInput,//这个样式中,提示视图会包含一个安全加密的文字栏位。
UIAlertViewStylePlainTextInput,//在这种样式中,提示视图会显示一个可见的文字栏位,一般来说使用这种样式来要求用户输入一些信息,例如电话号码。
UIAlertViewStyleLoginAndPasswordInput//在这个样式中,提示视图会显示两个文本栏位,一个是可见的用户名称栏位,另一个是加密 密码栏位。
}UIAlertViewStyle;
若希望从提示视图得到用户操作通知,就必须制定一个委托对象,委托对象符合UIAlertViewDelegate协定。
alertView:clickedButtonAtIndex 这个方法可以得到用户在提示视图上所按下的按钮。
-(NSString *)yesButtonTitle{
return @"Yes";
}
-(NSString *)noButtonTitle{
return @"No";
}
[ [ UIAlertView
alloc] initWithTitle:@"Title" message:@"Message" delegate:nil cancelButtonTitle:[self noButtonTitle] otherButtonTitles:[self yesButtonTitle],nil];
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *buttonTitle = [alertView buttonTItleAtIndex:buttonIndex];
if([buttonTitle isEqualToString:[self yesButtonTitle]]){
YES;
}else if([buttonTitle isEqualToString:[self noButtonTitle]]){
NO;
}
}
使用UISwitch创建及使用开关
定义以及初始化
@property(nonamotic,strong)UISwitch *muSwitch;
self.view.backgroundColor = [UIColor whiteColor];
self.mySwitch = [ [UISwitch alloc]initWithFrame:CGRectMake(100,100,0,0)];
self.view addSubview:self.mySwitch];
[self.mySwitch setOn:YES];//预设状态
判断开关
if([self.mySwitch isOn]){
NSLog(@"On");
}else{
NSLog(@"Off");
}
若希望在开关控件打开或关闭时得到消息通知信息,必须在类中加上开关的target。
[self.mySwitch addTatget:self action:@selector(switchIsChanged:) forControlEvents:UIControlEventValueChanged];
-(void)switchIsChanged:(UISwitch *)paramSender{
if([paramSender isOn]){
ON;
}else{
OFF;
}
}
使用UIPickerView来绑定数据
@inte易做图ce PickerView:UIViewController<UIPickerViewDataSource,UIPickerViewDelegate>
@property(nonamotic,strong)UIPickerView *myPicker;
@synthesize myPicker;
self.view.backgeoundColor = [UIColor whiteColor];
self.myPicker = [[UIPickerView alloc]init];
self.myPicker.dataSource = self;
self.myPicker.delegate = self;
self.myPicker.showsSelectionIndicator = YES; //水平阴影效果
self.myPicker.center = self.view.center;
self.view addSubview:self.myPicker];
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{//组件个数
NSInteger result = 0;
if([pickerView isEqual:self.myPicker]){
result = 1;
}
return result;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{//一个组件有多少个选项
NSInteger result = 0;
if([pickerView isEqual:self. myPicker]){
result = 10;
}
return result;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
NSString *result = nil;
if([pickerView isEqual:self. myPicker]){
result = [NSString stringWithFormat:@"Row %id",(long)row + 1];
}
return result;
}
//监听方法
selectedRowInComponent:
//值重置
reloadCompoment:
使用UIDatePicker来进行日期和时间的绑定
@inte易做图ce DatePicker:UIViewConteoller
@property(nonamotic,strong)UIDatePicker *myDatePicker;
@end
@synthesize myDatePicker;
self.myDatePicker.backgroundColor = [UIColor whiteColor];
self.myDatePicker = [[UIDatePicker alloc]init];
self.myDatePicker.center = self.view.center;
[self.view addSubview:self.myDatePicker];
[self.myDatePicker addTarget:self action:@selecter(datePickerDateChanged:) forControlEvents:UIControlEventValueChanged];
展示效果://根据你的需要来设置相关的参数
typedef enum{
UIDatePickerModeTime,
UIDatePickerModeDate,
UIDatePickerModeDateAndTime,
UIDatePickerModeCountDownTimer,
}UIDatePickerMode;
//取当前时间值
-(void)datePickerDateChanged:(UIDatePicker *)paramDatePicker{
if([paramDatePicker isEqual:self.myDatePicker]){
NSLog(@"Selected date = %@",paramDatePicker.date);
}
}
//限制选择时间范围(2013.1--2014.1)
NSDate *oneYearFromToday = [todayDate dateByAddingTimeInterval:oneYearTime];
NSDate *twoYearFromToday = [todayDate dateByAddingTimeInterval:2*oneYearTime];
self.myDatePicker.minimumDate = oneYearFromToday;
self.myDatePicker.maximumDate = twoYearsFromToday;
//秒表功能
-(void)viewDidLoad{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.myDatePicker = [[UIDatePicker alloc]init];
self.myDatePicker.datePickerMode = UIDatePickerModeCountDownTimer;
[self.view addSubview:self.myDatePicker];
NSTimeInterval townMinutes = 2 * 60;
[self.myDatePicker setCountDownDuration:twoMinutes];
}