IOS學習筆記 (3) – iPhone手機開發技術文章 iPhone軟體開發教學課程

使用控制器和視圖

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來綁定數據

@interface 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來進行日期和時間的綁定

@interface 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];

}

 

發佈留言