IOS開發學習第四章---使用更多的UI控件

這一章介紹了一些基本的UI控件以及詳細介紹了每個UI控件中各種屬性的用法。

設計UI控件有:

  • UIImageView
  • UITextField
  • UIButton
  • Slider
  • Switch
  • UIView
  • ActionSheet
  • Alert
小實例長這樣:


涉及知識點:
1. 虛擬鍵盤的關閉
對應不同的虛擬鍵盤,有不同的鍵盤關閉方法。
有Done按鈕的,沒有Done按鈕的處理方式不同
有Done按鈕的:
綁定textField的‘Did end on Exit'事件去一個BIAction,
[sender resignFirstResponder];

沒有Done按鈕的,如數字鍵盤
綁定textField父控件,也就是UIView
設置UIView的控件Class 爲’UIControl'
然後綁定它的‘Touch up' 事件,關閉代碼同上面。這樣做以後,點擊背景即可關閉虛擬鍵盤。

2. 如何獲取控件的改變
對於slider
int progress = lroundf(sender.value);

對於switch
BOOL setting = sender.isOn;

對於switch button
if (sender.selectedSegmentIndex == 0) {
        self.leftSwitch.hidden = NO;
        self.rightSwitch.hidden = NO;
        self.doSomethingButton.hidden = YES;
    }
    else {
        self.leftSwitch.hidden = YES;
        self.rightSwitch.hidden = YES;
        self.doSomethingButton.hidden = NO;
    }

3. 如何控制隱藏與顯示
見上面。
4. 如何打開ActionSheet 和獲取用戶的決定
UIActionSheet *actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:@"Are you sure?"
                                  delegate:self
                                  cancelButtonTitle:@"No Way!"
                                  destructiveButtonTitle:@"Yes, I’m Sure!"
                                  otherButtonTitles:nil];
    [actionSheet showInView:self.view];

if (buttonIndex != [actionSheet cancelButtonIndex]) {
        NSString *msg = nil;
        
        if ([self.nameField.text length] > 0) {
            msg = [NSString stringWithFormat:
                   @"You can breathe easy, %@, everything went OK.",
                   self.nameField.text];
        } else {
            msg = @"You can breathe easy, everything went OK.";
        }
        
        UIAlertView *alert = [[UIAlertView alloc]
                              initWithTitle:@"Something was done"
                              message:msg
                              delegate:self
                              cancelButtonTitle:@"Phew!"
                              otherButtonTitles:nil];
        [alert show];
    }


5. 如何使用alert顯示消息
見上面

難點:
1. 理解delegate (個人理解,可能有誤,還在完善中)
有些控件需要使用delegate纔可以讓它顯示出來和使用。
首先需要在相應的控件類中聲明需要用到的delegate協議。
@interface BIDViewController : UIViewController <UIActionSheetDelegate>

然後使用協議中特定的方法顯示和使用控件。
控件中的delegate屬性可以是自己也可以是下一個控件。
就是讓自己或下一個控件幫你做一個什麼事兒。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章