# 跳转传值
# 构造函数传参
- 界面指着
button
按住control
拖到代码里,生成关联
class ViewController: UIViewController {
@IBAction func touchJump(_ sender: Any) {
print("跳转");
// 传值方法
let view2 = ViewController2(data: "好好学习");
self.present(view2, animated: true) {
print("跳转成功");
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- new file -> Cocoa Touch class -> subclass of:
UIViewController
import UIKit
class ViewController2: UIViewController {
var data: String?;
init(data: String? = nil) {
super.init(nibName: nil, bundle: nil);
self.data = data
}
required init?(coder: NSCoder) {
super.init(coder: coder);
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.red;
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.setTitle("返回", for: UIControl.State.normal)
button.addTarget(self, action: #selector(back), for: UIControl.Event.touchUpInside)
self.view.addSubview(button);
// 接受界面入参
let label = UILabel(frame: CGRect(x: 20, y: 200, width: 200, height: 50));
label.text = data;
label.textColor = UIColor.white;
self.view.addSubview(label);
}
@objc func back () {
self.dismiss(animated: true) {
print("返回了");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 指定内容传参
import UIKit
class ViewController: UIViewController {
@IBAction func touchJump(_ sender: Any) {
print("跳转");
// 传值方法
let view2 = ViewController2();
view2.data = "我是第一个页面给你的值!";
self.present(view2, animated: true) {
print("跳转成功");
}
}
override func viewDidLoad() {
super.viewDidLoad()
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import UIKit
class ViewController2: UIViewController {
var data: String?;
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.red;
let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
button.setTitle("返回", for: UIControl.State.normal)
button.addTarget(self, action: #selector(back), for: UIControl.Event.touchUpInside)
self.view.addSubview(button);
// 接受界面入参
let label = UILabel(frame: CGRect(x: 20, y: 200, width: 200, height: 50));
label.text = data;
label.textColor = UIColor.white;
self.view.addSubview(label);
}
@objc func back () {
self.dismiss(animated: true) {
print("返回了");
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 反向传值
# 协议传值法
import UIKit
// 实现了指定的接受参数的协议
class ViewController: UIViewController, View2BackSendDataProtocal {
var label: UILabel?;
@IBAction func jump(_ sender: Any) {
let view2 = View2();
// 进入该页面的时候赋值 delegate 为自己,让其调用自己实现协议的赋值方法
view2.delegate = self;
self.present(view2, animated: true);
}
override func viewDidLoad() {
super.viewDidLoad()
label = UILabel(frame: CGRect(x: 20, y: 100, width: 100, height: 50));
self.view.addSubview(label!);
}
func sendData(data: String) {
print("data: \(data)");
self.label?.text = data;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import UIKit
// 只做一个协议,实现此协议的 类 需要实现【接受值的方法】
protocol View2BackSendDataProtocal {
func sendData(data: String);
}
class View2: UIViewController {
// delegate是一个【实现了指定协议的类】
var delegate: View2BackSendDataProtocal?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.gray;
let button = UIButton(frame: CGRect(x: 20, y: 100, width: 100, height: 50));
button.setTitle("返回", for: UIControl.State.normal);
button.addTarget(self, action: #selector(jump), for: UIControl.Event.touchUpInside);
self.view.addSubview(button);
}
// 返回事件
@objc func jump() {
// 此时 delegate 已经是可以接收值的 【实现了协议的类】 了
delegate?.sendData(data: "儿子给你的数据!");
self.dismiss(animated: true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# 闭包传值法
import UIKit
class ViewController: UIViewController {
var label: UILabel?;
@IBAction func jump(_ sender: Any) {
// 实现view2 的闭包函数
let view2 = View2();
view2.closure = ({s -> Void in
self.label?.text = s;
})
self.present(view2, animated: true);
}
override func viewDidLoad() {
super.viewDidLoad()
label = UILabel(frame: CGRect(x: 20, y: 100, width: 100, height: 50));
self.view.addSubview(label!);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import UIKit
class View2: UIViewController {
// 预留一个 optional类型的闭包
var closure: ((String) -> Void)?
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.gray;
let button = UIButton(frame: CGRect(x: 20, y: 100, width: 100, height: 50));
button.setTitle("返回", for: UIControl.State.normal);
button.addTarget(self, action: #selector(jump), for: UIControl.Event.touchUpInside);
self.view.addSubview(button);
}
@objc func jump() {
// 调用自己的闭包函数
self.closure!("还你的值!");
self.dismiss(animated: true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# UINavigator
- 将默认的view删掉,加上一个
UINavigation
- 新建第一个页面controller 的 class
- 点击 root view -> 右边第四个选项关联class
import UIKit
class TableViewController: UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "首页";
// 设置顶部导航栏
self.navigationController?.navigationBar.barTintColor = UIColor.white;
let barItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonItem.SystemItem.done, target: self, action: #selector(click));
self.navigationItem.leftBarButtonItem = barItem;
}
@objc func click() {
// 跳转到第二个页面
let viewController = ViewController();
self.navigationController?.pushViewController(viewController, animated: true);
}
// MARK: - Table view data source
override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return 0
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
- ViewController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.title = "关于";
self.view.backgroundColor = UIColor.blue;
// 不写默认是出栈,title是上一个栈的title
let backItem = UIBarButtonItem(title: "< 返回", style: UIBarButtonItem.Style.plain, target: self, action: #selector(pop))
self.navigationItem.leftBarButtonItem = backItem;
}
@objc func pop() {
self.navigationController?.popViewController(animated: true);
_ = self.navigationController?.popViewController(animated: true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# UITabBarController
- Main创建
UITabController
- 按住
control
从UITabBarController
拖到要关联的页面即可完成关联 - 创建多个viewController
import UIKit
class ViewController3: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor.green;
let item = UITabBarItem(tabBarSystemItem: UITabBarItem.SystemItem.contacts, tag: 2);
self.tabBarItem.title = "我的"; // 无效
self.tabBarItem = item;
self.tabBarItem.badgeValue = "99+"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
由于是在
viewDidLoad
初始化的,所以初始化需要在页面首次被打开才会展示正确。书上说可以在视图控制器的构造方法中进行
# UIAlertController
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad();
let button = UIButton(frame: CGRect(x: 20, y: 100, width: 100, height: 50));
button.setTitle("出弹窗", for: UIControl.State.normal);
button.backgroundColor = UIColor.darkGray;
button.tintColor = UIColor.white;
button.addTarget(self, action: #selector(alert), for: UIControl.Event.touchUpInside);
self.view.addSubview(button);
}
@objc func alert() {
// 整体的弹框
let alertController = UIAlertController(title: "确定要点击吗?", message: "系统会崩溃哦~", preferredStyle: .alert);
// 用户的选项
let ok = UIAlertAction(title: "确定", style: UIAlertAction.Style.default) {action in
print("点击了确定");
}
let cancel = UIAlertAction(title: "取消", style: UIAlertAction.Style.cancel) {action in
print("点击了取消");
}
alertController.addAction(ok);
alertController.addAction(cancel);
self.present(alertController, animated: true);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# WebView
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let webView = UIWebView(frame: self.view.bounds);
let url = URL(string: "https://www.bilibili.com");
let request = URLRequest(url: url!);
webView.loadRequest(request);
self.view.addSubview(webView);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import UIKit
class ViewController: UIViewController, UIWebViewDelegate {
var webView: UIWebView?;
var back: UIButton!;
var go: UIButton!;
override func viewDidLoad() {
super.viewDidLoad()
webView = UIWebView(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height - 60));
let url = URL(string: "https://www.bilibili.com");
let request = URLRequest(url: url!);
webView!.loadRequest(request);
self.view.addSubview(webView!);
webView?.delegate = self;
let toolBar = UIView(frame: CGRect(x: 0, y: self.view.frame.size.height - 60, width: self.view.frame.size.width, height: 30));
toolBar.backgroundColor = UIColor.gray;
back = UIButton(frame: CGRect(x: 0, y: 0, width: 100, height: 30))
back.setTitle("返回", for: .normal);
back.addTarget(self, action: #selector(goBack), for: UIControl.Event.touchUpInside)
go = UIButton(frame: CGRect(x: 50, y: 0, width: 100, height: 30))
go.setTitle("前进", for: .normal);
go.addTarget(self, action: #selector(goFront), for: UIControl.Event.touchUpInside);
toolBar.addSubview(back);
toolBar.addSubview(go);
self.view.addSubview(toolBar);
}
@objc func goBack() {
webView!.goBack();
}
@objc func goFront() {
print("前进");
webView!.goForward();
}
func weViewDidFinshLoad(_ webView: UIWebView) {
back.isEnabled = webView.canGoBack;
go.isEnabled = webView.canGoForward;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Webkit
- 优于webview
- 网页与原生交互频繁的场景处理,有与javascript交互的方法。
import UIKit
import WebKit
class ViewController: UIViewController {
var wkView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let configuration = WKWebViewConfiguration();
wkView = WKWebView(frame: self.view.frame, configuration: configuration);
self.view.addSubview(wkView!);
let url = URL(string: "http://www.bilibili.com");
let request = URLRequest(url: url!)
wkView!.load(request);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import UIKit
import WebKit
class ViewController: UIViewController, WKScriptMessageHandler {
var wkView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
// 偏好设置
let configuration = WKWebViewConfiguration();
configuration.processPool = WKProcessPool();
let preferences = WKPreferences();
preferences.minimumFontSize = 0;
preferences.javaScriptCanOpenWindowsAutomatically = true;
configuration.preferences = preferences;
// 注册javascript能使用的全局方法的名称
let userContentController = WKUserContentController();
userContentController.add(self, name: "nativeFunc");
let javascriptString = """
const fn = () => {
console.log("我是注入的js代码!js执行啦");
window.webkit.messageHandlers.nativeFunc.postMessage({name: "hdy", age: 18});
}
fn();
"""
let userScript = WKUserScript(source: javascriptString, injectionTime: .atDocumentStart, forMainFrameOnly: false);
// 开始注入
userContentController.addUserScript(userScript);
configuration.userContentController = userContentController;
wkView = WKWebView(frame: self.view.frame, configuration: configuration);
self.view.addSubview(wkView!);
let url = URL(string: "http://www.bilibili.com");
let request = URLRequest(url: url!)
wkView!.load(request);
}
// 接收js发送的消息
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
print(message.body, message.name);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# UIScrollCView
let scrollView = UIScrollView(frame: self.view.frame);
self.view.addSubview(scrollView);
let subView1 = UIView(frame: self.view.frame);
subView1.backgroundColor = UIColor.red;
scrollView.addSubview(subView1);
let subView2 = UIView(frame: self.view.frame);
subView2.backgroundColor = UIColor.blue;
scrollView.addSubview(subView2);
// 固定尺寸,来计算需要的尺寸
scrollView.contentSize = CGSize(width: self.view.frame.width, height: self.view.frame.height * 2);
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
// 划过头回弹效果
scrollView.bounces = false;
scrollView.alwaysBounceVertical = false; // 竖直方向回弹
scrollView.alwaysBounceHorizontal = false;// 水平方向回弹
// 滚动条显示
scrollView.showsVerticalScrollIndicator = false; // 竖直滚动条
scrollView.showsHorizontalScrollIndicator = false; // 水平滚动条
// 自动分页,轮播图的效果
scrollView.isPagingEnabled = true;
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
- 状态监听方法
class ViewController: UIViewController, UIScrollViewDelegate {
optional func scrollViewDidScroll(_ scrollView: UIScrollView)
optional func scrollViewDidZoom(_ scrollView: UIScrollView)
optional func scrollViewWillBeginDragging(_ scrollView: UIScrollView)
optional func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>)
optional func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool)
optional func scrollViewWillBeginDecelerating(_ scrollView: UIScrollView)
optional func scrollViewDidEndDecelerating(_ scrollView: UIScrollView)
optional func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView)
optional func viewForZooming(in scrollView: UIScrollView) -> UIView?
optional func scrollViewWillBeginZooming(_ scrollView: UIScrollView, with view: UIView?)
optional func scrollViewDidEndZooming(_ scrollView: UIScrollView, with view: UIView?, atScale scale: CGFloat)
optional func scrollViewShouldScrollToTop(_ scrollView: UIScrollView) -> Bool
optional func scrollViewDidScrollToTop(_ scrollView: UIScrollView)
optional func scrollViewDidChangeAdjustedContentInset(_ scrollView: UIScrollView)
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# UITableView
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var dataArray: Array<String> = ["第一行", "第二行", "第三行", "第四行", "第五行"];
override func viewDidLoad() {
super.viewDidLoad()
let tableView = UITableView(frame: self.view.frame, style: .grouped);
tableView.register(NSClassFromString("UITableViewCell"), forCellReuseIdentifier: "TableViewCellId");
tableView.delegate = self;
tableView.dataSource = self;
self.view.addSubview(tableView);
tableView.bounces = false;
}
// 分区个数
func numberOfSections(in tableView: UITableView) -> Int {
return 2;
}
// 行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count;
}
// 重要:每行内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCellId", for: indexPath)
cell.textLabel?.text = dataArray[indexPath.row];
return cell;
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "分区头部";
}
func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
return "分区尾部";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
- 定义数据类
import UIKit
class TableData: NSObject {
var done = false;
var title = "";
init(_ title: String = "", _ done: Bool = false) {
self.done = done
self.title = title
}
}
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
- 定义UI连接文件
- 定义
reuseIdentifier
:组件复用id,在页面可见高度上一直复用同一组件,只改数据,提升性能 - 将
类名
和reuseIdentifier
的值配置到UI组件上,关联页面TableViewCell和控制器类
import UIKit
class TableViewCell: UITableViewCell {
// 连接label
@IBOutlet weak var title: UILabel!
// 连接button
@IBOutlet weak var switchBtn: UISwitch!
// 定义本组件的复用id,一个页面只展示部分同一类组件,组件复用,性能好
static let reuseIdentifier = "itemCell"
// 管理本组件的数据,类传递引用,因此会直接改
var model: TableData = TableData("", false);
// 让父组件 tableView 统一调用的方法,初始化本组件数据
func config(_ model: TableData) {
// 给关联组件传值要放在这里
title.text = model.title
switchBtn.isOn = model.done
self.model = model
}
@IBAction func toogleTouch() {
self.model.done = switchBtn.isOn
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
- 定义tableView(继承
UITableViewDataSource, UITableViewDelegate
类) - UI关联类
- 在
viewDidLoad
指定delegate
和dataSource
代理连接,并且view添加上table
- 实现几个主要实现方法:行数/cell内容
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var tableView: UITableView!
var dataArray: Array<TableData> = [
TableData("吃饭", false),
TableData("打游戏", false),
TableData("睡觉", false),
];
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self;
tableView.dataSource = self;
self.view.addSubview(tableView);
// 是否使用 弹性滚动边界
tableView.bounces = false;
}
// 按钮事件
@IBAction func addItem(senter: UIButton) {
dataArray.append(TableData("接着睡觉", false));
tableView.reloadData();
}
// 分区个数
func numberOfSections(in tableView: UITableView) -> Int {
return 1;
}
// 行数
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataArray.count;
}
// 每行内容
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// 重点:调用指定类,并且指定复用id
let cell = (tableView.dequeueReusableCell(withIdentifier: TableViewCell.reuseIdentifier, for: indexPath))as! TableViewCell
cell.config(dataArray[indexPath.row]);
return cell;
}
func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return "今日任务";
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# 添加事件
- 添加action事件,
@IBAction
和senter
需要固定
// 按钮事件
@IBAction func addItem(senter: UIButton) {
print("aaaaa")
}
1
2
3
4
2
3
4