UIViewController类详解:
通过Nib文件初始化
init(nibName nibName: String?, bundle nibBundle: NSBundle?)
println("nibName = \(self.nibName)") //nibName
println("nibBundle = \(self.nibBundle)") //nibBundle
StoryBoard相关
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)
}
//subViewController是否能够执行Unwind Segue
override func canPerformUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject) -> Bool {
}
//如果执行Unwind Segue,就返回Segue
override func segueForUnwindingToViewController(toViewController: UIViewController, fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue {
}
//能够执行Segue的Controller
func viewControllerForUnwindSegueAction(action: Selector, fromViewController: UIViewController, withSender sender: AnyObject?) -> UIViewController? {
}
Unwindsegue的实现原理请参考相关文章
View相关
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初始化以后调用
override func viewDidLoad() {
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
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,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
- UIModalTransitionStyle
- Modal Presentation Styles
- UIViewControllerHierarchyInconsistencyException
- UIViewControllerShowDetailTargetDidChangeNotification
天行健,君子以自强不息;地势坤,君子以厚德载物!