zoukankan      html  css  js  c++  java
  • Swift

    UIViewController类详解:

    通过Nib文件初始化

    [objc] view plain copy
     
     
    1. init(nibName nibName: String?, bundle nibBundle: NSBundle?)  
    2. println("nibName = (self.nibName)")                                    //nibName  
    3. println("nibBundle = (self.nibBundle)")                                //nibBundle  
    
    

    StoryBoard相关

    [objc] view plain copy
     
     
    1. println("storyboard = (self.storyboard)")                              //storyboard<pre name="code" class="objc">//在跳转之前对Segue进行判断,如果返回false则不之行这个Segue的跳转, performSegueWithIdentifier:sender:如果使用了,则这个方法无效  
    2. override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {  
    3.     return true  
    4. }  
    5. //跳转执行  
    6. override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {  
    7.    print("prepareForSegue")  
    8. }  
    9. //根据UIStoryBoarSegue的Identifier进行跳转  
    10. override func performSegueWithIdentifier(identifier: String?, sender: AnyObject?) {  
    11.     super.performSegueWithIdentifier(identifier!, sender: sender)  
    12. }  
    [objc] view plain copy
     
     
    1. //subViewController是否能够执行Unwind Segue  
    2. override func canPerformUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject) -> Bool {    
    3. }  
    [objc] view plain copy
     
     
    1. //如果执行Unwind Segue,就返回Segue  
    2. override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue {  
    3. }  
    [objc] view plain copy
     
     
    1. //能够执行Segue的Controller  
    2. func viewControllerForUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject?) -> UIViewController? {  
    3. }  
    
    
    
    
    
    
    

    Unwindsegue的实现原理请参考相关文章

    View相关

    [objc] view plain copy
     
     
    1. println("view = (view)")  
    2. println("view is loaded = (isViewLoaded())")  
    3. title = "ViewController"<pre name="code" class="objc">//如果不是nib文件初始化而来,初始化的时候需要调用这个方法初始化view,此方法不能主动调用,是系统调用的<pre name="code" class="objc">override func loadView() {   
    4.     super.loadView()<pre name="code" class="objc">}//view初始化以后调用  
    [objc] view plain copy
     
     
    1. override func viewDidLoad() {  
    [objc] view plain copy
     
     
    1.     super.viewDidLoad()   <span style="font-family: Arial, Helvetica, sans-serif;">//view将可见的时候调用</span>  
    2. } <pre name="code" class="objc">override func viewWillAppear(animated: Bool) {  
    3.     super.viewWillAppear(animated)  
    4. }  
    5.       
    6. // view变得完全可见了以后执行  
    7. override func viewDidAppear(animated: Bool) {  
    8.     super.viewDidAppear(animated)  
    9. }  
    10.       
    11. //view被遮挡或者隐藏时调用  
    12. override func viewWillDisappear(animated: Bool) {  
    13.     super.viewWillDisappear(animated)  
    14. }  
    15.       
    16. //view被遮挡或者隐藏后调用  
    17. override func viewDidDisappear(animated: Bool) {  
    18.     super.viewDidDisappear(animated)  
    19. }  
    
    
    
    
    
    
    
    

    模式跳转

    [objc] view plain copy
     
     
    1. //设置模式跳转的类别,但是必须是目的Controller设置,不能是上级设置  
    2. //CoverVertical, FlipHorizontal, CrossDissolve, PartialCurl四种类型  
    3. viewController.modalTransitionStyle = .FlipHorizontal  
    4. //设置模式展示样式,适合于iPad上  
    5. viewController.modalPresentationStyle = .FullScreen  
    6. //如果展示不是.FullScreen, 那么设置是不是捕获statusBar的样式,适合iPad  
    7. viewController.modalPresentationCapturesStatusBarAppearance = true  
    8. //判断在模式跳转时消失是否键盘  
    9. viewController.disablesAutomaticKeyboardDismissal()  
    10.   
    11. presentViewController(viewController, animated: true) { () -> Void in  
    12.     //跳转到下个界面  
    13. }  
    14. dismissViewControllerAnimated(true , completion: { () -> Void in  
    15.     //回复模式跳转  
    16.       
    17. })  

    配置View的layout

    [objc] view plain copy
     
     
    1. // layoutSubviews方法调用之前  
    2. override func viewWillLayoutSubviews() {  
    3.     super.viewWillLayoutSubviews()  
    4. }  
    5. // layoutSubviews方法调用之后  
    6.  override func viewDidLayoutSubviews() {  
    7.     super.viewDidLayoutSubviews()  
    8. }<pre name="code" class="objc">  

    updateViewConstraints()

    
    
    [objc] view plain copy
     
     
    1. //延伸的方向--set which sides of your view can be extended to cover the whole screen.  
    2. if self.respondsToSelector(Selector("edgesForExtendedLayout")) {  
    3.     self.edgesForExtendedLayout = .None  
    4. }  
    5. //Scrollview滚动时处于全屏,默认YES  
    6. if self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets")) {  
    7.      self.automaticallyAdjustsScrollViewInsets = true  
    8. }  
    9. //当statusbar是透明时,是否扩展至StatusBar,默认情况下是NO,且statusbar不是透明的  
    10. if self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars")) {  
    11.     self.extendedLayoutIncludesOpaqueBars = false  
    12. }  
    13.           
    14. //控制view的大小UIPopoverController用的比较的广泛  
    15. self.preferredContentSize = self.view.bounds.size  
    
    

    跳转相关

    [objc] view plain copy
     
     
    1. isBeingPresented()                                                      //是否在展示  
    2. isBeingDismissed()                                                      //是否在dismiss          
    3.           
    4. isMovingToParentViewController()  
    5. isMovingFromParentViewController()  

    旋转相关

    [objc] view plain copy
     
     
    1. //是否需要旋转  
    2. override func shouldAutorotate() -> Bool {  
    3.     return true  
    4. }  
    5. //支持的方向  
    6. override func supportedInterfaceOrientations() -> Int {  
    7.     return 2  
    8. }  
    9. //优先支持的方向  
    10. override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {  
    11.     return .Portrait  
    12. }  

    自定义的ViewController Container

    [objc] view plain copy
     
     
    1. //https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html  
    2. //addChildVIewController:调用这个方法指明一个视图控制器作为你的子视图。  
    3. func addChildViewController(childController: UIViewController) {  
    4.       
    5. }  
    6. //调用这个方法将一个视图控制器从你的子视图列表里移除。  
    7. func removeFromParentViewController() {  
    8.       
    9. }  
    10. //这是一个使用一个唯一可选的视图替换另一个视图的新方法,或者移动一个子视图到前台来。通过使用这个方法,这个视图控制器的生命周期信息会被正确地发送出去        func transitionFromViewController(fromViewController: UIViewController, toViewController: UIViewController, duration: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?) {  
    11.       
    12. }  
    13. //将要移到父Controller  
    14. func willMoveToParentViewController(parent: UIViewController?) {  
    15.       
    16. }  
    17. //已经移到父Controller  
    18. func didMoveToParentViewController(parent: UIViewController?) {  
    19.       
    20. }  
    21. //触发子ViewController的viewWillAppear  
    22. func beginAppearanceTransition(isAppearing: Bool, animated: Bool) {  
    23.       
    24. }  
    25. //触发childd的viewDidAppear这些方法  
    26. func endAppearanceTransition() {  
    27.       
    28. }  
    29. //child ViewController的作为状态栏  
    30. func childViewControllerForStatusBarStyle() -> UIViewController? {  
    31.     return nil;  
    32. }  
    33. //child ViewController的状态栏是否隐藏设置状态栏  
    34. func childViewControllerForStatusBarHidden() -> UIViewController? {  
    35.     return nil;  
    36. }  

    恢复相关

    [objc] view plain copy
     
     
    1. restorationIdentifier 恢复标示  
    2. restorationClass      恢复的类  
    3. override func encodeRestorableStateWithCoder(coder: NSCoder) {  
    4.           
    5. }  
    6. override func decodeRestorableStateWithCoder(coder: NSCoder) {  
    7.           
    8. }  
    9. applicationFinishedRestoringState() 恢复完成  

    获得其他的ViewController

    [objc] view plain copy
     
     
    1. println("parentViewController=(self.parentViewController)")            //父类Controller  
    2. println("presentedViewController=(self.presentedViewController)")      //Controller模式跳转到去Controller或父容器  
    3. println("presentingViewController=(self.presentingViewController)")    //Controller模式跳转来自于Controller或父容器  
    4. //        self.navigationController  
    5. //        self.tabBarController  
    6. //        self.presentationController  
    7. //        self.splitViewController  
    8. //        self.popoverPresentationController  
    
    

    StatusBar相关

    [objc] view plain copy
     
     
    1. //如果展示不是.FullScreen, 那么设置是不是捕获statusBar的样式,适合iPad  
    2. viewController.modalPresentationCapturesStatusBarAppearance = true  
    3. //child ViewController的作为状态栏  
    4. func childViewControllerForStatusBarStyle() -> UIViewController? {  
    5.     return nil;  
    6. }  
    7. //child ViewController的状态栏是否隐藏设置状态栏  
    8. func childViewControllerForStatusBarHidden() -> UIViewController? {  
    9.     return nil;  
    10. }  
    11. //设置当前ViewController的StatusBar的样式  
    12. override func preferredStatusBarStyle() -> UIStatusBarStyle {  
    13.     return .Default  
    14. }  
    15. //隐藏还是展示statusBar  
    16. override func prefersStatusBarHidden() -> Bool {  
    17.     return true  
    18. }  
    19. //statusBar的改变动画  
    20. override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {  
    21.     return .Fade  
    22. }  
    23. //当statusBar的状态改变后需要调用刷新  
    24. //    setNeedsStatusBarAppearanceUpdate()  

    Navigation相关

    [objc] view plain copy
     
     
    1. override func setToolbarItems(toolbarItems: [AnyObject]?, animated: Bool) {  
    2.           
    3. }  
    4. self.navigationItem  
    5. self.editButtonItem()  
    6. hidesBottomBarWhenPushed = true  
    7. self.toolbarItems = nil  

    TabBar相关

    [objc] view plain copy
     
     
    1. self.toolbarItems  

    常量

    [objc] view plain copy
     
     
      1. UIModalTransitionStyle  
      2. Modal Presentation Styles  
      3. UIViewControllerHierarchyInconsistencyException  
      4. UIViewControllerShowDetailTargetDidChangeNotification 
  • 相关阅读:
    (转)iOS-Runtime知识点整理
    iOS开发--SQLite重要框架FMDB的使用
    iOS开发--数据库管理CoreData的使用
    iOS超全开源框架、项目和学习资料汇总--数据库、缓存处理、图像浏览、摄像照相视频音频篇
    【导航条滚动透明】一个分类搞定
    成熟的程序员应该掌握的4个知识点
    iOS开发之浅谈MVVM的架构设计与团队协作
    Leetcode-One Edit Distance
    Leetcode-Read N Characters Given Read4 II
    Leetcode-Read N Characters Given Read4
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/10311594.html
Copyright © 2011-2022 走看看