I have CollectionViewController, when I am trying to click on cell and navigate to respective ViewControllers its not working.how can I solve this issue.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell:AddOptionCollectionViewCell = collectionView.cellForItem(at: indexPath) as! AddOptionCollectionViewCell
if (cell.name.text == "CONTRAST"){
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ContrastViewController") as! ContrastViewController
self.navigationController?.pushViewController(newViewController, animated: true)
}
I think this problem is due to nil value of navigation stack for your CollectionViewController class.
So, first of all go to storyboard and select CollectionViewController class and embed NavigationController into it. After this try and run it will work.
All the best.
In my case, I mistakenly added the MainViewController to the window.rootViewController instead of adding it to the UINavigationController and then using the navigation as rootViewController.
at SceneDelegate.swift, it should be:
let navigationController = UINavigationController()
navigationController.pushViewController(MainRouter.createModule(using: navigationController), animated: false)
window.rootViewController = navigationController
window.makeKeyAndVisible()
Related
I'm trying to push from viewcontroller with tableview to another view with tableview too,
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Clicke\(indexPath.row)")
guard let vc = storyboard?.instantiateViewController(withIdentifier: "SecAdsViewController") as? SecAdsViewController else { return }
vc.data = data[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
}
the code is looking good, storyboard id is correct too but it is not pushing when I click on the cell and no action, console response is good too
breakpoint has no issues!
my story board
As you mention in the comment the best solution is to create a new storyboard.
Try this
let storyboard = UIStoryboard(named: "Sections", bundle: "nil")
guard let vc = storyboard.instantiateViewController(withIdentifier: "SecAdsViewController") as? SecAdsViewController else { return }
vc.data = data[indexPath.row]
self.navigationController?.pushViewController(vc, animated: true)
This solution assumes you don't have a NavigationController in Sections storyboard, which is something you would not want in any case as you already have a NavigationController in the other storyboard.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let vc = storyboard?.instantiateViewController(withIdentifier: "vc1")as? DetailViewController
//vc?.image = img[indexPath.row]!
//vc?.name = imageArray[indexPath.row] storyboard
self.navigationController?.pushViewController(vc!, animated: true)
}
Error is:
Use of unresolved identifier 'storyboard'
Look into this:
// Case: When the next ViewController is existed in the same Storyboard.
let controller = self.storyboard?.instantiateViewController(withIdentifier: "ControllerIdentifier") as! YourViewController
// pass data here to the next controller.
self.navigationController?.pushViewController(controller, animated: true)
// Case: When the next ViewController is exists in other Storyboard.
let storyboard = UIStoryboard(name: "StoryboardName", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "ControllerIdentifier") as! YourViewController
// pass data here to the next controller.
self.navigationController?.pushViewController(controller, animated: true)
Note: As you are using this line 'withIdentifier: "vc1"', I would like to suggest you that always assign the Storyboard ID for a ViewController same as ViewController's class name. Make it as a habit, it will be helpful because you don't need to remember the Storyboard Id what you set. So keep the thing simple.
Check in the properties of DetailViewController in Main.storyboard and see if the storyboard id is set properly. According to your code it should be vc1
I am trying to navigate from ConversationsListVC (source) to ConversationVC (destination) programmatically by instantiating the source VC (self) as the root navigation controller VC and then instantiating the destination VC so that I can push navigate.
I get the print statements onto the console (see below) but the app does not navigate to the destination VC, the app doesn't crash or throw any error. what am I missing here?
debug 1
debug 2
function:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("debug 1")
var window: UIWindow!
guard let convoVC = storyboard?.instantiateViewController(withIdentifier: "ConversationVC") as? ConversationVC else { return }
window?.rootViewController = UINavigationController(rootViewController: self)
navigationController?.pushViewController(convoVC, animated: true)
print("debug 2")
}//end func
try this one maybe the window instance is not correctly.
let storyBoard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let newViewController = storyBoard.instantiateViewController(withIdentifier: "ConversationVC") as! ConversationVC
navigationController?.pushViewController(newViewController, animated: true)
and make sure your viewcontroller is assign with the Identifier
You can't just randomly create a new UINavigationController and push the destination onto it. You need to push the destination VC onto the UINavigationController in which self is currently embedded in.
If self is embedded in a UINavigationController already, do:
self.navigationController?.pushViewController(convoVC, animated: true)
If the source VC is not embedded in a UINavigationController, you need to do that in the storyboard.
Since you are using storyboards, you can also consider adding a push segue between the source and destination, and performing that segue:
self.performSegue(withIdentifier: "someIdentifier", sender: self)
I have a problem.
I need help. I have been looking for a solution for about 5 hours. Unfortunately without success.
My problem is that I have several storyboards and created without Segue.
I would love the selected Tableviewcell title pass to rootViewController.
Here is my code in my ModalViewTable:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
let indexNumber = "\(indexPath.row)"
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!
let storyboard = UIStoryboard(name: "addTime", bundle: nil)
var viewController = storyboard.instantiateViewController(withIdentifier: "addTimeVC") as! addTimeViewController
viewController.test = "TESTABCDEF"
passedValue = ("\(indexNumber)")
dismiss(animated: true, completion: nil)
}
I have a property test in my firstViewController. What is here wrong?
I've also tried with delegates and closures but without success. Please help me.
Try this instead:
let indexNumber = "\(indexPath.row)"
let indexPath = tableView.indexPathForSelectedRow
let currentCell = tableView.cellForRow(at: indexPath!) as UITableViewCell!
let viewController = navigationController!.presentingViewController as! addTimeViewController
viewController.test = "TESTABCDEF"
passedValue = ("\(indexNumber)")
navigationController!.dismiss(animated: true, completion: nil)
When you present a view controller modally using the present(_:animated) method, the view controller that was presented has the presentingViewController property set to the view controller that presented it. In other words, this property contains a reference to the previous view controller.
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)
}