IOS开发学习第四章---使用更多的UI控件

来源:互联网 发布:知乎阅读 编辑:程序博客网 时间:2024/04/30 05:01

这一章介绍了一些基本的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属性可以是自己也可以是下一个控件。
就是让自己或下一个控件帮你做一个什么事儿。

0 0
原创粉丝点击