https://blog.csdn.net/weixin_43704791/article/details/86424080
2019年01月13日
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()
}
}
}