iOS常用控件

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

PageA

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

    • 当前类实现接口

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

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

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

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

    @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-显示滑块的值

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

    • loadHTMLString 加载本地页面

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

    • loadRequest 加载网络页面

      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 接口的如下方法

        // 页面加载完成后调用
        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-使用按钮控制其动画开闭

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

    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**
	
	```
	// 单个选项
	UIAlertView(title: "警告", message: "我是警告内容", delegate: nil, cancelButtonTitle: "知道").show()
	
	// 两个选项
	UIAlertView(title: "警告", message: "我是警告内容", delegate: nil, cancelButtonTitle: "取消", otherButtonTitles: "确定").show()
	
	// 多个选项...
	UIAlertView(title: "警告", message: "我是警告内容", delegate: nil, cancelButtonTitle: "取消", otherButtonTitles: "确定", "玩一玩").show()
	```
    
- **ActionSheet**    

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

	```		
	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)
	```
	    
    ---
	   
	```
	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 处理即可
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值