Table View To Custom View Controller? - swift

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.

Related

Swift present transitions navigation controller UIBarbuttonitem disappearing

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)

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)

Left Side Menu opens when Right Side Menu is Clicked in Swift

I'm using ENSideMenu Library for creating the menus in my navigation bar in Swift. I already created the left side menu of the navigation bar, but then when I created the right side. What happen is, when I click the right side button that contains the action for showing the right menu... the left side menu is shown instead. Here's my current implementation.
RightSideNavigationController
override func viewDidLoad() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let menu = storyboard.instantiateViewController(withIdentifier: "RightMenuTableViewController") as! RightMenuTableViewController
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: menu, menuPosition: ENSideMenuPosition.right)
sideMenu?.menuWidth = 200
view.bringSubview(toFront: navigationBar)
}
RightMenuTableViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var destView: UIViewController!
if indexPath.row == 0 {
destView = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
} else {
destView = storyboard.instantiateViewController(withIdentifier: "ViewController") as! ViewController
}
sideMenuController()?.setContentViewController(destView)
}
SideNavigationController
override func viewDidLoad() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let menu = storyboard.instantiateViewController(withIdentifier: "MenuTableViewController") as! MenuTableViewController
sideMenu = ENSideMenu(sourceView: self.view, menuViewController: menu, menuPosition: ENSideMenuPosition.left)
sideMenu?.menuWidth = 200
view.bringSubview(toFront: navigationBar)
}
MenuTableViewController
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
var destinationViewController: UIViewController!
if indexPath.section == 0{
if indexPath.row == 0 {
destinationViewController = storyboard.instantiateViewController(withIdentifier: "TopViewController") as! TopViewController
} else if indexPath.row == 1 {
destinationViewController = storyboard.instantiateViewController(withIdentifier: "MemberListViewController") as! MemberListViewController
}
}else {
if indexPath.row == 0 {
destinationViewController = storyboard.instantiateViewController(withIdentifier: "MemberRegisterViewController") as! MemberRegisterViewController
} else if indexPath.row == 3 {
destinationViewController = storyboard.instantiateViewController(withIdentifier: "UserRegistrationViewController") as! UserRegistrationViewController
}
}
sideMenuController()?.setContentViewController(destinationViewController)
}
The ENSideMenu Library currently doesn't support 2 menus which uses the library in the same page. This is according to the issues posted in ENSideMenu Library GitHub

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.

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