Pass a parameter to prepareForSegue from function - ios

I have to functions, both of them trigger performSegueWithIdentifier with the same segue. But depending of which function was called I need to pass different parameters in prepareForSegue.
Some thing like
func first() {
// do some stuff
performSegueWithIdentifier("My segue", sender:AnyObject?)
}
func second() {
// do some stuff
performSegueWithIdentifier("My segue", sender:AnyObject?)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My segue" {
let destination = segue.destinationViewController as! MyController
if functionFirstWasCalled {
destination.property = value
} else if functionSecondWasCalled {
destination.property = anotherValue
}
}
}
Surely, I can do this by setting booleans from second() and first() and then checking them in prepareForSegue - but maybe there is some more elegant way to do this ?

In objective -c you would do:
[self performSegueWithIdentifier:#"segueNAme" sender:#"firstMethod"];
and you can access this message in the prepareForSegue method
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([sender isEqualToString:#"firstMethod"]) {
//firstMEthod called the segue
}
}
The swift equivalent I think would be:
self.performSegueWithIdentifier("segueNAme", sender: "firstMethod")
and
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject) {
if (sender == "firstMethod") {
//firstMEthod called the segue
}
}
My suggestion would be to instead of sending a plain string , send a dictionary type object that contains the methodName, className and some other params useful for future debugging.

All you have to do is send a flag by sender attribute, something like this:
func first() {
performSegueWithIdentifier("My segue", sender:true)
}
func second() {
// do some stuff
performSegueWithIdentifier("My segue", sender:false)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My segue" {
let destination = segue.destinationViewController as! MyController
let isFirstFunctionCalled = sender as! Bool // cast sender to bool
if isFirstFunctionCalled {
destination.property = value
} else {
destination.property = anotherValue
}
}
}

Simply set parallel properties in this View Controller which you then pass to the destination View Controller based on the function called. i.e:
var a = ""
var b = 0
func first() {
// do some stuff
a = "first function determined this variable."
b = 1
performSegueWithIdentifier("My segue", sender:AnyObject?)
}
func second() {
// do some stuff
a = "second function determined this variable."
b = 182
performSegueWithIdentifier("My segue", sender:AnyObject?)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "My segue" {
let destination = segue.destinationViewController as! MyController
if functionFirstWasCalled {
destination.property = a
} else if functionSecondWasCalled {
destination.property = b
}
}
}

Related

How to set a delegate in Swift

I want to send my UserModel with all user informations from a ViewController (ShowUserViewController) to another ViewController (ChatViewController) with a delegate but its not working.
In my ShowUserViewControllers user are all informations I want to send to the ChatViewController.
var user: UserModel?
In my ChatViewController I have the following declaration where I want to send my datas:
var currentUser: UserModel?
Here my protocol:
protocol UserInfoToChatID {
func observeUserID(user: UserModel)
}
Here I prepare the segue and set delegate by tapping the button:
} else if segue.identifier == "UserInfoToChatVC" {
let chatVC = segue.destination as! ChatViewController
chatVC.currentUser = self.user
}
}
var delegate: UserInfoToChatID?
#IBAction func chatButtonTapped(_ sender: UIBarButtonItem) {
delegate?.observeUserID(user: user!)
}
At last I call the delegate in my ChatViewController:
extension ChatViewController: UserInfoToChatID {
func observeUserID(user: UserModel) {
self.currentUser = user
performSegue(withIdentifier: "UserInfoToChatVC", sender: self)
}
}
If you need to pass data from one ViewController to another, you don't have to use delegates for this. You can just pass this data as sender parameter of performSegue method:
performSegue(withIdentifier: "UserInfoToChatVC", sender: user!)
then in prepare for segue just downcast sender as UserModel and assign destination's currentUser variable
...
} else if segue.identifier == "UserInfoToChatVC" {
let chatVC = segue.destination as! ChatViewController
chatVC.currentUser = sender as! UserModel
}
}
But in your case you actually don't have to pass user as sender. You can just assign destination's currentUser variable as ShowUserViewController's global variable user
...
} else if segue.identifier == "UserInfoToChatVC" {
let chatVC = segue.destination as! ChatViewController
chatVC.currentUser = user!
}
}
2 things:
first, if you just want to pass data from one viewController to other viewController you don't need to use delegate pattern, just pass the object to the next viewController on prepare form segue.
second, if you want to implement the delegate pattern you should have one viewController than call to the delegate and the other implement the functions.
example:
protocol ExampleDelegate: class {
func delegateFunction()
}
class A {
//have delegate var
weak var delegate: ExampleDelegate?
// someWhere in the code when needed call to the delegate function...
delegate?.delegateFunction()
}
Class B: ExampleDelegate {
func delegateFunction() {
// do some code....
}
//when you move to the next viewControoler(to A in that case)
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "AClass" {
if let vc = segue.destination as? A {
vc.delegate = self
}
}
}
To pass the UserModel object forward, from ShowUserViewController to ChatViewController, you should use something called Dependency Injection:
So you'll do something like this inside ShowUserViewController:
#IBAction func chatButtonTapped(_ sender: UIBarButtonItem) {
performSegue(withIdentifier: "UserInfoToChatVC", sender: nil)
}
Note: The sender parameter should be the object that initiated the segue. It could be self, i.e. the ShowUserViewController object, but I'd advise against passing the UserModel object, because that object did not initiate the segue, and has nothing to do with navigation at all. It should be injected inside the Destination Controller later on.
In the same file, override the prepare(for:) method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "UserInfoToChatVC" {
let chatVC = segue.destination as! ChatViewController
chatVC.currentUser = self.user
}
}
I believe you've mostly done this part right, but you may need to communicate back from ChatViewController to ShowUserViewController.
In that case, you can and should use Delegation.
Create something like this inside ShowUserViewController:
protocol ChatViewControllerDelegate: class {
func didUpdateUser(_ model: UserModel)
}
class ChatViewController: UIViewControler {
var user: UserModel?
weak var delegate: ChatViewControllerDelegate?
/* more code */
func someEventHappened() {
delegate?.didUpdateUser(self.user!)
}
}
Finally, there is an additional line to be added to the prepare(for:) method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "UserInfoToChatVC" {
let chatVC = segue.destination as! ChatViewController
chatVC.currentUser = self.user
// Add this line...
chatVC.delegate = self
}
}
And specify that the ShowUserViewController implements the ChatViewControllerDelegate protocol, then override the didUpdateUser(_:) method:
func didUpdateUser(_ model: UserModel) {
// Some code here
}

