简体   繁体   English

在另一个情节提要中快速显示viewController

[英]present viewController in another storyboard swift

i've been struggling with for sometime. 我已经挣扎了一段时间。 I'm trying to present a viewController modally, but everytime i do this i get following error: 我试图以模态形式呈现viewController,但是每次这样做时,都会出现以下错误:

libswiftCore.dylib`swift_dynamicCastClassUnconditional:
0x10f7d57d0:  pushq  %rbp
0x10f7d57d1:  movq   %rsp, %rbp
0x10f7d57d4:  testq  %rdi, %rdi
0x10f7d57d7:  je     0x10f7d580e               ; swift_dynamicCastClassUnconditional + 62
0x10f7d57d9:  movabsq $-0x7fffffffffffffff, %rax
0x10f7d57e3:  testq  %rax, %rdi
0x10f7d57e6:  jne    0x10f7d580e               ; swift_dynamicCastClassUnconditional + 62
0x10f7d57e8:  movq   0xae741(%rip), %rax       ; swift::ISAMask
0x10f7d57ef:  andq   (%rdi), %rax
0x10f7d57f2:  nopw   %cs:(%rax,%rax)
0x10f7d5800:  cmpq   %rsi, %rax
0x10f7d5803:  je     0x10f7d581d               ; swift_dynamicCastClassUnconditional + 77
0x10f7d5805:  movq   0x8(%rax), %rax
0x10f7d5809:  testq  %rax, %rax
0x10f7d580c:  jne    0x10f7d5800               ; swift_dynamicCastClassUnconditional + 48
0x10f7d580e:  leaq   0x332fd(%rip), %rax       ; "Swift dynamic cast failed"
0x10f7d5815:  movq   %rax, 0xae5cc(%rip)       ; gCRAnnotations + 8
0x10f7d581c:  int3   
0x10f7d581d:  movq   %rdi, %rax
0x10f7d5820:  popq   %rbp
0x10f7d5821:  retq   
0x10f7d5822:  nopw   %cs:(%rax,%rax)

Which is on following line: 在下面的行中:

let showItemVc = showItemStoryboard.instantiateViewControllerWithIdentifier("ShowItemViewController") as ShowItemViewController

didSelectRowAtIndexPath didSelectRowAtIndexPath

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    if segmentedControl?.selectedSegmentIndex == 0 {


        let showItemStoryboard = UIStoryboard(name: "ShowItemStoryboard", bundle: nil)
        let showItemVc = showItemStoryboard.instantiateViewControllerWithIdentifier("ShowItemViewController") as ShowItemViewController

        self.presentViewController(showItemVc, animated: true, completion: nil)
    }
}

It works if i just change 如果我改变就可以了

as ShowItemViewController

to

as UIViewController

But then i cant push values to the ShowItemViewController, what is the issue here? 但是然后我不能将值推送到ShowItemViewController,这里的问题是什么?

showItemViewController showItemViewController

import UIKit

class ShowItemViewController: UIViewController, UITableViewDelegate {

    @IBOutlet var pageControl: UIPageControl?
    @IBOutlet var tableView:UITableView?
    var userId: NSString?
    var itemId: NSString?

    override func viewDidLoad() {
        super.viewDidLoad()

        var no = (UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("itemCancel:")))

        self.navigationItem.leftBarButtonItem = no


        var hiddenButton = (UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Add, target: nil, action: nil))

        self.navigationItem.rightBarButtonItem = hiddenButton


        println(userId)


        self.tableView?.registerNib(UINib(nibName: "ImagePageViewCell", bundle: nil), forCellReuseIdentifier: "ImagePageViewCell")

        pageControl = UIPageControl()
        pageControl?.frame = CGRectZero
        pageControl?.currentPage = 0
        pageControl?.numberOfPages = 3
        pageControl?.tintColor = UIColor.whiteColor()
        pageControl?.userInteractionEnabled = false
        self.navigationItem.titleView = pageControl?




    }

}

If you're instantiating a UINavigationController that has your ShowItemViewController inside it, you need a reference to that view controller. 如果要实例化其中包含ShowItemViewControllerUINavigationController ,则需要对该视图控制器的引用。 UINavigationController has a topViewController property that represents the first child item that's added to it. UINavigationController具有topViewController属性,该属性表示添加到其中的第一个子项。 This is the item you need if you want to populate data in the ShowItemViewController , and it's embedded in a navigation controller. 如果要在ShowItemViewController填充数据,则需要此项目,并将其嵌入在导航控制器中。

let showItemStoryboard = UIStoryboard(name: "ShowItemStoryboard", bundle: nil)
let showItemNavController = showItemStoryboard.instantiateViewControllerWithIdentifier("ShowItemViewController") as UINavigationController
let showItemVC = showItemNavController.topViewController as ShowItemViewController
// Set the properties in your showItemVC
presentViewController(showItemNavController, animated: true, completion: nil)

You should also check that the let s are actually successful by doing the if let XXX = YYY as? ZZZ {} 您还应该通过执行if let XXX = YYY as? ZZZ {}检查let实际上是否成功if let XXX = YYY as? ZZZ {} if let XXX = YYY as? ZZZ {} dance before proceeding, or you'll get a runtime error. 在继续操作之前if let XXX = YYY as? ZZZ {}跳舞,否则会出现运行时错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM