Swift--UINavigationController

前端之家收集整理的这篇文章主要介绍了Swift--UINavigationController前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码目录


AppDelegate.swift

  1. func application(application: UIApplication,didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
  2. // Override point for customization after application launch.
  3. self.window = UIWindow(frame: UIScreen.mainScreen().bounds);
  4. self.window?.backgroundColor = UIColor.whiteColor();
  5. self.window?.makeKeyAndVisible();
  6. let main = ViewController();
  7. let nvc = UINavigationController(rootViewController: main);
  8. self.window?.rootViewController = nvc;
  9. return true
  10. }

ViewController.swift

  1. class ViewController: UIViewController {
  2.  
  3. override func viewDidLoad() {
  4. super.viewDidLoad()
  5. self.title = "Main";
  6. let nextItem = UIBarButtonItem(title: "Next",style: UIBarButtonItemStyle.Plain,target: self,action: "next");
  7. self.navigationItem.rightBarButtonItem = nextItem;
  8. }
  9.  
  10. override func didReceiveMemoryWarning() {
  11. super.didReceiveMemoryWarning()
  12. }
  13. func next(){
  14. let next = Next();
  15. self.navigationController?.pushViewController(next,animated: true);
  16. }
  17.  
  18.  
  19. }

Next.swift

  1. class Next: UIViewController {
  2. override func viewDidLoad() {
  3. super.viewDidLoad()
  4. self.title = "Next"
  5. let btn = UIButton(frame: CGRect(x: 110,y: 100,width: 100,height: 40));
  6. btn.setTitle("Back",forState: .Normal)
  7. btn.addTarget(self,action: "butClick",forControlEvents: .TouchUpInside)
  8. btn.backgroundColor = UIColor.blueColor();
  9. self.view.addSubview(btn)
  10. }
  11. override func didReceiveMemoryWarning() {
  12. super.didReceiveMemoryWarning()
  13. }
  14. func butClick()
  15. {
  16. self.navigationController?.popViewControllerAnimated(true);
  17. }
  18.  
  19. }

效果

猜你在找的Swift相关文章