Push to navigation controller from UICollectionViewCell in UITableViewCell - ios

Hi people and please help
Am not using interface builder.
In AppDelegate:
...
tabBarController.viewControllers = [tabOne, tabTwo, tabThree, tabFour]
window?.rootViewController = UINavigationController(rootViewController: tabBarController)
...
in tabOne witch is UIViewController, lazy load UITableView with custom cell and in that cell row I lazy load UICollectionView.
And i need to push in navigationController some viewController?
Thanks

Please make tabBarController as rootViewController of window
Then create 4 navigationControllers for 4 viewController (tabOne, tabTwo, tabThree, tabFour) of tabbarController.
Like these codes:
let viewControllers = [UINavigationController(rootViewController: tabOne),
UINavigationController(rootViewController: tabTwo),
UINavigationController(rootViewController: tabThree),
UINavigationController(rootViewController: tabFour)]
tabBarController.viewControllers = viewControllers
window?.rootViewController = tabBarController
TabbarController is a containerViewController, so:
you need to create 4 navigationController for 4 tabs, because they have the particular flows.
as your code, tabOne.navigationViewController is nil so you can't push other viewcontroller because your navigationController doesn't belong to tabOne, it belongs to tabBarController.

You have to take callback from Collection view cell to the controller or class(which contains the UICollectionView, I think in your case table view cell's .m file)
Then callback on the controller which contains the main table view.
Now you are on the root view controller of the tab.
Push the navigation controller from here (If you already have any navigation controller)
Your case:
TabBarViewController -> RootViewController(One out of four tab) ->
TableView -> TableViewCell -> UICollectionView -> UICollectionViewCell
Take the callback on RootViewController Then you can do anything.
Remember You should have navigation controller to push another controller.

I find solution.
In super class for ViewControllers, create function that push to navigation, and from View (Cell) need to delegate self, and call that function. Thanks to all who try to help

Related

How can I embed NavigationController to Viewcontroller in xib

I have a viewcontroller lets say A that created with xib and I want to add navigationController to it .
View hierarchy :
UITabbarController -> NavigationController -> B Viewcontroller (A added tabbarcontroller in here when a button in B click)
I add A view to Tabbar in B Viewcontroller's button action like
tabBarController?.view.addSubview(A.view)
With this result , View A appears on Tabbar that what I want.
In A viewController there is a Tableview has some datas.When I click a row I want to push another Viewcontroller with self.navigationController.push(..) But I can't push anything because UITabbarController doesnt have a navigationController so I want to add NavigationController to A viewController . I searched lots of things but can't find any possible solution for it.
How can I achieve this?
Set the rootViewController of UINavigationController and add it to the view hierarchy.
let navigationController = UINavigationController(rootViewController: aViewController)
Also, to append a new view controller to the UITabBarController's viewControllers use:
tabBarController?.viewControllers?.append(navigationController)
Or:
tabBarController?.viewControllers?.insert(navigationController, at: 0)
Based on your view hierarchy
UITabbarController -> NavigationController -> B Viewcontroller
You must append ViewController not ViewController's view
func addMoreViewController() {
let aViewController = UINavigationController(rootViewController: aViewController)
self.viewControllers.append(aViewController)
}

Adding a ViewController in front of window!.rootViewController of type UITabBarController Swift 4

I have inherited a storyboard that launched directly to a UITabBarController. In the AppDelegate.swift file in the didFinishLaunchingWithOptions method there is the following code:
//let loginScreen = window!.rootViewController
let tabController = window!.rootViewController as! UITabBarController
let navController = tabController.viewControllers![0] as! UINavigationController
let salaryController = navController.topViewController as! SalaryTableViewController
let nav2Controller = tabController.viewControllers![1] as! UINavigationController
let employeeController = nav2Controller.topViewController as! EmployeeTableViewController
I am trying to change the code so the login screen is the rootViewController but when I remove the rootViewController from the tabController assignment I get an error that casting a UITableViewController to UITabBarController always fails. Does anyone know why casting a UITableViewController to a UITabBarController always fails unless the UITableViewController is the rootViewController? Is there a way to change the launch screen to a different controller without changing the rootViewController assignment? Here is the storyboard for visual:
enter image description here
UITabBarController is a subclass of UIViewController. You cannot cast superclass to subclass. (Viceversa is possible)
To implement your idea, you should remove initialing your root view controller from storyboard and do it programmatically. Because now, the Xcode think your initial view controller is UITabBarController and the only way is that you instantiate your class programmatically and then make it root view controller.
Why don't you present your login VC as a modal view controller? That way you didn't need to change anything on the view hierarchy.

How I could clean UINavigationBar transitions history?

I currently have parental "menu" TableView with UINavigationBar and from each cell there is a segues by reference outlet to 3 similar Views with different information.
In each View there is a buttons to other 2 Views.
With every button's segue opens another View.
The problem:
From every View UINavigationBar's back button returns me to previous View but i tries to make back button to "menu".
Additional Bar Button Item and segue from it makes very close effect but segue animation is not like in UINavigationController.
How I could clean UINavigationBar transitions history in segue to initial View?
You can try pop to root view controller or You can edit navigation controller viewControllers property and remove/add some VC in between.
You can try Unwind Segue mechanism too.
Here are some methods(function) that navigation controller providing for pop operations. They are returning optional UIViewController (intance) from it’s navigation stack, that is popped.
open func popViewController(animated: Bool) -> UIViewController? // Returns the popped controller.
open func popToViewController(_ viewController: UIViewController, animated: Bool) -> [UIViewController]? // Pops view controllers until the one specified is on top. Returns the popped controllers.
open func popToRootViewController(animated: Bool) -> [UIViewController]?
Here is sample code as a solution to your query::
// if you want to back to root of your app
if let rootNavigationController = self.window?.rootViewController as? UINavigationController {
rootNavigationController.popToRootViewControllerAnimated(true)
}
// But if you want to back to root of your current navigation
if let viewcontroller = self.storyboard?.instantiateViewController(withIdentifier: "NewViewController") as? NewViewController { // or instantiate view controller using any other method
viewcontroller.navigationController?.popToRootViewControllerAnimated(true)
}

How to open a TabBarController in ViewControllers that Instantiate from SlideMenuViewController

I want to have tabBar in all my ViewControllers. I have implemented the SWRevealViewController, which have two view controllers linked one is TabBarController and another is TableViewController I wants to have the same tabBar in all my ViewControllers that Segues from TableViewController.
I want to have tabBar in all my ViewControllers. I have implemented
the SWRevealViewController, which have two view controllers linked one
is TabBarController and another is TableViewController I wants to have
the same tabBar in all my ViewControllers that Segues from
TableViewController.
You can do some tweak like this, create the custom delegate and set delegate of TableViewController and TabBarController to SWRevealViewController. Now first time lets say you open the TableViewController and when tap on any cell then just invoke the delegate method which should execute inside SWRevealViewController class and then perform segue inside that class which should open the TabBarController and when click back button of TabBarController again invoke the delegate method which should execute inside SWRevealViewController class and perform segue to open the TableViewController
This shouldn't be too difficult. Your RevealViewController (the initial controller of your storyboard) already seems to be a subclass of SWRevealViewController. This is good, but it's not essentially needed for this scenario. What you do need is a UIViewController implementation for your side menu. The Storyboard alone won't do.
Also, I wouldn't use segues here to switch between the tabs because a segue has a destination view controller. So every time you perform a segue, you would create a new instance of the destination view controller. I would rather implement a UIViewController that handles the cell selection of the UITableView
Now let's say you create a UIViewController or UITableViewController as your menu (actually the RearViewController). Make sure to assign this class to your UITableViewController in your storyboard.
class MenuViewController: UITableViewController {
// MARK: UITableViewDelegate
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let tabIndex = indexPath.row
let tabbarVC = (revealViewController().frontViewController as! UITabBarController)
tabbarVC.selectedIndex = tabIndex // this assumes the menu items are in the same order as the tabs. if not you need to adjust this
revealViewController().revealToggle(animated: true) // close the side menu after switching tabs
}
}
this should be all you need

