本篇会介绍如下 TextFireld、TextView、Button、Segment、Label、Slider、WebView、ActivityIndicator、Progress、Alert(DEPRECATED)、ActionSheet(DEPRECATED)、ActionControl、ToolBar、Bar Button Item
PageA
-
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 }
-
-
Switch-让右开关随左开关联动
@IBAction func rightSwitchControl(_ sender: UISwitch) { var setting = sender.isOn // 获取左开关的开闭状态 self.swLeft.setOn(setting, animated: true) self.swRight.setOn(setting, animated: true) }
-
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 } }
-
Slider-显示滑块的值
@IBAction func slideValueChangeLabel(_ sender: UISlider) { var progressAsInt = sender.value // 获取滑块的值 let newResult = NSString(format: "%d", progressAsInt) self.lbValue.text = newResult as String }
-
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网址系统会发出不安全警告!!!处理方法如下
-
在Info.plist中添加
NSAppTransportSecurity
(类型 Dictionary) -
在
NSAppTransportSecurity
下添加NSAllowsArbitraryLoads
(值设为 YES)
-
-
PageB
-
ActivityIndicator-使用按钮控制其动画开闭
if self.aiv_test.isAnimating { // 判断当前aiv是否正进行动画 aiv_test.stopAnimating() } else { aiv_test.startAnimating() }
-
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 } }
-
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
-
ToolBar
-
save 须选择 Bar Button Item -> System Item -> save
-
使用方式添加按钮再向类中拖个
@IBAction
处理即可
-
-
Bar Button Item
- 使用方式按照按钮向类中拖个
@IBAction
处理即可
- 使用方式按照按钮向类中拖个