Swift UITextField,UITextView,UISegmentedControl,UISwitch

下面我们通过一个demo来简单的实现下这些控件的功能.

首先,我们拖将这几个控件拖到storyboard,并关联上相应的属性和动作.如图:

关联上属性和动作后,看看实现的代码:

 //点击按钮收起键盘
    @IBAction func closeKeyboard(sender: UIButton) {
        self.view.endEditing(true)
        
    }
   //点击文本框键盘的return键收起键盘
    @IBAction func TextFieldDidEndExit(sender: UITextField) {
        self.textfield.resignFirstResponder()
    }
    //点击屏幕收起键盘
    override func touchesBegan(touches: NSSet,withEvent event: UIEvent) {
        self.view.endEditing(true)
    }
    //点击分段按钮完成一些工作
    @IBAction func SegmentValueChange(sender: UISegmentedControl) {
        var index = self.segment.selectedSegmentIndex
        println("您选中了:\(index)")
       // print("您选中了:%ld",index)
        
    }
    //通过开关取值然后实现联动
    @IBAction func SwitchValueChange(sender: UISwitch) {
       
        let flag = sender.on
        println("flag=\(flag)")
        self.leftSwish.setOn(flag,animated: true)
        self.rightSwish.setOn(flag,animated: true)
    }
     //多行文本框的委托方法收起键盘(一定要指定委托)
    func textView(textView: UITextView,shouldChangeTextInRange range: NSRange,replacementText text: String) -> Bool{
        //只有用户选择了分段按钮的回车返回时才执行
        if (self.segment.selectedSegmentIndex == 1)
        {
            if text == "\n"
            {
                //如果用户录入回车,收起键盘
                textview.resignFirstResponder()//多行文本框放弃第一响应者
                return false
            }
        }
        return true
    }

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...