UIViewController详解--Swift版本

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

UIViewController类详解:

通过Nib文件初始化

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

 
StoryBoard相关 
 

copy

    println("storyboard=\(self.storyboard)")//storyboard<prename="code"class="objc">//在跳转之前对Segue进行判断,如果返回false则不之行这个Segue的跳转,performSegueWithIdentifier:sender:如果使用了,则这个方法无效
  1. overridefuncshouldPerformSegueWithIdentifier(identifier:String?,sender:AnyObject?)->Bool{
  2. returntrue
  3. }
  4. //跳转执行
  5. overridefuncprepareForSegue(segue:UIStoryboardSegue,0); background-color:inherit">sender:AnyObject?){
  6. print("prepareForSegue")
  7. //根据UIStoryBoarSegue的Identifier进行跳转
  8. overridefuncperformSegueWithIdentifier(identifier:String?,153); font-weight:bold; background-color:inherit">super.performSegueWithIdentifier(identifier!,sender:sender)
  9. }
copy
    //subViewController是否能够执行UnwindSegue
  1. overridefunccanPerformUnwindSegueAction(action:Selector,0); background-color:inherit">fromViewController:UIViewController,withSendersender:AnyObject)->Bool{
  2. }
copy
    //如果执行UnwindSegue,就返回Segue
  1. overridefuncsegueForUnwindingToViewController(toViewController:UIViewController,0); background-color:inherit">identifier:String?)->UIStoryboardSegue{
  2. copy
    //能够执行Segue的Controller
  1. funcviewControllerForUnwindSegueAction(action:Selector,0); background-color:inherit">sender:AnyObject?)->UIViewController?{
  2. }
 

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

copy

    println("view=\(view)")
  1. println("viewisloaded=\(isViewLoaded())")
  2. title="ViewController"<prename="code"class="objc">//如果不是nib文件初始化而来,初始化的时候需要调用这个方法初始化view,此方法不能主动调用,是系统调用的<prename="code"class="objc">overridefuncloadView(){
  3. super.loadView()<prename="code"class="objc">}//view初始化以后调用
