如何以编程方式添加UI /视图控制器?

时间:2015-04-26 23:54:18

标签: ios swift viewcontroller

我正在尝试在点击UIView时加载ViewController,但我一直收到错误

  

***由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:'Storyboard()不包含带有标识符'CameraVC3'的视图控制器<'

我认为没有正确地将ViewController添加到我的故事板中,所以我试图以编程方式进行,但它仍然没有被识别。

的ViewController

class ViewController: UIViewController, UIScrollViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate{

    let rect1 = CGRectMake(100, 60, 40, 60)
    let captureButton2 = UIView(frame: rect1)
    captureButton2.backgroundColor = UIColor.blueColor()
    scrollView.addSubview(captureButton2)
    captureButton2.addGestureRecognizer(UITapGestureRecognizer(target: self,
       action: Selector("didTapImageView:")))

    func didTapImageView(tap: UITapGestureRecognizer){

         let captureDetails = storyboard!.instantiateViewControllerWithIdentifier("CameraVC3")! as! CameraVC3
         presentViewController(captureDetails, animated: true, completion: nil)

}

CameraViewController

class CameraVC3: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate{

    override func viewWillAppear(animated: Bool) {
    println("camera vc3 view will appear")
    super.viewWillAppear(animated)

    view.addGestureRecognizer(UITapGestureRecognizer(target: self, action: "capture:"))
}

func capture() {
     if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
    var imagePicker = UIImagePickerController()
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.Camera;
    imagePicker.mediaTypes = [kUTTypeImage]
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:2)

为了使用UIStoryboard的{​​{1}}方法,您必须设置一个视图控制器的标识符,以匹配您作为标识符传递的字符串。

故事板上的视图控制器可以通过身份检查器设置其标识符(与为视图控制器设置自定义类的位置相同):

enter image description here

在“故事板ID”框中,只需填写您要用作此视图控制器标识的字符串。在这种情况下,请将instantiateViewControllerWithIdentifier放在该框中。

答案 1 :(得分:1)

使用以下行:

<!DOCTYPE html>
<html>
<head>
    <title>Testing</title>
    <meta charset="utf-8">

    <!-- Kendo sources -->
    <link href="kendo/styles/kendo.common.min.css" rel="stylesheet">
    <link href="kendo/styles/kendo.default.min.css" rel="stylesheet">
    <!--<link href="kendo/styles/kendo.mobile.all.css" rel="stylesheet" />-->
    <!--<link href="kendo/styles/kendo.dataviz.mobile.min.css" rel="stylesheet">-->

    <script src="kendo/js/jquery.min.js"></script>
    <script src="kendo/js/kendo.ui.core.min.js"></script>

    <!-- Source -->
    <link rel="stylesheet" href="src/stylesheet.css" />

</head>
<body>
    <!-- Home -->
    <div data-role="view" id="home" data-title="Home">
        <div data-role="header">
            <div data-role="tabstrip">
                <span data-role="view-title"></span>
            </div>
        </div>
        <div data-role="footer">
            <div data-role="tabstrip">
                <a data-role="button" href="#menupage">Menu</a>
            </div>
        </div>
    </div>
    <script>var app = new kendo.mobile.Application(document.body);</script>
</body>
</html>

您需要在storyboard中为ViewController指定一个标识符名称“CameraVC3”。您可以在身份检查器的let captureDetails = storyboard!.instantiateViewControllerWithIdentifier("CameraVC3")! as! CameraVC3 字段中设置此项。