prepareForSegue called before performSegue

I am trying to perform a segue that passes a number of variables to the next view including one variable, currentID, which is retrieved from a parse database. performSegue should not be called until after currentID has been set to the currentID downloaded from the database. However, when I run the code, currentID ends up being an empty string when it is passed to the next view.
Here is my code called by the Button:
#IBAction func submitButtonPressed(_ sender: Any) {
let point = PFGeoPoint(latitude:0.0, longitude:0.0)
let testObject = PFObject(className: "Person")
testObject["inputAmount"] = inputAmount
testObject["outputAmount"] = outputAmount
testObject["inputCurrency"] = inputCurrency
testObject["outputCurrency"] = outputCurrency
testObject["location"] = point
testObject.saveInBackground { (success, error) -> Void in
// added test for success 11th July 2016
if success {
print("Object has been saved.")
self.currentID = String(describing: testObject.objectId!)
if(self.currentID != ""){
self.performSegue(withIdentifier: "mainToListSegue", sender: self)
}
} else {
if error != nil {
print (error)
} else {
print ("Error")
}
}
}
}
And here is the prepareForSegue method:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let listViewController = (segue.destination as! UINavigationController).viewControllers[0] as! ListViewController
listViewController.inputCurrency = inputCurrency
listViewController.outputCurrency = outputCurrency
listViewController.inputAmount = inputAmount
listViewController.outputAmount = outputAmount
listViewController.currentID = currentID
listViewController.cellContent = cellContent
}
To achieve your needs, you MUST connect your segue between viewcontrollers, and not from UIButton to viewcontroller.
Every time you need to prepare your segue before calling it, this is the procedure:
Then, name it and use delegate method
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "mySegue" {
}
}
For navigating from one controller to another, connect your segue from view controller instead of from the button and it will work.

Pass data with prepareForSegue

