How to segue multiple view controllers programmatically using swift 3.0 - ios

I'm getting an error:
has no segue with identifier 'dest1''
and what I did was:
route(ctrl+drag) in storyboard viewcontroller -> dest1, viewcontroller -> dest2
have an action button in viewController.swift
set storyboard ids to "dest1" and "dest2"
#IBAction func Parameters(_ sender: AnyObject) {
self.performSegue(withIdentifier: "dest1", sender: nil)
}
Prepare for segue
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("0")
if segue.identifier == "dest1" {
if let svc = segue.destination as? secondViewController {
print("1")
}
} else if segue.identifier == "dest2" {
if let svc = segue.destination as? reverbViewController {
print("2")
}
}
What am I missing?

From your description, you have set storyboard ids to "dest1" and "dest2", whereas you need to set segue identifiers. Click on the segue itself in IB, and set it's property.
Storyboard identifiers are used when creating the view controller using code...
instantiateViewControllerWithIdentifier("myViewController")

Related

How to override the prepareForSegue with multiple Segue's

So I'm learning Core Data and I managed to create a segue with a parent relationship that allows me to segue to show only certain items based on the category you select in the previous VC (A Category View Controller). Now I have created a separate VC after this which shows the weather based on your location and to get to it you have to click the button from the original VC (The Category View Controller) to get to the weatherViewController. However every time I try to do so and perform a segue I run into this error below :
This is the code I have so far :
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destinationVC = segue.destination as! NoteTableViewController
let weatherDestinationVC = segue.destination as! WeatherViewController
if let indexLocale = noteTableView.indexPathForSelectedRow {
destinationVC.selectionCategory = noteArray[indexLocale.row]
print(indexLocale.row)
} else {
print("Error")
weatherDestinationVC.delegate = self
performSegue(withIdentifier: "goToWeather", sender: self)
}
}
#IBAction func goToWeatherView(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "goToWeather", sender: self)
}
Any help would be appreciated since I am just learning Core Data!
Conditionally cast the different segue destination view controllers or check for the segue's identifier whichever you prefer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationVC = segue.destination as? NoteTableViewController {
if let indexLocale = noteTableView.indexPathForSelectedRow {
destinationVC.selectionCategory = noteArray[indexLocale.row]
print(indexLocale.row)
}
} else if let weatherDestinationVC = segue.destination as? WeatherViewController {
weatherDestinationVC.delegate = self
}
}
Also, you don't have to performSegue within prepareForSegue.

Transfer data from segue to segue with different navigation controller