Swift can't fixed navigation bar for UITableView

I am trying to display a fix navigation bar for my UiTableViewController, I have a first ViewController and when I click on it, this will open my UITableViewController Here is the code of the click :
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
presentViewController(vc, animated: true, completion: nil)
the UItableView is correctly display but not navigation bar appear, if I add one, the navigation bar scroll with the table view and I don't want this behavior.
I tried this without success :
Go to the Editor menu, and click on the Embed In submenu, and choose
Navigation Controller
And tried to change some settings here :
Actually, in your case you want to show navigation and for navigation you have to push your view controller to a UINavigationController thats why the solution is :
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)
presentViewController offers a mechanism to display a modal view controller; i.e., a view controller that will take full control of your UI by being superimposed on top of a parent controller & establish a parent child relation b/w presenting & presented view controllers.
where as
pushViewController offers a much more flexible navigation process where you can push & pop a new controller to UINavigationController, so to go back to the previous one, in a ordered way. Imagine that controllers in a navigation controller will just build a sequence from left to right like building a stack of view controllers stacking upon each other.
Do it this way:
let vc = storyboard.instantiateViewControllerWithIdentifier("MyTableViewController") as! MyTableViewController
vc.myObject = object // I pass some data
self.navigationController?.pushViewController(vc, animated: true)

Resources