Storyboard

2022/11/2

# 跳转传值

# 构造函数传参

  • 界面指着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

# 指定内容传参








 
 
 








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

# 反向传值

# 协议传值法



 
 




 
 







 
 
 
 


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

# 闭包传值法

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

# 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

# UITabBarController

  • Main创建UITabController
  • 按住controlUITabBarController拖到要关联的页面即可完成关联
  • 创建多个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

由于是在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

# 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

# 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

# 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

# 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

# 添加事件

  • 添加action事件,
  • @IBActionsenter需要固定
// 按钮事件
@IBAction func addItem(senter: UIButton) {
    print("aaaaa")
}
复制成功
1
2
3
4
上次更新: 6/13/2025
Powered By Valine
v1.4.16