Swift iOS : NavigationController
控制器NavigationController常用于用来做层次化UI导航。类名为UINavigationController。 NavigationController可以通过属性包含多个ViewController、一个UINavigationBar、一个可选的UIToolbar。 以一个共三层的ViewController的案例,来展示NavigationController的使用:
代码如下: import UIKit @UIApplicationMain class AppDelegate: UIResponder,UIApplicationDelegate { var window: UIWindow? var nav : Nav? func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { self.window = UIWindow(frame: UIScreen.main.bounds) nav = Nav() self.window!.rootViewController = nav self.window?.makeKeyAndVisible() return true } } class Nav: UINavigationController { var count = 0 var label : UILabel! override func viewDidLoad() { super.viewDidLoad() self.view.backgroundColor = .white self.pushViewController(Level1(),animated: true) } } class Level1: UIViewController { var count = 0 var label : UILabel! override func viewDidLoad() { super.viewDidLoad() self.title = "Level 1" self.view.backgroundColor = .white let button = UIButton(type: .system) button.frame = CGRect(x: 120,y: 100,width: 100,height: 50) button.setTitle("Dive Into 2",for: .normal) button.addTarget(self,action: #selector(Level1.buttonAction(_:)),for: .touchUpInside) view.addSubview(button) } func buttonAction(_ sender:UIButton!){ let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.nav?.pushViewController(Level2(),animated: true) } } class Level2: UIViewController { var count = 0 var label : UILabel! override func viewDidLoad() { super.viewDidLoad() self.title = "Level 2" self.view.backgroundColor = .white let button = UIButton(type: .system) button.frame = CGRect(x: 120,height: 50) button.setTitle("Dive Into 3",for: .normal) button.addTarget(self,action: #selector(Level2.buttonAction(_:)),for: .touchUpInside) view.addSubview(button) } func buttonAction(_ sender:UIButton!){ let appDelegate = UIApplication.shared.delegate as! AppDelegate appDelegate.nav?.pushViewController(Level3(),animated: true) } } class Level3: UIViewController { var count = 0 var label : UILabel! override func viewDidLoad() { super.viewDidLoad() self.title = "Level 3" self.view.backgroundColor = .white let button = UIButton(type: .system) button.frame = CGRect(x: 120,height: 50) button.setTitle("End",action: #selector(Level3.buttonAction(_:)),for: .touchUpInside) view.addSubview(button) } func buttonAction(_ sender:UIButton!){ } } 解释下:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |