简体   繁体   中英

How to presentViewController without use Seque

I wanna present a collectionView after I do some search from the tableView and tap the cell.

I create a searchController which is also a tableViewController and implement the updateSearchResultsForSearchController(), it works well and display the result I want.

Since storyboard hasn't got a searchController, I can't create a segue between the searchController and the collectionViewController.What I tried is

//PostListId is the storyboard id of postList(collectionview here)
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    // Choose the viewController I wanna display
    let postList = storyboard?.instantiateViewControllerWithIdentifier("PostListId") as! PostListController
    // Send data to collectionView 
    postList.key = 1
    // PresentViewController
    self.presentViewController(postList, animated: true, completion: nil)
}

But when I tap the cell after search, I got a error here, and no error messages feedback, Why?

Try something like this:

let postCtrl = PostListController() //create instance of your controller 
postCtrl.key = 1
let navigationController = UINavigationController(rootViewController: postCtrl) //create navigation controller if you want
pushViewController(navigationController, animated: true) //display

or you can present your controller as modal if you prefer:

presentViewController(postCtrl, animated: true, completion: nil)

Your code looks fine as of now. Make sure you set the storyboard id:

图片

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