copy
    overridefuncviewDidLoad(){
copy
    super.viewDidLoad()<spanstyle="font-family:Arial,sans-serif;">//view将可见的时候调用</span>
  1. }<prename="code"class="objc">overridefuncviewWillAppear(animated:Bool){
  2. super.viewWillAppear(animated)
  3. //view变得完全可见了以后执行
  4. overridefuncviewDidAppear(animated:Bool){
  5. super.viewDidAppear(animated)
  6. }
  7. //view被遮挡或者隐藏时调用
  8. overridefuncviewWillDisappear(animated:Bool){
  9. super.viewWillDisappear(animated)
  10. //view被遮挡或者隐藏后调用
  11. overridefuncviewDidDisappear(animated:Bool){
  12. super.viewDidDisappear(animated)
  13. 模式跳转

    copy

      //设置模式跳转的类别,但是必须是目的Controller设置,不能是上级设置
    1. //CoverVertical,FlipHorizontal,CrossDissolve,PartialCurl四种类型
    2. viewController.modalTransitionStyle=.FlipHorizontal
    3. //设置模式展示样式,适合于iPad上
    4. viewController.modalPresentationStyle=.FullScreen
    5. //如果展示不是.FullScreen,那么设置是不是捕获statusBar的样式,适合iPad
    6. viewController.modalPresentationCapturesStatusBarAppearance=//判断在模式跳转时消失是否键盘
    7. viewController.disablesAutomaticKeyboardDismissal()
    8. presentViewController(viewController,0); background-color:inherit">animated:true){()->Voidin
    9. //跳转到下个界面
    10. dismissViewControllerAnimated(true,0); background-color:inherit">completion:{()->Voidin
    11. //回复模式跳转
    12. })
    配置View的layout
    copy
      //layoutSubviews方法调用之前
    1. overridefuncviewWillLayoutSubviews(){
    2. super.viewWillLayoutSubviews()
    3. //layoutSubviews方法调用之后
    4. overridefuncviewDidLayoutSubviews(){
    5. super.viewDidLayoutSubviews()
    6. class="objc">
    updateViewConstraints()
     
    
    copy
      //延伸的方向--setwhichsidesofyourviewcanbeextendedtocoverthewholescreen.
    1. ifself.respondsToSelector(Selector("edgesForExtendedLayout")){
    2. self.edgesForExtendedLayout=.None
    3. //Scrollview滚动时处于全屏,默认YES
    4. self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets")){
    5. self.automaticallyAdjustsScrollViewInsets=//当statusbar是透明时,是否扩展至StatusBar,默认情况下是NO,且statusbar不是透明的
    6. self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars")){
    7. self.extendedLayoutIncludesOpaqueBars=false
    8. //控制view的大小UIPopoverController用的比较的广泛
    9. self.preferredContentSize=self.view.bounds.size

跳转相关
copy
    isBeingPresented()//是否在展示
  1. isBeingDismissed()//是否在dismiss
  2. isMovingToParentViewController()
  3. isMovingFromParentViewController()
旋转相关
copy
    //是否需要旋转
  1. overridefuncshouldAutorotate()->Bool{
  2. //支持的方向
  3. overridefuncsupportedInterfaceOrientations()->Int{
  4. return2
  5. //优先支持的方向
  6. overridefuncpreferredInterfaceOrientationForPresentation()->UIInterfaceOrientation{
  7. return.Portrait
  8. }
自定义的ViewController Container copy
    //https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
  1. //addChildVIewController:调用这个方法指明一个视图控制器作为你的子视图。
  2. funcaddChildViewController(childController:UIViewController){
  3. //调用这个方法将一个视图控制器从你的子视图列表里移除。
  4. funcremoveFromParentViewController(){
  5. //这是一个使用一个唯一可选的视图替换另一个视图的新方法,或者移动一个子视图到前台来。通过使用这个方法,这个视图控制器的生命周期信息会被正确地发送出去functransitionFromViewController(fromViewController:UIViewController,toViewController:UIViewController,duration:NSTimeInterval,options:UIViewAnimationOptions,animations:()->Void,completion:((Bool)->Void)?){
  6. //将要移到父Controller
  7. funcwillMoveToParentViewController(parent:UIViewController?){
  8. //已经移到父Controller
  9. funcdidMoveToParentViewController(parent:UIViewController?){
  10. //触发子ViewController的viewWillAppear
  11. funcbeginAppearanceTransition(isAppearing:Bool,0); background-color:inherit">animated:Bool){
  12. //触发childd的viewDidAppear这些方法
  13. funcendAppearanceTransition(){
  14. //childViewController的作为状态栏
  15. funcchildViewControllerForStatusBarStyle()->UIViewController?{
  16. returnnil;
  17. //childViewController的状态栏是否隐藏设置状态栏
  18. funcchildViewControllerForStatusBarHidden()->UIViewController?{
  19. 恢复相关
    copy
      restorationIdentifier恢复标示
    1. restorationClass恢复的类
    2. overridefuncencodeRestorableStateWithCoder(coder:NSCoder){
    3. overridefuncdecodeRestorableStateWithCoder(coder:NSCoder){
    4. applicationFinishedRestoringState()恢复完成
    获得其他的ViewController
    copy
      println("parentViewController=\(self.parentViewController)")//父类Controller
    1. println("presentedViewController=\(self.presentedViewController)")//Controller模式跳转到去Controller或父容器
    2. println("presentingViewController=\(self.presentingViewController)")//Controller模式跳转来自于Controller或父容器
    3. //self.navigationController
    4. //self.tabBarController
    5. //self.presentationController
    6. //self.splitViewController
    7. //self.popoverPresentationController

    StatusBar相关

    copy
    1. viewController.modalPresentationCapturesStatusBarAppearance=true
    2. //设置当前ViewController的StatusBar的样式
    3. overridefuncpreferredStatusBarStyle()->UIStatusBarStyle{
    4. return.Default
    5. //隐藏还是展示statusBar
    6. overridefuncprefeRSStatusBarHidden()->Bool{
    7. //statusBar的改变动画
    8. overridefuncpreferredStatusBarUpdateAnimation()->UIStatusBarAnimation{
    9. return.Fade
    10. //当statusBar的状态改变后需要调用刷新
    11. //setNeedsStatusBarAppearanceUpdate()
    Navigation相关
    copy
      overridefuncsetToolbarItems(toolbarItems:[AnyObject]?,0); background-color:inherit">animated:Bool){
    1. self.navigationItem
    2. self.editButtonItem()
    3. hidesBottomBarWhenPushed=self.toolbarItems=nil
    TabBar相关
    copy
      self.toolbarItems
    常量
    copy
      UIModalTransitionStyle
    1. ModalPresentationStyles
    2. UIViewControllerHierarchyInconsistencyException
    3. UIViewControllerShowDetailTargetDidChangeNotification

    猜你在找的Swift相关文章