Passing data between View Controllers (Swift) - swift

I'm trying to pass an Image from one view to another and my code doesn't seem to be working- it takes the user to the new view but the image isn't transferred over as well. Any help would be greatly appreciated!
So, here's a button called Post that takes the user to a new view.
#IBAction func postButton(sender: AnyObject) {
performSegueWithIdentifier("toBrowsePage", sender: nil)
}
Then, in another file for the other view controller...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! ListPage
itemToAdd.postingImage.image = browsingImage.image
}
}

Never assign image directly from another ViewController like this:
itemToAdd.postingImage.image = browsingImage.image
But instead of doing that just pass the imageName to the next View Controller and create one instance into your nextViewController which holds this image name and after that you can assign that image with in ViewDidLoad method of other ViewController.
consider this example:
you can pass ImageName this way:
itemToAdd.imageName = "YourImageName"
In your nextViewController create an instance which hold this String:
var imageName = ""
In your ViewDidLoad method:
postingImage.image = UIImage(named: imageName)
Hope this will help.

Its because you are trying to set the image of UIImageView which is not there in memory so it will not be displayed.
Instead you need to pass only image object to next controller. Create on properly for your Image in next controller & then pass the image from this controller to next controller.

In your ListPage ViewController define an UIImage
var newImage: UIImage?
Now In prepareForSegue:
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! ListPage
itemToAdd.newImage = browsingImage.image
}
}
Now in ListPage ViewDidLoad method set Image:
postingImage.image = newImage

Related

How to update a label on the next screen?

Say I have two screens. Screen A has a picker and a button, which triggers a segue to screen B, which displays some content depending on the option selected by the picker.
How do I have the information as to what picker was selected in A, passed to B? So far, I have A doing:
#IBAction func pickThing(_ value: Int) {
self.thing = value;
}
Which seems to work; I believe that it is detecting the value and storing it. However, when I try adding #IBOutlet weak var thingLabel: WKInterfaceLabel! to match the label in B, I can only set the value of it when the app first loads.
If I put self.thingLabel.setText("test") in the awake() function, it sets the label to "test", so that works. But changing it to self.thingLabel.setText("thing \(self.thing)") doesn't work - it sets it to whatever self.thing is initialized as, but doesn't change it later. So awake() is not the right method to use. I've also tried putting it in willActivate and in pickThing, but neither of them did anything.
Is there some method that gets called when a screen is switched to? If not, how can I send data from one screen to the next?
For example on ViewController A use this function
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ViewControllerB {
vc.thing = self.thing
}
}
Or you can use closures in same methods and callback change from B ViewController on A
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let vc = segue.destination as? ViewControllerB {
vc.clouser = { [weak self] thingB in
guard let `self` = self else { return }
self.thing = thingB
}
}
}
You need to make a variable in view controller B titled something like "parentVC," which would be of view controller A's class. In view controller A's class, you need to call prepare(for segue UIStoryboardSegue, sender: Any?). In this method, you can access the segue's destination property, which would be view controller B. From here you can set view controller B's "parentVC" property to "self," i.e. view controller A. Then in view controller B's class you can access properties of view controller A by using the "parentVC" variable. The code in view controller A's class would look something like this:
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.destination is ViewControllerB {
let viewControllerB = segue.destination as? ViewControllerB
viewControllerB.parentVC! = self
}
}

Can I get data from a label from one VC and pass the data to a textfield from another VC [duplicate]

I'm trying to move a user uploaded image from one UIImageView to another UIImageView on a different View Controller. I'm able to have the user upload an image and have it saved in the first UIImageView but after they press the "Post" button, the UIImageView on the next view controller remains blank.
Note: browsingImage is the name of the UIImageView on the second view controller (destination UIImageView)
Any help would be greatly appreciated!
#IBAction func cameraButton(sender: AnyObject) {
addNewPicture()
}
func addNewPicture() {
let picker = UIImagePickerController()
picker.allowsEditing = true
picker.delegate = self
presentViewController(picker, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
postingImage.image = image
self.dismissViewControllerAnimated(true, completion: nil)
}
#IBAction func postButton(sender: AnyObject) {
performSegueWithIdentifier("toBrowsePage", sender: nil)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! ListPage
itemToAdd.postingImage.image = browsingImage.image
}
}
In prepare(for:) you can't access the #IBOutlets of the destination view controller because they haven't been set up yet. You should assign the image to a property of the destination view controller, and then move it into place in viewDidLoad():
In source view controller:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toBrowsePage" {
let dvc = segue.destination as! ListPage
dvc.newImage = postingImage.image
}
}
In destination view controller:
class ListPage: UIViewController {
#IBOutlet weak var browsingImage: UIImageView!
var newImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
browsingImage.image = newImage
}
}
From your description,browsingImage is in destination viewController,so
in this line
itemToAdd.postingImage.image = browsingImage.image
You pass the destination imageview to source imageview
IBOutlets are not accessible in your current view controller. The outlets are not yet initialized since the your second view is not yet loaded.
Instead you can create a UIImage variable in your second viewController and assign the value to it in the prepareForSegue.
And in your second viewController's viewDidLoad method assign that value to your ImageView outlet.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
let destination = segue.destinationViewController as! ListPage
destination.receivedImage = postingImage.image
}
}
In your next viewController declare a variable called
#IBOutlet weak var postingImage: UIImageView!
var receivedImage : UIImage?
In the viewDidLoad method
postingImage.image = receivedImage
At the time of the Segue happening your UIImageView is not initialised so you can not assign an image to it, the best practise would be to pass the UIImage and initialise your UIImageView in viewDidLoad.
so in your ListPage class make a property of type UIImage change your prepareForSegue line as
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "toBrowsePage" {
var itemToAdd = segue.destinationViewController as! ListPage
itemToAdd.image = browsingImage.image
}
}
in viewDidLoad or viewDidAppear of your destination viewController you will do something like this
browsingImage.image = image

