IOS學習筆記 (3)

來源:互聯網
上載者:User

使用控制器和視圖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];} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.