简而言之,我正在展示一个来自 childVC 的视图,该视图刚刚添加到视图层次结构中,但显然它还没有添加到窗口层次结构中。
我刚刚开始这个项目并将其设置为基于页面的项目。UIPageViewController
添加到包含的锅炉RootViewController
是:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
// Configure the page view controller and add it as a child view controller.
self.pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
self.pageViewController!.delegate = self
let startingViewController: CameraViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!
let viewControllers = [startingViewController]
self.pageViewController!.setViewControllers(viewControllers, direction: .Forward, animated: false, completion: {done in })
self.pageViewController!.dataSource = self.modelController
self.addChildViewController(self.pageViewController!)
self.view.addSubview(self.pageViewController!.view) // !! -- viewDidAppear for CameraViewController called here
// Set the page view controller's bounds using an inset rect so that self's view is visible around the edges of the pages.
var pageViewRect = self.view.bounds
if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
pageViewRect = CGRectInset(pageViewRect, 40.0, 40.0)
}
self.pageViewController!.view.frame = pageViewRect
self.pageViewController!.didMoveToParentViewController(self)
// Add the page view controller's gesture recognizers to the book view controller's view so that the gestures are started more easily.
self.view.gestureRecognizers = self.pageViewController!.gestureRecognizers
}
我更改的唯一行是将我自己的 VC 设置为startingViewController
:
let startingViewController: CameraViewController = self.modelController.viewControllerAtIndex(0, storyboard: self.storyboard!)!
我试图让第一个屏幕成为相机 - ImagePickerVC。这是我的代码。如您所见,我在其中添加了一个UIImageView
以CamerViewController
显示预览,但我试图首先呈现UIImagePickerController
on viewDidAppear
:
import UIKit
class CameraViewController: UIViewController, UINavigationControllerDelegate, UIImagePickerControllerDelegate {
@IBOutlet var imageView: UIImageView!
@IBAction func didTapCameraButton(sender: AnyObject) {
presentImagePicker();
}
var dataObject: AnyObject?
var imagePicker: UIImagePickerController!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated);
presentImagePicker();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func presentImagePicker() {
imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = .Camera
presentViewController(imagePicker, animated: false, completion: { () -> Void in
println("save bolean")
})
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
imageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
}
}
但是,我收到了警告,并且imagePicker
从未出现过:
Warning: Attempt to present <UIImagePickerController: 0x14e833a00> on <Here.CameraViewController: 0x14e614af0> whose view is not in the window hierarchy!
我设置了断点,发现添加到时在这一行上调用viewDidAppear
了:CameraViewController
PageViewController
RootViewController
self.view.addSubview(self.pageViewController!.view) // !! -- viewDidAppear for CameraViewController called here
这似乎应该是,但为什么会出错?该视图刚刚添加,因此它位于 RootViewController 的视图层次结构中。
我还添加了一个按钮来UIImagePickerController
手动显示CameraViewController
. 这表明它没有任何问题。