简体   繁体   中英

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:

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

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

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. UINavigationController has a topViewController property that represents the first child item that's added to it. This is the item you need if you want to populate data in the ShowItemViewController , and it's embedded in a navigation controller.

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 {} dance before proceeding, or you'll get a runtime error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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