pushViewController does no action - ios

i have a tableview and i want to go to another vc when one of rows tapped. my didSelectRowAtIndexPath function is as below. print command works and shows the right clicked row. but when i use self.navigationController?.pushViewController it does not go to vc with playVideo storyBoardId. after changing it to presentViewController it works. what's wrong about my code that push doesn't work?
thanks
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
print("clicked " + String(indexPath.row) )
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("playVideo") as! PlayVideoViewController
vc.id = self.video[indexPath.row].id
// Present View as Modal
//presentViewController(vc as UIViewController, animated: true, completion: nil)
// Push View
//self.navigationController?.pushViewController(vc, animated: true)
}

Its because the view controller you are currently in may not have a navigation controller.
You cannot push a view controller without having a UINavigationController.
If your requirement is to push a view controller, then perform the following steps.
Embed a navigation controller for the current view controller.(Open storyboard -> Choose your view controller -> Choose "Editor" -> "Embed in" -> UINavigationController)
Now your code
self.navigationController?.pushViewController(vc, animated: true)
will be working fine.

Make sure that your ViewController is indeed built on a NavigationController. Try forcing the line:
self.navigationController!.pushViewController
if it crashes, you know there is not nav set up. Or you could just check in your storyboard but this is a quick way to tell.

I dont know why are you doing it like this. I think its unnecessarily complicated.
This should by your function didSelectRowAtIndexPath:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("playVideo", sender: tableView)
}
Then you can specify as much segues as you want in this function:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
if (segue.identifier == "playVideo") {
let indexPath:NSIndexPath = tableView.indexPathForSelectedRow!
let PlayVideoViewController = segue.destinationViewController as! PlayVideoViewController
//you can pass parameters like project id
detailVC.projectID = ids[indexPath.row]
}
}
Your segue has to be named like this in interface builder:

Related

How to perform a segue from a pushed vc that has the segue defined in the storyboard

Here is the thing: my storyboard as a SettingViewController (after named SVC) and a StationsViewcontroller from which the latter is wired to a details view controller by the "openDetails" segue.
For practical reasons, my SVC can push several levels of specifics settings view controllers, which I did not defined in the storyboard.
At one point, one of those vc pushes the StationsViewController, which then can issue a performSegue to the detail: unfortunately, I get the "openDetails" segue is not defined.
What should I do to fix the issue?
--
The code has nothing peculiar, in the SettingsViewController, I push others VC, including the StationsViewController:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// Gracefully remove the selection
tableView.deselectRow(at: indexPath, animated: true)
guard let section = MainSections(rawValue: indexPath.section) else { return }
switch section {
case .stations:
let vc = StationsViewController()
// Do no segue at dismiss
vc.onlyDismiss = true
vc.didDismiss = {
self.navigationController?.popViewController(animated: true)
}
push(vc, hideNavigation: true)
// Blah blah
And in the StationsViewController where I want to get to the details vc, I perform the segue:
func openPoiDetails(_ gs: GasStation) {
poiDetails = gs
performSegue(withIdentifier: "poiDetails", sender: nil)
// Blah blah
The storyboard is as follows, and we can see the StationsViewController on the left is wired to the details vc via the "openDetails" segue:
The problem is that your StationsViewController was just created from its initializer and the storyboard knows nothing about it. Therefore, you can't use the segue. Instead, you need to ask the storyboard to instantiate the StationsViewController:
Replace:
let vc = StationsViewController()
with:
let vc = self.storyboard?.instantiateViewController(withIdentifier: "StationsViewController") as! StationsViewController
Make sure you set the "StationsViewController" as the Storyboard ID in the Identify Inspector in Xcode.

PageMenu pod presents Modal segue instead of Push segue

This question relates to:
SWIFT: Push segue is resulting in a modal segue instead
'Show' segue in Xcode 6 presents the viewcontroller as a modal in iOS 7
I understand this question might be very similar to others. But I have been unable to use some of the answers to solve my issue.
Here is how my storyboard looks:
The viewController has a segmentControl that controls two viewControllers. I now want to segue to the DetailViewController, but it is appearing as modal segue which hides the tabBar and navigationBar.
I have tried deleting and recreating the segue as the some off the answers have suggested but it doesn't solve anything. Is there anything someone could suggest me or direct me to?
Edit:
After testing out the demo that the pod provides I was able to outline the issue I am struggling with. I have implemented the same methods in which it is practically identical. The only difference is that my method for this PageMenu does not use nib files like the demo has done.
In my tableView delegate I am trying to pass a recipe data to the DetailView. This is how my prepareForSegue and didSelect looks:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "detail", sender: recipe)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "detail" {
let vc = segue.destination as! DetailViewController
let indexPath = tableView.indexPathForSelectedRow!
vc.recipe = RecipeManager.shared.recipes[indexPath.row]
}
}
Here is the demo's didSelect:
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let newVC : UIViewController = UIViewController()
newVC.view.backgroundColor = UIColor.white
newVC.title = "Favorites"
parentNavigationController!.pushViewController(newVC, animated: true)
}
When comparing it with the demo I am struggling to understand where to implement the parentNavigationController!.pushViewController(newVC, animated: true) which I believe will solve my issue.
Assuming you implemented the parentNavigationController as they did in the demo, you are almost all set.
Delete your existing Segue - you won't be using it.
Give your DetailViewController a Storyboard ID - such as "DetailVC"
Change your code to instantiate the DetailVC and push it onto the parent Nav Controller
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if let vc = self.storyboard?.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
vc.recipe = RecipeManager.shared.recipes[indexPath.row]
parentNavigationController!.pushViewController(vc, animated: true)
}
}

