您现在的位置: IOS教程網 >> IOS系統 >  >> iOS 程式開發 >> iOS 開發綜合

iOS 第五章:常用控制項與對應動作

編輯:iOS 開發綜合

這一章的目標是把手上的 UIKit 元件摸熟:先回顧窗口與視圖,再逐一認識標籤、文字框、按鈕、開關、滑桿與分段控制項。

一、回顧:窗口與視圖

  • UIWindow:一個 App 只有一個,初始邊框等於整個螢幕,支援視窗層疊。
  • UIView:負責螢幕上一塊顯示區域,可以嵌套、可以有子視圖,並回應觸摸事件。
  • UIImageView:專門用來放圖片的視圖。

二、UIKit 的元件地圖

依用途分類會比死背名稱有效:

  • 容器:為內容提供額外的視覺分隔。
  • 窗口:提供繪製場所,UIWindow。
  • 顯示類:UILabel、UITextView、UIImageView。
  • 警告與動作表:UIAlertView、UIActionSheet,用來取得使用者注意。
  • 導航類:UITabBar、UINavigationBar,負責畫面之間的移動。
  • 表格類:UITableView,專門顯示結構化資料。
  • 選擇器:UIPickerView,用滾輪方式選資料。
  • 滾動與文字:UIScrollView、UITextView。

三、UILabel:唯讀的文字顯示

CGRect labelFrame = CGRectMake(0, 10, 100, 50);
UILabel *myLabel = [[UILabel alloc] initWithFrame:labelFrame];

myLabel.backgroundColor = [UIColor clearColor];
myLabel.textColor = [UIColor redColor];
myLabel.font = [UIFont fontWithName:@"Verdana" size:18.0];
myLabel.numberOfLines = 2;
myLabel.text = @"Hello World\nSecond line";

[self.view addSubview:myLabel];
[myLabel release];

四、UITextField:單行輸入

CGRect textRect = CGRectMake(10, 10, 300, 31);
UITextField *myTextField = [[UITextField alloc] initWithFrame:textRect];
myTextField.backgroundColor = [UIColor whiteColor];
myTextField.font = [UIFont systemFontOfSize:22.0];
myTextField.adjustsFontSizeToFitWidth = YES;
myTextField.minimumFontSize = 2.0;
myTextField.delegate = self;

按鍵盤的 Return 時收起鍵盤:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

限制輸入長度:

- (BOOL)textField:(UITextField *)textField
shouldChangeCharactersInRange:(NSRange)range
            replacementString:(NSString *)string {
    NSInteger MAX_CHARS = 10;
    NSMutableString *newText = [NSMutableString stringWithString:textField.text];
    [newText replaceCharactersInRange:range withString:string];
    return ([newText length] <= MAX_CHARS);
}

五、UIButton:兩種建立方式

用 Interface Builder

建立 View 後,從物件庫把按鈕拖進來,用屬性檢查器調整外觀;在 .h 中宣告 IBAction,並在 IB 裡拉線連接:

- (IBAction)buttonPress:(UIButton *)sender;
- (IBAction)buttonPress:(UIButton *)sender {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示標題"
                                                    message:@"通過 xib 實現的按鈕處理方法"
                                                   delegate:self
                                          cancelButtonTitle:@"OK"
                                          otherButtonTitles:nil];
    [alert show];
}

用純程式碼

roundedButtonType = [UIButton buttonWithType:UIButtonTypeRoundedRect];
roundedButtonType.frame = CGRectMake(50.0, 250.0, 88.0, 48.0);
[roundedButtonType setTitle:@"Rounded" forState:UIControlStateNormal];
roundedButtonType.backgroundColor = [UIColor clearColor];
[roundedButtonType addTarget:self
                      action:@selector(action:)
            forControlEvents:UIControlEventTouchUpInside];
roundedButtonType.tag = 2;   // 用 tag 分辨多個按鈕

帶圖片的按鈕:

[myButton setImage:[UIImage imageNamed:@"myButtonImage.png"] forState:UIControlStateNormal];

六、UISwitch:開關

CGRect switchRect = CGRectMake(120, 50, 0, 0);
UISwitch *mySwitch = [[UISwitch alloc] initWithFrame:switchRect];

[mySwitch addTarget:self
              action:@selector(switchAction:)
    forControlEvents:UIControlEventValueChanged];

[mySwitch setOn:YES animated:YES];

想改開關上「ON / OFF」的字樣與顏色,可以深入它的子視圖去取出兩個 label:

UIView *mainView = [[[[mySwitch subviews] objectAtIndex:0] subviews] objectAtIndex:2];
UILabel *onLabel  = [[mainView subviews] objectAtIndex:0];
UILabel *offLabel = [[mainView subviews] objectAtIndex:1];

onLabel.text  = @"YES";
offLabel.text = @"NO";
onLabel.textColor  = [UIColor yellowColor];
offLabel.textColor = [UIColor greenColor];

不過要提醒:這種做法依賴私有視圖層級,系統一升級就可能失效,正式產品不建議使用。

七、UISlider:滑桿

CGRect sliderRect = CGRectMake(20, 50, 280, 40);
UISlider *mySlider = [[UISlider alloc] initWithFrame:sliderRect];
mySlider.minimumValue = 0;
mySlider.maximumValue = 100;
mySlider.continuous   = YES;   // 拖動過程持續觸發

把值吸附到固定刻度再顯示:

-(void)sliderAction:(id)sender {
    int stepAmount = 10;
    float stepValue = (abs([(UISlider *)sender value]) / stepAmount) * stepAmount;
    [sender setValue:stepValue];
    lblSliderValue.text = [NSString stringWithFormat:@"%d", (int)stepValue];
}

把這些元件串起來練習,你會發現它們的套路高度一致:建立 → 設定屬性 → addTarget 綁事件 → 加到視圖 → 釋放。記住這個節奏,換任何一個 UIKit 控制項都能快速上手。

  1. 上一篇文章:
  2. 下一篇文章:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © IOS教程網 All Rights Reserved