Im trying to pass data from viewController 1 to viewController2, I have 2 buttons and 1 segue(therefore there is one segue identifier) for those 2 buttons, each button when pressed should show: 1 label to show the title and 1 textView to show a definition, I am having troubles to show its own data of each word; I know it has to be the some code referencing the SENDER in the performSegueWithIdentifier, but I don't know how to do it.
I appreciate your help !!! thanks.
here is my code
class ViewController: UIViewController {
#IBAction func AbstractionBtn(sender: AnyObject) {
performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
}
#IBAction func binarySystemBtn(sender: AnyObject) {
performSegueWithIdentifier("ShowDefinition", sender: "Binary System")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "ShowDefinition") {
if let destinationViewController = segue.destinationViewController as? EnglishViewController {
destinationViewController.titleMsg = "Abstraction"
destinationViewController.definitionMsg = "Abstraction Definition"
}
} else if(segue.identifier == "ShowDefinition"){if let destinationViewController = segue.destinationViewController as? EnglishViewController {
destinationViewController.titleMsg = "Binary System"
destinationViewController.definitionMsg = "Binary System Definition"
}
}
}
You have correctly passed the definition as a String in the sender parameter in performSegueWithIdentifier. You just need to use its value in prepareForSegue, but first you must cast it from AnyObject? back to a String.
Your code could be something like:
class ViewController: UIViewController {
#IBAction func AbstractionBtn(sender: AnyObject) {
performSegueWithIdentifier("ShowDefinition", sender: "Abstraction")
}
#IBAction func binarySystemBtn(sender: AnyObject) {
performSegueWithIdentifier("ShowDefinition", sender: "Binary System")
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if (segue.identifier == "ShowDefinition") {
if let destinationViewController = segue.destinationViewController as? EnglishViewController {
if let definition = sender as? String {
if definition == "Abstraction" {
destinationViewController.titleMsg = "Abstraction"
destinationViewController.definitionMsg = "Abstraction Definition"
} else if definition == "Binary System" {
destinationViewController.titleMsg = "Binary System"
destinationViewController.definitionMsg = "Binary System Definition"
}
}
}
}
}
}
Try this one
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
{
if (segue.identifier == "ShowDefinition")
{
if let destinationViewController = segue.destinationViewController as? EnglishViewController
{
var btn = sender as! UIButton
if btn.tag == 1
{
destinationViewController.titleMsg = "Abstraction"
destinationViewController.definitionMsg = "Abstraction Definition"
}
else
{
destinationViewController.titleMsg = "Binary System"
destinationViewController.definitionMsg = "Binary System Definition"
}
}
}
}
And set button tag like this
AbstractionBtn.tag = 1
binarySystemBtn.tag = 2
Now Call for segue like this
#IBAction func AbstractionBtn(sender: AnyObject) {
performSegueWithIdentifier("ShowDefinition", sender:sender)
}
Create a var named titleMsg and definitionMsg.
In each #IBAction method set the appropriate title message to self.titleMsg and similarly for self.definitionMsg.
After that call,
performSegueWithIdentifier("ShowDefinition", sender: self)
And then override,
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowDefinition" {
let destinationVC = segue.destinationViewController as? EnglishViewController
destinationVC.titleMsg = self.titleMsg
destinationVC.definitionMsg = self.definitionMsg
}
}
Hope that helped!

Cannot assign value of type String to type UILabel

I have marked the part of my code where the problem is, it is commented out. The error message is:
Cannot assign value of type String! to type UILabel!.
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendDataSegue" {
if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
var sendingText = metadataObj.stringValue
sendToDetailViewController.messageLabelDos = sendingText
}
}
}
The label it should be changing is in my DetailViewController and it is a label. The code above is from my original ViewController. How can I make this work?
More code to put in context:
if metadataObj.stringValue != nil {
dispatch_async(dispatch_get_main_queue()) {
self.performSegueWithIdentifier("SendDataSegue", sender: self)
}
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendDataSegue" {
if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
var sendingText = metadataObj.stringValue
sendToDetailViewController.viaSegue = sendingText
}
}
}
You need to pass the String instead of setting text to label, because when you correct it and set like this sendToDetailViewController.messageLabelDos.text = sendingText, you will get nil error because messageLabelDos is not initialize yet, so try like this. Create one string instance in DetailViewController and use that inside prepareForSegue for passing String and then use that String instance in viewDidLoad to assign Label to text.
class ViewController: UIViewController {
//Your other methods
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "SendDataSegue" {
if let sendToDetailViewController = segue.destinationViewController as? DetailViewController {
var sendingText = metadataObj.stringValue
sendToDetailViewController.messageDos = sendingText
}
}
}
}
Inside DetailViewController
var messageDos: String = ""
override func viewDidLoad() {
super.viewDidLoad()
self.messageLabelDos.text = messageDos
}

Is it possible to pass a enum through segue in swift

I have this prepareForSegue in my first ViewController with a enum in it
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
enum TypeOfSegue {
case Edit, Create
}
if let DestViewController: DetailViewController = segue.destinationViewController as? DetailViewController where segue.identifier == "EditItem" {
let edit = TypeOfSegue.Edit
}
if let DestViewController: DetailViewController = segue.destinationViewController as? DetailViewController where segue.identifier == "CreateNewItem" {
let create = TypeOfSegue.Create
}
}
I want to pass the constants edit or create to my DetailViewController. If possible, how can I do this and under what variable will the TypeOfSegue be saved in DetailViewController?
You can define a mode property in your DetailViewController
class DetailViewController:UIViewController {
var mode: TypeOfSegue!
}
Next you can populate it
enum TypeOfSegue {
case Edit, Create
}
class ListViewController:UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let destViewController: DetailViewController = segue.destinationViewController as? DetailViewController where segue.identifier == "EditItem" {
destViewController.mode = .Edit
}
if let destViewController: DetailViewController = segue.destinationViewController as? DetailViewController where segue.identifier == "CreateNewItem" {
destViewController.mode = .Create
}
}
}
Update
You can also implement the prepareForSegue method this way
enum TypeOfSegue: String, CustomStringConvertible {
case Edit = "EditItem", Create = "CreateNewItem"
var description: String { return self.rawValue }
}
class ListViewController:UIViewController {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
guard let
destViewController = segue.destinationViewController as? DetailViewController,
identifier = segue.identifier,
segueType = TypeOfSegue(rawValue: identifier) else { fatalError("Wrong segue") }
destViewController.mode = segueType
}
}

Resources