Dynamic TableView Cell to Multiple Detail Views Swift - 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.

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)

xcode swift4 how do I change the page in viewdidload

I cannot use segue on viewDidLoad.
I already check the segue is worded on another func.
When load the view check some condition then change view this is what I want.
override func viewDidLoad(){
super.viewDidLoad()
performSegue(withIdentifier: "go" , sender: self)
}
If you want to push your view controller on view did load then just use this code.
let storyBoard : UIStoryboard = UIStoryboard(name: "your_StoryBoard", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "VC_Identifier") as! TargerViewController
self.navigationController?.pushViewController(nextViewController, animated: true)
OR
You can set the target viewcontroller as root viewcontroller by check any condition.
Hope it will helps you out.
i prefer that don't use segue use as below
let storyboard = UIStoryboard(name: "YourStoryboard", bundle: nil)
let VC = storyboard.instantiateViewController(withIdentifier: "Your Identifier") as! Your ViewController
self.present(VC, animated:true, completion:nil)
user this for redirect from One VC To Another VC
Replace viewDidLoad to viewDidAppear solved my problem
override func viewDidAppear() {
super.viewDidLoad()
performSegue(withIdentifier: "go" , sender: self)
}

Push navigation from xib Files to storyboard viewcontroller in swift3 [duplicate]

I have a view in my xib file which contain buttons. i want to move to a ViewController when i will press the button (#IBAction). I have used below code
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("About") as! AboutViewController
self.presentViewController(nextViewController, animated: true, completion: nil)
I am getting the error "Value of type 'SlideMenuView' has no member 'presentViewController'.
because my class is a UIView type :
class SlideMenuView: UIView {
}
so how can I navigate to other view controller.
That is beacuase the class you are trying to present from is a UIView and not a UIViewController. It has no Present method.
I'm guessing your view (SlideMenuView) is embedded inside a viewcontroller. what you need to do is implement a delegate, and inform your containing viewController to present next Viewcontroller.
code below:
#protocol SlideMenuViewDelegate: class {
func slideMenuViewAboutButtonClicked(menuView: SlideMenuView)
class SlideMenuView: UIView {
weak var delegate: SlideMenuViewDelegate?
#IBAction func aboutButtonClicked(sender: AnyObject) {
self.delegate?.slideMenuViewAboutButtonClicked(self)
}
now, in your viewController, implement this delegate method:
func slideMenuViewAboutButtonClicked(menuView: SlideMenuView) {
let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewControllerWithIdentifier("About") as! AboutViewController
self.presentViewController(nextViewController, animated: true, completion: nil)
}
Also, dont forget to assign the sliderMenuView object the viewcontroller as a delegate.
something like:
self.sliderMenuView.delegate = self // (self == the containing viewController
I did it in a different way. In class file
class SlideMenuView: UIView {
var navigationController: UINavigationController? // Declare a navigation controller variable
// And create a method which take a navigation controller
func prepareScreen(navController: UINavigationController)-> UIView {
navigationController = navController
let nibView = NSBundle.mainBundle().loadNibNamed("SlideMenuView", owner: self, options: nil)[0] as! UIView
self.addSubview(nibView)
return nibView
}
// In Button action
#IBAction func btnAction(sender: UIButton) {
var storyBoard = UIStoryboard(name: "Main", bundle: nil)
let nextViewController = storyBoard!.instantiateViewControllerWithIdentifier("NextViewController") as! UIViewController
navigationController?.pushViewController(nextViewController, animated: true)
}
}
// For calling from UIViewController
slideBarMenuIstance.prepareScreen(self.navigationController!)

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