Swift present transitions navigation controller UIBarbuttonitem disappearing - swift

navigation controller > searchviewcontroller > detailviewcontroller , With the present module, I want it to pass with the effect using the hero library, but the back button disappears, I transfer the data with json, how can I bring it back button but present transition must because hero 3rd library use transitions effect
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let vc = UIStoryboard.init(name: "Main", bundle: Bundle.main).instantiateViewController(withIdentifier: "searchViewController") as! searchviewcontroller
vc.hero.isEnabled = true
vc.hero.modalAnimationType = .fade
let backItem = UIBarButtonItem()
backItem.title = self.viewModel.moviesList?[indexPath.row].Title
backItem.tintColor = .white
navigationItem.backBarButtonItem = backItem
vc.viewModel.movieId = self.viewModel.moviesList?[indexPath.row].imdbID
self.present(vc, animated: true, completion: nil)
}
}
SearchViewController
this way it works without any problems, the data goes through and when I press back it comes back without any problems
self.navigationController?.pushViewController(vc, animated: true)
The back button disappears, how can I add it here?
self.present(vc, animated: true, completion: nil)

Related

How to navigate from a custom collection view to a TabbarController in swift 4?

I have tried this but didn't work for me:
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let cell = collectionView.cellForItem(at: indexPath)
cell?.layer.borderColor = UIColor.gray.cgColor
cell?.layer.borderWidth = 0.5
print("swift")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "Action") as! UIViewController
self.navigationController?.pushViewController(viewController, animated: true)
}
You are pushing to a UIViewController.
You might be pushing to a UIViewController inside UITabbarController. To navigate to UITabbarController you need to instantiate the UITabbarController and push to it.
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "TabBar") as! UITabBarController
self.navigationController?.pushViewController(viewController, animated: true)
*//Get reference to your tab bar.*
let tabBar:UITabBarController = self.window?.rootViewController as! UITabBarController
let navInTab:UINavigationController = tabBar.viewControllers?[1] as! UINavigationController
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let destinationViewController = storyboard.instantiateViewControllerWithIdentifier("ControllersName") as? ControllersType
navInTab.pushViewController(destinationViewController!, animated: true)
*//To whichever index you want to navigate.*
tabBar.selectedIndex = 1
**Hope this helps.**
You should segue to the tab bar controller, not the UIViewController contained by it.
An easy way to do it is setting a segue using your storyboard and call performSegue when you want to initiate the transition.
First set the storyboard as follows:
Then you may call self.performSegue(withIdentifier: "logInSegue", sender: self)
(logInSegue is the name of my segue but obviously you should change it to your segue identifier you have set in your storyboard)

Swift: Gesturing doesn't bring back the TabBar

I am trying to move back from a ViewController (simple text page) to a main ViewController with a TabBar at the bottom using gesturing.
The gesture works as I return to the original main screen but there is no TabBar.
In the simple ViewController I use this code to go back to the originating ViewController.
#objc func respondToSwipeGesture(gesture: UIGestureRecognizer) {
if let swipeGesture = gesture as? UISwipeGestureRecognizer {
switch swipeGesture.direction {
case UISwipeGestureRecognizerDirection.right:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let controller = storyboard.instantiateViewController(withIdentifier: "AboutViewControllerID")
self.present(controller, animated: true, completion: nil)
if let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "AboutViewControllerID") as? AboutViewController
{
present(vc, animated: true, completion: nil)
}
default:
break
}
}
}
In the ViewController with the TabBarController I have tried the following lines to rejuvinate the TabBarController but without any joy.
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
tabBarController?.tabBar.isHidden = false
NSLog("TabBar true")
}
Any ideas?
First if you want to go-back don't use present as it will add the same VCS twice in the stack , you have to use unwindSegue/dismiss , or load the tabBar itself with id
self.dismiss(animated: true) {
// use this if it's not directly behind the dismissed VC
let tab = storyboard.instantiateViewController(withIdentifier: "tabBarID") as! UITabBarController
UIApplication.shared.keyWindow?.rootViewController = tab
}
//

Dynamic TableView Cell to Multiple Detail Views Swift

I'm using presentViewController to navigate between two views in XCode. I was wondering how I might select a cell row from a dynamic tableView to navigate to a view controller. So, basically, when I tap a dynamic cell, each cell navigates to its own different view. How might I do this?
So far I have this code:
#IBAction func press(sender: AnyObject) {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("newViewController") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)
}
But, instead of using a button, I would like each cell to lead to another particular view as I said above.
thanks in advance
Create an array that holds the view controller identifiers:
let identifiers = ["newViewController", "newViewController2", "newViewController3"]
Implement this UITableViewDelegate method in your view controller:
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
if let identifier = identifiers[indexPath.row] {
let storyboard: UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let vc: UINavigationController = storyboard.instantiateViewControllerWithIdentifier("newViewController") as! UINavigationController
self.presentViewController(vc, animated: true, completion: nil)
}
}
Don't forget to set your tableView's delegate to the view controller that contains the tableview and that implements the above delegate method.

Table View To Custom View Controller?

I Wanted to know if I have two cells in a tableview, and I want that if i click the first cell it seagues to specific view controller, and if I click the second cell it seagues to another view controller.
How can I do that?
You can do it this way in didSelectRowAtIndexPath method:
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
switch indexPath.row {
case 0 :
println("First cell")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("FirstViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
case 1 :
println("Second Cell")
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("SecondViewController") as! UIViewController
self.presentViewController(vc, animated: true, completion: nil)
default:
println("No cell selectd")
}
}
HERE is your sample project.
Call performSegue:withIdentifier: from the method didSelectRowAtIndexPath in your view controller. You can conditionally call segues with different identifiers in your code.

Show ViewController from XIB-file-Button - Swift

is there a way how to segue from a xib-file (custom TableViewCell) to another ViewController in the Main.storyboard.
There's no possibility to drag a segue, like within the main storyboard.
In the cell I've got a button, from where I want to change the view.
How can I solve it?
Thanks!
You can always instantiate a view controller from your Storyboard and present it on button tapped:
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewControllerWithIdentifier("ViewControllerID") as UIViewController
self.presentViewController(vc, animated: true, completion: nil)
Define a segue in StoryBoard and specify the identifier, like detail
Perform segue in talbeView(_, didSelectRowAtIndexPath):
Use self.performSegueWithIdentifier to fire a segue
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
self.performSegueWithIdentifier("detail", sender: nil)
tableView.deselectRowAtIndexPath(indexPath, animated: true)
}