Change a button's characteristic outside class programatically

In the code below, I want to be able to change the visibility of a button in another class, but when I try to change button.isHidden to false, the button still doesn't show up.
View Controller 1:
override viewDidLoad(){
button.isHidden = true
}
View Controller 2:
ViewController1().button.isHidden = false
How can I change the button's visibility from another controller
Calling ViewController1() creates a ViewController1 instance instead of working with the ViewController1 instance that has already been instantiated.
To access properties (in this case the button) of ViewController1 from ViewController2, you have to pass a reference to that button from ViewController1 to ViewController2 and change the properties through that reference.
You need to set the reference in your prepare(for segue) function in ViewController1.
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "mySegue {
let nextVC = segue.destination as! ViewController2
nextVC.button = sender as! UIButton
}
}
You also need to set the segue to manual and call it in ViewController1 by self.performSegue(withIdentifier: "mySegue", sender: self.button)
You need to create the property in ViewController2 and access it like this:
class ViewController2 {
var button:UIButton?
func showButtonOnVC1(){
guard let button = self.button else { return }
button.isHidden = false
}
}

Show Label Value on 2nd View - Swift

Since i'm new and a newbie on Swift can someone tell me how I can get the label value on screen 1 to show on screen 2. Thanks
Edit: So I tried the way you told me but for some reason the Label text did not change in View 2. Any help? Thanks
I am passing textFiled data to destViewController to show how segue performs when passing data.
Note: If you want to pass string data to your destVC.You can assign your string like var someValue: String
mainStoryBoard:
MainVC:
#IBOutlet weak var textField: UITextField!
//or you can assign string like var someValue: : String
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let viewController = segue.destination as! destVC
viewController.dataText = textField.text // someValue.text
}
DestVC:
#IBOutlet var label: UILabel!
var dataText = String()
override func viewDidLoad()
label.text = dataText
}
Output:
You already have a segue between ViewController1 and ViewController2 it seems, so now you just need to pass some date from one to another. This can be done in the function prepare(for segue: UIStoryboardSegue, sender: AnyObject?) which is called when you transition from ViewController1 to ViewController2.
The UIStoryboardSegue class has a property called source of type UIViewController and another property called destination which is also a UIViewController.
So, if you define a property on your ViewController2, like so:
var labelValue: String
Then you can pass a value to it in your prepareForSegue defined on ViewController1 like so:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
guard let viewController2 = segue.destination as? YourViewController2 else {
return
}
viewController2.labelValue = theValueFromViewController1
}
Here is tutorial telling you a bit more (in Objective C though)
And this is also a fine introduction.
Hope that helps you.
I'll consider you have ViewController class VC1 (screen 1) & ViewController class VC2 (screen 2). I see from the screenshot that, you're already using a segue to go from the VC1 to VC2 view.
Now, declare a variable in your VC2 class let's call it labelValue,
class VC2 {
var labelValue: String?
...
}
Whenever you use a storyboard segue to move from one viewcontroller's view to other viewcontroller's view a method named,
func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)
will get called if it's overriden in the source viewController class (scene1 in your case). In this method you'd be able to access the destination view controller (scene 2) and it's properties. So for your case you can implement it like this,
class VC1 {
//I have assumed the below label will hold the value which you want to pass it to the scene 2.
var lblResult: UILabel!
......
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// You can access the VC2 instance from the segue
if let vc2 = segue.destinationViewController as? VC2 {
vc2.labelValue = lblResult.text
}
}
Now once you implement the prepareForSegue: method correctly as shown above, you'd be able to get the label's value from scene 1 to labelValue property of scene2.
Do learn more about segues here : More About Segues and Storyboards here.
================================
Edit:
Coming to your edited question, in your View2 viewController, you've declared labelNumber1 as String and not as UILabel, so whenever you pass the value to labelNumber1, the value will be containing in this variable and it will not be shown in the screen since it is just a normal string. So what you can do is, declare the labelNumber1 as UILabel
class View2: UIViewController {
#IBOutlet var labelNumber1: UILabel!
.....
}
and in your prepare for segue in ViewController1 make the following change,
class ViewController1: UIViewController {
.....
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// You can access the VC2 instance from the segue
if let view2 = segue.destinationViewController as? VC2 {
view2.labelNumber1.text = lblResult.text
}
}
However if you still want to keep labelNumber1 as String, and to verify whether the value has been passed from ViewController1 during segue, add below line to viewDidLoad() of View2 ViewController.
class View2: UIViewController {
.....
override func viewDidLoad() {
super.viewDidLoad()
print("labelNumber1: \(labelNumber1)")
}
}
And you can see the printed value of labelNumber1 in the console.
HTH :)

Why two successive page opens with segue

when I am selected button twice will open a new page.
//////
Main viewController
var Country = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
#IBAction func taxiAction(sender: AnyObject) {
let opt = ["1","2","3","4","5"]
Country = opt
performSegueWithIdentifier("viewPlaceSegu", sender: sender)
}
...
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// get a reference to the second view controller
if segue.identifier == "viewPlaceSegu" {
if let secondViewController = segue.destinationViewController as? TableViewPlace {
// set a variable in the second view controller with the String to pass
secondViewController.tnt = Country as! [String]
}
}
}
////
http://i.stack.imgur.com/LKrN7.jpg
I tried so but didn't realize problem .
Does anyone know about this? :)
A likely cause is that you have connected the segue from the button to the new ViewController in the storyboard. When the button is pressed it will load the segue created in storyboard as well as the one created programatically.
If this is the cause then you would just need to delete the storyboard segue and create a new one from the ViewController rather than from the button.