I want to transfer data from the view on the bottom right to the one on the upper right. Thanks in advance.. :)
I think that what you search are unwind segues.
You can find a good how to about segues in this article from Apple:
https://developer.apple.com/library/content/featuredarticles/ViewControllerPGforiPhoneOS/UsingSegues.html
Especially interesting for you should be the section "Creating an Unwind Segue".
The result should then be similar to this:
1. Give your segue a identifier, "make sure the identifier should be created from Controller"
2. use this function
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourIdentifier" {
let dvc = segue.destination as! yourVC
`// pass data
dvc.data = data
}
}
You can use it's override method to pass data
1) apply segue.identifier in storyBoard
2) In Action add self.performSegue(withIdentifier:"addTask" , sender: indexPath)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "addTask" {
let taskData:Task?
let vc = segue.destination as! AddTaskVC
vc.taskData = taskList[indexpath.row]
vc.isUpdateTask = isEditTask
}
}
You can use this when you set segue from first controller to third. Do check for identifier names while creating segue.
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.identifier == "yourIdentifier1"
{
let nextScene = segue.destination as? UIViewController
nextScene.stringUserName = "Mike"
}
else if segue.identifier == "yourIdentifier2"
{
let laterScene = segue.destination as? UIViewController
laterScene.count = 5
}
}
If you still find and error, please show the error

Passing data with tabBar DidSelectItem using tags and PerformSegueWithIdentifier

I am using swift 3 have a taBbar and when I select an tabBarItem the segue is called programatically to go to a new View Controller. I also need to pass some data with this segue.
my code (for tabbar didSelectItem):
func tabBar(tabBar: UITabBar, didSelectItem item: UITabBarItem) {
if item.tag == 0 {
self.performSegueWithIdentifier("nearbyHotelsSegue", sender: nil)
} else if item.tag == 1 {
self.performSegueWithIdentifier( "nearbyRestaurantsSegue", sender: nil)
} else if item.tag == 2 {
self.performSegueWithIdentifier( "nearbyEventsSegue", sender: nil)
} else if item.tag == 3 {
self.performSegueWithIdentifier( "morePlacesSegue", sender: nil)
}
}
From my research so far, I know that can use PrepareForSegue for this, but I can't use this inside the tabBar (didSelectItem) method. How can I pass data using tabbar didSelectitem method or is there any other good way to achieve this?
Thanks.
The prepareForSegue method is used as an overridden one, so you should use it out of tabBar function's bounds. When you call performSegue(withIdentifier: "segueName", sender: nil), you have given nil in sender. Sender is the parameter of type Any?, where you put your custom data you want to pass to another controller. In prepare(forSegue: UIStoryboardSegue, sender: Any?) when you cast your destination view controller as the one you want to use, pass the sender data to this controller, e.g.:
override func prepare(forSegue: UIStoryboardSegue, sender: Any?) {
if let controller = segue.destination as? YourDestinationViewController {
controller.receivedData = sender
}
}
I solved this using the following code. it seems prepareForSegue is not effected by the use of PerformSegueWithIdentifier to perform the segue programmatically.
So I used the code posted in the question unchanged and then added the following code to pass data.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "nearbyHotelsSegue" {
if let toViewController = segue.destinationViewController as? NearbyHotelsViewController {
toViewController.returnedText = (searchBaseItem?.baseItemId)!
}
}
//....other ifs
}

Pass data from UIViewController to UISpiltView

Right now I am facing some problem to pass data from UIViewController to UISpiltViewController.
My app start with UIViewController, and after user login it, there is a segue from UIViewController to UISpiltViewController, UISpiltViewController is root view of a NavigationViewController, and this NavigationViewController is a root view of a TableViewController. I have written a class for this TableViewController, called MasterView, and right now I want to pass some information from loginView to this MasterView.
self.performSegueWithIdentifier("login", sender: self)
But I want to know how to write the prepareforsegue function.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login"
{
let controller = (segue.destinationViewController as UINavigationController).topViewController as MasterViewController
controller.authorityNum = self.authorityArray
}
}
I want to pass an array, but I will get class cast error. Please help me with this.
Try this i am not sure but i think your segue is Model segue so
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "login"
{
var splitView:UISplitViewController = segue.destinationViewController as! UISplitViewController
var controller = splitView.viewControllers.first?.topViewController as! MasterViewController
controller.authorityNum = self.authorityArray
}
}
Try to NSLog every line it may help
Like this,
let destinationViewController = segue.destinationViewController as! MasterViewController
Swift 3 of the accepted answer.
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "login" {
let splitView:UISplitViewController = segue.destination as! UISplitViewController
let navController = splitView.viewControllers.first as! UINavigationController
let controller = navController?.topViewController as! MasterViewController
controller.authorityNum = self.authorityArray
}
}

xcode navigationbar is not more visible after popover with button

I have created a popover in iphone view with a button inside to segue to another ViewController.
But all Viewcontroller after popOver have the navigationbar not anymore.
My project structure:
NavigationController -> ViewController1 -> ViewController2(popOver) ->ViewController3
All connections are: "show",except from ViewController1 to ViewController2: "present as popover"
If i connect(show) directly from ViewController1->ViewController3, everything is fine...
Where can be the problem?
I used this tutorial:
http://richardallen.me/2014/11/28/popovers.html
ViewController1:
func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "PopOverIdentifier" {
let popoverViewController = segue.destinationViewController as!
popoverViewController.modalPresentationStyle = UIModalPresentationStyle.ViewController2
popoverViewController.popoverPresentationController!.delegate = self
}
}
ViewController2
includes Pickerview and:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toVC3" {
var DestViewController : ViewController3 = segue.destinationViewController as!ViewController3
DestViewController.passedUserID_target = selected_user
}
}
Problem:
I solved this problem (temporarily) with adding a new NavigationController to the ViewController3 with (Editor->EmbedIn->NavigationController) and add a back button to buttom of the layout.
The problem to segue data from VC1->VC3, i solved with defining a global Variable in VC3 and add a action Button (in VC2) with assigning the data to these created global variables.

Resources