Swift - UISplitViewController

本文详细探讨了Swift编程中如何使用UISplitViewController,包括在AppDelegate中的配置和MasterViewController与DetailViewController的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

AppDelegate中:


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        
        let master = MasterViewController()
        let detail = DetailViewController()
        master.detailViewController = detail
        let navigationController = UINavigationController(rootViewController: master)
        //判断设备
        if UIDevice.current.userInterfaceIdiom == .phone{
            self.window?.rootViewController = navigationController
        }else{
            //创建分割视图控制器(iPad特有)
            let split = UISplitViewController()
            //横屏下显示
            split.viewControllers = [navigationController,detail]
            self.window?.rootViewController = split
        }
        return true
    }

创建MAsterViewController:

import UIKit
class MasterViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
 
    var tableView:UITableView!
    var ctrls = ["A","B","C"]
    var detailViewController:DetailViewController!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "列表"
        self.tableView = UITableView(frame: self.view.frame,style:.plain)
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "ReusedCell")
        self.view.addSubview(tableView)
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ctrls.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let identifier = "ReusedCell"
        let cell = tableView.dequeueReusableCell(withIdentifier: identifier, for: indexPath)
        cell.accessoryType = .disclosureIndicator
        cell.textLabel?.text = self.ctrls[indexPath.row]
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        detailViewController!.loadControl(ctrl: self.ctrls[indexPath.row])
//        detailViewController.title = ctrls[indexPath.row]
        if (UIDevice.current.userInterfaceIdiom == .phone){
         tableView.deselectRow(at: indexPath, animated: true)
            self.navigationController?.pushViewController(detailViewController, animated: true)
        }
    }
    
}

DetailViewController:

import UIKit
class DetailViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        let ctrl = self.title != nil ? self.title! : ""
        loadControl(ctrl: ctrl)
    }
    func loadControl(ctrl:String){
        clearViews()
        switch ctrl {
        case "A":
            let label = UILabel(frame: self.view.bounds)
            label.backgroundColor = UIColor.black
            label.textColor = UIColor.orange
            label.text = "hellow"
            self.view.addSubview(label)
        case "B":
            let button = UIButton(frame: CGRect(x: 150, y: 250, width: 100, height: 100))
            button.setTitle("按钮", for: .normal)
            button.backgroundColor = UIColor.red
            self.view.addSubview(button)
        case "C":
            let uiSwitch = UISwitch(frame: CGRect(x: 150, y: 250, width: 0, height: 0))
            uiSwitch.setOn(false, animated: true)
            self.view.addSubview(uiSwitch)
        default:
            print("error")
        }
    }
    func clearViews(){
        for v in self.view.subviews{
            v.removeFromSuperview()
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值