ios

ios的常用控件

包含TextField、Switch、Slider、WebView等常用控件的使用介绍

Posted by catface on January 4, 2015

本篇会介绍如下 TextFireld、TextView、Button、Segment、Label、Slider、WebView、ActivityIndicator、Progress、Alert(DEPRECATED)、ActionSheet(DEPRECATED)、ActionControl、ToolBar、Bar Button Item

PageA

  1. TextField & TextView-输入完成点击 return 键盘消失

    • 当前类实现接口

      1
      
        class ViewController: UIViewController, UITextFieldDelegate, UITextViewDelegate
      
    • 实现对应的接口方法

      1
      2
      3
      4
      5
      
        // 主动放弃焦点
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {
            textField.resignFirstResponder()
            return true
        }
      

      1
      2
      3
      4
      5
      6
      7
      8
      
        // 主动放弃焦点
        func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
            if text == "\n" {
                textView.resignFirstResponder()
                return false
            }
            return true
        }
      
  2. Switch-让右开关随左开关联动

    1
    2
    3
    4
    5
    
     @IBAction func rightSwitchControl(_ sender: UISwitch) {
         var setting = sender.isOn // 获取左开关的开闭状态
         self.swLeft.setOn(setting, animated: true)
         self.swRight.setOn(setting, animated: true)
     }
    
  3. Segment-用分段控件控制两个开关的显示与隐藏

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
     @IBAction func showOrHideSwitch(_ sender: UISegmentedControl) {
         if self.swLeft.isHidden { // 获取左开关的隐藏状态
             self.swLeft.isHidden = false
             self.swRight.isHidden = false
         } else {
             self.swLeft.isHidden = true
             self.swRight.isHidden = true
         }
     }
    
  4. Slider-显示滑块的值

    1
    2
    3
    4
    5
    
     @IBAction func slideValueChangeLabel(_ sender: UISlider) {
         var progressAsInt = sender.value // 获取滑块的值
         let newResult = NSString(format: "%d", progressAsInt)
         self.lbValue.text = newResult as String
     }
    
  5. WebView-加载网页

    • loadHTMLString 加载本地页面

      1
      2
      
        let html = "<h1>welcome <a href='http://catface.cc'>catface</a></h1>"
        wv_home.loadHTMLString(html, baseURL: nil)
      
    • loadData 加载本地页面(略)

    • loadRequest 加载网络页面

      1
      2
      3
      4
      5
      6
      
        let addr = NSURL(string: "http://catface.cc")
        let request = NSURLRequest(url: addr! as URL) // 将URL封装成Request
        self.wv_home.loadRequest(request as URLRequest)
      		
        // 必不可少,将WebView的委托对象(当前视图控制器)分配给WebView->监听
        self.wv_home.delegate = self
      
      • 可选实现 UIWebViewDelegate 接口的如下方法

        1
        2
        3
        4
        
          // 页面加载完成后调用
          func webViewDidFinishLoad(_ webView: UIWebView) {
              NSLog("%@", wv_home.stringByEvaluatingJavaScript(from: "document.body.innerhtml")!)
          }
        
    • 注意 - 加载http网址系统会发出不安全警告!!!处理方法如下

      1. 在Info.plist中添加 NSAppTransportSecurity(类型 Dictionary)

      2. NSAppTransportSecurity 下添加 NSAllowsArbitraryLoads(值设为 YES)

PageB

  1. ActivityIndicator-使用按钮控制其动画开闭

    1
    2
    3
    4
    5
    
     if self.aiv_test.isAnimating { // 判断当前aiv是否正进行动画
         aiv_test.stopAnimating()
     } else {
         aiv_test.startAnimating()
     }
    
  2. Progress-使用 Timer 控制进度条的进度

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    
     var mTimer: Timer!  // 来一个Timer
     @IBAction func changePV(_ sender: UIButton) {
         // 参数:时间间隔s | 消息发送目标 | 调用方法 | 给消息发送参数 | 是否重复
         mTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: "download", userInfo: nil, repeats: true)
     }
    	
     func download() {
         pv_test.progress += 0.1
         if pv_test.progress == 1.0 {
             mTimer.invalidate()
             var alert = UIAlertView(title: "下载完成", message: "下载结束哦,哥哥", delegate: nil, cancelButtonTitle: "OK")
             alert.show()
             return
         }
     }
    
  3. Alert、ActionSheet被ActionControl替代(IOS8)

  • Alert

    1
    2
    3
    4
    5
    6
    7
    8
    
      // 单个选项
      UIAlertView(title: "警告", message: "我是警告内容", delegate: nil, cancelButtonTitle: "知道").show()
        
      // 两个选项
      UIAlertView(title: "警告", message: "我是警告内容", delegate: nil, cancelButtonTitle: "取消", otherButtonTitles: "确定").show()
        
      // 多个选项...
      UIAlertView(title: "警告", message: "我是警告内容", delegate: nil, cancelButtonTitle: "取消", otherButtonTitles: "确定", "玩一玩").show()
    
  • ActionSheet

    1
    2
    
      var asTest: UIActionSheet = UIActionSheet(title: "选择列表", delegate: nil, cancelButtonTitle: "取消", destructiveButtonTitle: "破坏性选项", otherButtonTitles: "百度", "腾讯", "阿里")
      asTest.show(in: self.view)
    
  • ActionControl(掌握这个可以了)

    1
    2
    3
    4
    5
    6
    
      var acTest: UIAlertController = UIAlertController(title: "标题", message: "消息内容", preferredStyle: UIAlertControllerStyle.alert)
        
      acTest.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) {(alertAction) -> Void in NSLog("取消")})
      acTest.addAction(UIAlertAction(title: "确定", style: UIAlertActionStyle.default) {(alertAction) -> Void in NSLog("确定")})
        
      self.present(acTest, animated: true, completion: nil)
    

    1
    2
    3
    4
    5
    6
    7
    8
    9
    
      var acTest: UIAlertController = UIAlertController()
        
      acTest.addAction(UIAlertAction(title: "取消", style: UIAlertActionStyle.cancel) {(alertAction) -> Void in NSLog("取消")})
      acTest.addAction(UIAlertAction(title: "破坏性选项", style: UIAlertActionStyle.destructive) {(alertAction) -> Void in NSLog("破坏")})
      acTest.addAction(UIAlertAction(title: "百度", style: UIAlertActionStyle.default) {(alertAction) -> Void in NSLog("百度")})
      acTest.addAction(UIAlertAction(title: "腾讯", style: UIAlertActionStyle.default) {(alertAction) -> Void in NSLog("腾讯")})
      acTest.addAction(UIAlertAction(title: "阿里", style: UIAlertActionStyle.default) {(alertAction) -> Void in NSLog("阿里")})
        
      self.present(acTest, animated: true, completion: nil)  
    

Page C&D

  1. ToolBar

    • save 须选择 Bar Button Item -> System Item -> save

    • 使用方式添加按钮再向类中拖个 @IBAction 处理即可

  2. Bar Button Item

    • 使用方式按照按钮向类中拖个 @IBAction 处理即可