Tap on cell of UITableView and the other view doesn't appear

I have a tableView with some results of a search. I have connected the first and the second view with the "storyboard Segue" from the cell to the second view.
This is the code written in the first ViewController:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let indexPath = self.tableProva.indexPathForSelectedRow?.row
let vc = segue.destination as! DettaglioNewViewController
vc.titleLibro = posts[indexPath!].name
vc.authorLibro = posts[indexPath!].author
vc.ISBNLibro = posts[indexPath!].ISBN
}
In other project it works perfectly, but in this project it doesn't work, to show the second viewController I have to tap and swipe to the right or left.
It seems that the problem is graphic.
If I try to click on the cell does not become gray. turns gray if I hold down long on it
Has this happened to anyone else?
Can anyone help me?
You should use the 'didSelectRowAtIndexPath' method in order to segue to a different view for a specific cell. Use.....
var storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: NSBundle(forClass: self.dynamicType))
vc = storyboard.instantiateViewControllerWithIdentifier("LoginVC") as DettaglioNewViewController
vc.loadView()
..... To segue needed information.
* Information Example *
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath)
{
myFavIndex = indexPath.row
let storyBoard = UIStoryboard(name: "Main", bundle:nil)
let view = storyBoard.instantiateViewControllerWithIdentifier("LoginVC") as! DettaglioNewViewController
view.imageURL = self.imageArray[mySelectedIndex]
view.titleString = self.titleArray[mySelectedIndex]
self.presentViewController(view, animated: true, completion: nil)
}
In the above example I'm using the new View Controllers image and title as view.imageURL and view.titleString to populate that views elements from my selected Cells Information via [mySelectedIndex]. You should be able to do this with an Int, UIImage, String, e.g.
You don't need to initialize your controller from the code. Just create a custom segue and call 'performSegue' method in 'didSelect' method of table delegate.
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.performSegueWithIdentifier("yourIdentifierInStoryboardForCustomSegue", sender: nil)
}

How to push segue from cell click in table view to another view controller [duplicate]

This question already has answers here:
How to make a push segue when a UITableViewCell is selected
(10 answers)
Closed 6 years ago.
I have one table view controller and by clicking the cell i am redirecting the user to detail view controller.But when i perform segue " present view controller" its working fine.But what i need is?
I need to perform push segue by clicking the table view cell.How to do that?
Here is my code:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: BusinessDetailViewController = storyboard.instantiateViewControllerWithIdentifier("BusinessDetailViewController") as! BusinessDetailViewController
vc.BusinessData = arrDict[indexPath.row]
self.presentViewController(vc, animated: true, completion: nil)
}
I have tried this below line :
self.navigationController?.pushViewController(vc, animated: true)
But it doesn't work.Any Solution Please !!
First of all your UITableViewController must have a UINavigationController
To do it rapidly embedded it to your table from the xCode menu
:
Then , you must create the segue, ctrl-drag from the UITableViewController to the destination viewController:
Then select the segue and give it an identifier in the property inspector:
Now you are able to perform this segue in code:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
performSegueWithIdentifier("mySegue", sender: cell)
}
If you are not using UINavigationController than add a segue in storyboard from table cell to destination viewController and give a identifier to segue.
then on cell click call
- performSegueWithIdentifier:sender:
and implement
- prepareForSegue:sender:
Without a navigation controller it's not possible to push a controller. Your self.navigationController property is nil (you can check it)
How to solve it?
A :You need to embed your view controller in navigation controller either via code or via storyboard
try this code, its working for me,
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewControllerWithIdentifier("PlayViewController") as! PlayViewController
self.navigationController?.pushViewController(controller, animated: true)
}
and set the storyboard Id, example given below,
hope its helpful

Missing back button in my ViewController after seque from TableViewControler in Swift

In my project, I have a few View Controllers and I use UINavigationController.
When I make a seque from UIViewController to UITableViewController I have "back" button added automatically, but when I make seque from UITableViewController to UIContentViewController tapped a cell, I don't have a "back" button in UIContentViewController.
Plan of my app is here
The segue in UITableViewController looks like that:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let CV = self.storyboard?.instantiateViewControllerWithIdentifier("ContentViewController") as! ContentViewController
let tnumber = tableView.indexPathForSelectedRow?.item
CV.titleContent = daneOWpisach?[tnumber!]["title"]
CV.webContent = daneOWpisach?[tnumber!]["description"]
CV.category = category
self.presentViewController(CV, animated: true, completion: nil)
}
What should I do, if I would like to have a back button in UIContentViewController added automatically?
In your code you are presenting the view controller on cell selection..! Back button will show only if you perform push not on presenting the view.
In the above diagram what I have seen is you already make the segue from UITableViewController to UIContentViewController. select the segue and go to attribute inspector give the identifier for the segue.
On the cell selection perform the segue action with the identifier you specified
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("Your Segue name", sender: sender)
}
If you want to pass data to destination view controller, do it like below..
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
if segue.identifier == "Your Segue name" {
let CV = segue.destinationViewController as! ContentViewController
let tnumber = tableView.indexPathForSelectedRow?.item
CV.titleContent = daneOWpisach?[tnumber!]["title"]
CV.webContent = daneOWpisach?[tnumber!]["description"]
CV.category = category
}
}
Hope this helps you

Resources