UIViewController类详解:
通过Nib文件初始化
[objc] view plain copy
- init(nibName nibName: String?, bundle nibBundle: NSBundle?)
- println("nibName = \(self.nibName)") //nibName
- println("nibBundle = \(self.nibBundle)") //nibBundle
StoryBoard相关
[objc] view plain copy
- println("storyboard = \(self.storyboard)") //storyboard<pre name="code" class="objc">//在跳转之前对Segue进行判断,如果返回false则不之行这个Segue的跳转, performSegueWithIdentifier:sender:如果使用了,则这个方法无效
- override func shouldPerformSegueWithIdentifier(identifier: String?, sender: AnyObject?) -> Bool {
- return true
- }
- //跳转执行
- override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
- print("prepareForSegue")
- }
- //根据UIStoryBoarSegue的Identifier进行跳转
- override func performSegueWithIdentifier(identifier: String?, sender: AnyObject?) {
- super.performSegueWithIdentifier(identifier!, sender: sender)
- }
[objc] view plain copy
- //subViewController是否能够执行Unwind Segue
- override func canPerformUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject) -> Bool {
- }
[objc] view plain copy
- //如果执行Unwind Segue,就返回Segue
- override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue {
- }
[objc] view plain copy
- //能够执行Segue的Controller
- func viewControllerForUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject?) -> UIViewController? {
- }
Unwindsegue的实现原理请参考相关文章
View相关
[objc] view plain copy
- println("view = \(view)")
- println("view is loaded = \(isViewLoaded())")
- title = "ViewController"<pre name="code" class="objc">//如果不是nib文件初始化而来,初始化的时候需要调用这个方法初始化view,此方法不能主动调用,是系统调用的<pre name="code" class="objc">override func loadView() {
- super.loadView()<pre name="code" class="objc">}//view初始化以后调用
[objc] view plain copy
- override func viewDidLoad() {
[objc] view plain copy
- super.viewDidLoad() <span style="font-family: Arial, Helvetica, sans-serif;">//view将可见的时候调用</span>
- } <pre name="code" class="objc">override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- }
- // view变得完全可见了以后执行
- override func viewDidAppear(animated: Bool) {
- super.viewDidAppear(animated)
- }
- //view被遮挡或者隐藏时调用
- override func viewWillDisappear(animated: Bool) {
- super.viewWillDisappear(animated)
- }
- //view被遮挡或者隐藏后调用
- override func viewDidDisappear(animated: Bool) {
- super.viewDidDisappear(animated)
- }
模式跳转
[objc] view plain copy
- //设置模式跳转的类别,但是必须是目的Controller设置,不能是上级设置
- //CoverVertical, FlipHorizontal, CrossDissolve, PartialCurl四种类型
- viewController.modalTransitionStyle = .FlipHorizontal
- //设置模式展示样式,适合于iPad上
- viewController.modalPresentationStyle = .FullScreen
- //如果展示不是.FullScreen, 那么设置是不是捕获statusBar的样式,适合iPad
- viewController.modalPresentationCapturesStatusBarAppearance = true
- //判断在模式跳转时消失是否键盘
- viewController.disablesAutomaticKeyboardDismissal()
- presentViewController(viewController, animated: true) { () -> Void in
- //跳转到下个界面
- }
- dismissViewControllerAnimated(true , completion: { () -> Void in
- //回复模式跳转
- })
配置View的layout
[objc] view plain copy
- // layoutSubviews方法调用之前
- override func viewWillLayoutSubviews() {
- super.viewWillLayoutSubviews()
- }
- // layoutSubviews方法调用之后
- override func viewDidLayoutSubviews() {
- super.viewDidLayoutSubviews()
- }<pre name="code" class="objc">
updateViewConstraints()
[objc] view plain copy
- //延伸的方向--set which sides of your view can be extended to cover the whole screen.
- if self.respondsToSelector(Selector("edgesForExtendedLayout")) {
- self.edgesForExtendedLayout = .None
- }
- //Scrollview滚动时处于全屏,默认YES
- if self.respondsToSelector(Selector("automaticallyAdjustsScrollViewInsets")) {
- self.automaticallyAdjustsScrollViewInsets = true
- }
- //当statusbar是透明时,是否扩展至StatusBar,默认情况下是NO,且statusbar不是透明的
- if self.respondsToSelector(Selector("extendedLayoutIncludesOpaqueBars")) {
- self.extendedLayoutIncludesOpaqueBars = false
- }
- //控制view的大小UIPopoverController用的比较的广泛
- self.preferredContentSize = self.view.bounds.size
跳转相关
[objc] view plain copy
- isBeingPresented() //是否在展示
- isBeingDismissed() //是否在dismiss
- isMovingToParentViewController()
- isMovingFromParentViewController()
旋转相关
[objc] view plain copy
- //是否需要旋转
- override func shouldAutorotate() -> Bool {
- return true
- }
- //支持的方向
- override func supportedInterfaceOrientations() -> Int {
- return 2
- }
- //优先支持的方向
- override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
- return .Portrait
- }
自定义的ViewController Container
[objc] view plain copy
- //https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomContainerViewControllers/CreatingCustomContainerViewControllers.html
- //addChildVIewController:调用这个方法指明一个视图控制器作为你的子视图。
- func addChildViewController(childController: UIViewController) {
- }
- //调用这个方法将一个视图控制器从你的子视图列表里移除。
- func removeFromParentViewController() {
- }
- //这是一个使用一个唯一可选的视图替换另一个视图的新方法,或者移动一个子视图到前台来。通过使用这个方法,这个视图控制器的生命周期信息会被正确地发送出去 func transitionFromViewController(fromViewController: UIViewController, toViewController: UIViewController, duration: NSTimeInterval, options: UIViewAnimationOptions, animations: () -> Void, completion: ((Bool) -> Void)?) {
- }
- //将要移到父Controller
- func willMoveToParentViewController(parent: UIViewController?) {
- }
- //已经移到父Controller
- func didMoveToParentViewController(parent: UIViewController?) {
- }
- //触发子ViewController的viewWillAppear
- func beginAppearanceTransition(isAppearing: Bool, animated: Bool) {
- }
- //触发childd的viewDidAppear这些方法
- func endAppearanceTransition() {
- }
- //child ViewController的作为状态栏
- func childViewControllerForStatusBarStyle() -> UIViewController? {
- return nil;
- }
- //child ViewController的状态栏是否隐藏设置状态栏
- func childViewControllerForStatusBarHidden() -> UIViewController? {
- return nil;
- }
恢复相关
[objc] view plain copy
- restorationIdentifier 恢复标示
- restorationClass 恢复的类
- override func encodeRestorableStateWithCoder(coder: NSCoder) {
- }
- override func decodeRestorableStateWithCoder(coder: NSCoder) {
- }
- applicationFinishedRestoringState() 恢复完成
获得其他的ViewController
[objc] view plain copy
- println("parentViewController=\(self.parentViewController)") //父类Controller
- println("presentedViewController=\(self.presentedViewController)") //Controller模式跳转到去Controller或父容器
- println("presentingViewController=\(self.presentingViewController)") //Controller模式跳转来自于Controller或父容器
- // self.navigationController
- // self.tabBarController
- // self.presentationController
- // self.splitViewController
- // self.popoverPresentationController
StatusBar相关
[objc] view plain copy
- //如果展示不是.FullScreen, 那么设置是不是捕获statusBar的样式,适合iPad
- viewController.modalPresentationCapturesStatusBarAppearance = true
- //child ViewController的作为状态栏
- func childViewControllerForStatusBarStyle() -> UIViewController? {
- return nil;
- }
- //child ViewController的状态栏是否隐藏设置状态栏
- func childViewControllerForStatusBarHidden() -> UIViewController? {
- return nil;
- }
- //设置当前ViewController的StatusBar的样式
- override func preferredStatusBarStyle() -> UIStatusBarStyle {
- return .Default
- }
- //隐藏还是展示statusBar
- override func prefersStatusBarHidden() -> Bool {
- return true
- }
- //statusBar的改变动画
- override func preferredStatusBarUpdateAnimation() -> UIStatusBarAnimation {
- return .Fade
- }
- //当statusBar的状态改变后需要调用刷新
- // setNeedsStatusBarAppearanceUpdate()
Navigation相关
[objc] view plain copy
- override func setToolbarItems(toolbarItems: [AnyObject]?, animated: Bool) {
- }
- self.navigationItem
- self.editButtonItem()
- hidesBottomBarWhenPushed = true
- self.toolbarItems = nil
TabBar相关
[objc] view plain copy
- self.toolbarItems
常量
[objc] view plain copy
- UIModalTransitionStyle
- Modal Presentation Styles
- UIViewControllerHierarchyInconsistencyException
- UIViewControllerShowDetailTargetDidChangeNotification
时间: 2024-11-09 00:28:45