pushViewController not working on iPad only - ios

I have a tab bar view controller with a view controller member, which is embedded in a navigation view controller. In this view controller, I have a tableview, with a list of items. When you click on an item, you are directed to another view controller. Here is the code of the row that is selected:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print([indexPath.row])
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let sleepViewController = storyBoard.instantiateViewController(withIdentifier: "SleepItemEnter") as! SleepEnterTimesViewController
sleepViewController.daySelected = sleepList[indexPath.row].dayOfWeek
sleepViewController.daySelectedInt = indexPath.row
self.navigationController?.pushViewController(sleepViewController, animated: true)
}
Now it has been working fine on all iPhones I tested. But now I tested it on iPad Pro and it doesn't work. The line "self.navigationController?.pushViewController" isn't working. The new view controller isn't showing at all.
Why would this be, only on iPad?

Related

How to open view tabbar controller from didSelectRowAt indexPath within storyboard in swift?

I have a storyboard with tabbarcontroller. One of tab bar has a tableview and I want that when the user tap in a row from tableview open a detail view with another tabbar. The problem is when I open detail view tab bar hides . how to show tabbar at didselectrowatindexpath?
The code in didselectrowatindexpath:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let DVC=storybaord.instantiateViewController(withIdentifier: "NewsViewController") as! NewsViewController
DVC.getImage=sneaker[indexPath.row]
DVC.getNews=News[indexPath.row]
self.navigationController?.pushViewController(DVC, animated: true)
}
I have created single view application.I have created DiscoveryNewsTableViewCell.I populated the table view cell with two arrays 1-sneaker 2- News array .I want when the particular array displayed ist tab of bar controller .But i did get these fields on first tab of tabbar controller .Here is the sample code for this https://drive.google.com/file/d/1uSYl-4a1UQXpMXkSQ_l8aij6EH_v2yna/view?usp=sharing
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// setting these values in local storage using user defaults
let currentObj = News[indexPath.row]
UserDefaults.standard.set(currentObj, forKey: "currentOb")
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let DVC=storybaord.instantiateViewController(withIdentifier: "NewsViewController") as! NewsViewController
self.navigationController?.pushViewController(DVC, animated: true)
}
This is happening because you are pushing the viewController instead of TabBarController.
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storybaord=UIStoryboard(name: "Main", bundle: nil)
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
let DVC = tabBar.viewControllers[0] as! NewsViewController
tabBar.selectedIndex = 0
DVC.getImage=sneaker[indexPath.row]
DVC.getNews=News[indexPath.row]
self.navigationController?.pushViewController(tabBar, animated: true)
}
In the above code replace 0 in
tabBar.viewControllers[0]
tabBar.selectedIndex = 0
with index of view controller you want to open.
And also in the line
let tabBar=storybaord.instantiateViewController(withIdentifier: "tabBar") as! UITabBarController
tabBar has to be set as storyboard identifier for UITabBarController that you want to display as shown below.

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)
}

pushViewController does no action

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:

pushViewController don't work when my tableView is in an TabBar

I have a TableViewController in a TabBar.
When I select one cell of my tableView, I want start a new controller with pushViewController(MyNewController).
This is my code :
In my TableView :
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myController = (storyboard.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController)!
myController.parameter = self.tableau[indexPath.row]
UINavigationController().pushViewController(myController, animated: true)
}
In my TabBar :
func viewDidLoad() {
super.viewDidLoad()
var controllerArray: [UIViewController] = []
let controller: UIViewController = ClubPreviewListViewController()
controller.title = "TEST"
controllerArray.append(controller)
self.view.addSubview(pageMenu!.view)
}
(I use CAPSPageMenu for customize my TabBar, but it's not the problem, I have the same problem without)
In my controller :
deinit {
print ("TEST")
}
When I select a cell, the log write "TEST" everytime I select but don't change the view.
I think it's my navigationController the problem, but I don't know how to fix it.
Before I implement the TabBar, I use my TableView alone, and the push did works.
Sorry for my english ! Thanks for your help.
EDIT:
I change my pushViewController :
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myController = (storyboard.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController)!
myController.parameter = self.tableau[indexPath.row]
//UINavigationController().pushViewController(myController, animated: true)
self.navigationController?.pushViewController(myController, animated: true)
}
Same reaction.
My NavigationController isn't directly link with my TabBar. I need to create an other one ? I don't really understand how NavigationController works !
This is my configuration
EDIT2:
If I use the navigationController of my TabBar and not of my TableView, the view change !
self.saveTabBarNavigationController.pushViewController(myController, animated: true)
You are creating an instance of a navigation controller on the line
UINavigationController().pushViewController(myController, animated: true)
Once this method finishes executing, the navigation controller and its view controllers will be deallocated because nothing retains your navigation controller. Causing 'TEST' to be printed in your deinit() function.
You should be obtaining the navigation controller that owns your current tab bar controller and pushing onto the stack using that. Without knowing the structure of your application it is difficult to know where your navigation controller is (if it exists).
Check your storyboard, but essentially try:
self.navigationController.pushViewController(myController, animated: true)
Try this it will work only if you have a navigation embedded tabbar controller
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let myController = (storyboard.instantiateViewControllerWithIdentifier("MyViewController") as? MyViewController)!
myController.parameter = self.tableau[indexPath.row]
navigationController.pushViewController(myController, animated: true)
}

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

Resources