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

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

Related

Segue Image to next ViewController (Swift)

Im trying to pass a Cached/Downloaded image to my display view controller that will be segued with different images. I can download the image and set it to a UIImageView on my current controller, or if I manually download an image that's UIImage i can segue it as I assign it to ImageStore variable. However when downloading my image through cache/download method, it arrives as UIImageView, how can I pass that as variable to another controller?
#IBOutlet weak var Image1: UIImageView!
var ImageStore: UIImage!//transfer image code to next screen
var ImageStore: UIImage!//transfer image code to next screen
#IBAction func Chart1Click(_ sender: Any)//chart 1
{
performSegue(withIdentifier: "Chart1", sender: nil)
}
#IBAction func Chart2Click(_ sender: Any)//chart 2
{
performSegue(withIdentifier: "Chart2", sender: nil)
}
#IBAction func Button(_ sender: Any)//download charts
{
Image1.sd_setImage(with: URL(string: "https://www.vedur.is/photos/flugkort/PGDE14_EGRR_0000.png"), placeholderImage: UIImage(named: "placeholder.png"))
//assing the Image1 to ImageStore ??
//more downloaded here
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if (segue.identifier == "Chart1")
{
let destinationViewController = segue.destination as! ChartAController
destinationViewController.TransferImage = ImageStore
print("Chart 1 Segue")
}
if (segue.identifier == "Chart2")
{
let destinationViewController = segue.destination as! ChartAController
destinationViewController.TransferImage = ImageStore2
print("Chart 2 Segue")
}
}
Just send
ImageStore.image
It should solve the issue. If you want to send image instead of imageView.

Attempting to pass data from one viewcontroller to another using segues

I have two viewcontrollers setup and set the segue connections as well. I'm attempting to pass data from one of the VC's to the other. Using the below code and using the func override works. What i want to do is only have the override func prepare fire off when a button is pressed and when i paste the override func code into a button action, it doesnt work - the label on the first VC doesnt update - it stays to the default value i set.
First ViewController code:
var testLabel1:String = "default"
#IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
testLabel?.text = testLabel1
// Do any additional setup after loading the view.
}
Second ViewController code:
override func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is ViewController
{
let vc = segue.destination as? ViewController
vc?.testLabel1 = "Success"
}
}
Button action that ive tried
#IBAction func actionBtn(_ sender: Any) {
func prepare(for segue: UIStoryboardSegue, sender: Any?)
{
if segue.destination is ViewController
{
let vc = segue.destination as? ViewController
vc?.testLabel1 = "Success"
}
}
}
You need to trigger it with performSegue
class FirstVc:UIViewController {
#IBOutlet weak var lbl:UILabel!
#IBAction func goToSecond(_ sender: Any) {
self.performSegue(withIdentifier:"YourSegueName",sender:nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "YourSegueName" {
let vc = segue.destination as! SecondVC
vc.testLabel1 = "Success"
}
}
func setData(_ str:String){
self.lbl.text = str
}
}
class SecondVC:UIViewController {
var testLabel1 = ""
weak var delegate:FirstVc?
func goBack() {
self.delegate?.setData("FromSecond")
self.dismiss(animated: true, completion: nil)
}
}

Value not passing between viewControllers

Struggling to get my viewControllers to send value from the main viewController to a second. I want it to happen on a button click, I'm going to get the value from the button and pass it to the new form. But it just isn't working.
Code for main ViewController
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func butClick(_ sender: UIButton) {
NSLog("Button Pressed : %#",[sender .currentTitle])
//var tt = [sender .currentTitle]
// Create the view controller
let vc = TimesTablesViewController(nibName: "TimesTablesViewController", bundle: nil)
vc.passedValue = "xx"
self.performSegue(withIdentifier: "pushSegue", sender: nil)
}
}
Code for second viewController called TimesTablesViewController:
class TimesTablesViewController: UIViewController {
#IBOutlet weak var titleLabel: UILabel!
var passedValue:String = ""
override func viewDidLoad() {
super.viewDidLoad()
titleLabel?.text = "\(passedValue) Times Table"
}
}
I've followed tutorials but can't seem to solve the problem! Thanks for any help!
Replace
self.performSegue(withIdentifier: "pushSegue", sender: nil)
with
self.present(vc,animated:true,completion:nil)
or ( if the current vc is inside a naigation )
self.navigationController?.pushViewController(vc,animated:true)
Using
self.performSegue(withIdentifier: "pushSegue", sender: nil)
is fit with storyboards not xibs and if this your case then you need to use the above line only inside the button action with implementing this method inside the source vc
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "pushSegue" {
if let nextViewController = segue.destination as? TimesTablesViewController{
nextViewController.passedValue = "xx"
}
}
}
I’m assuming that the new view controller is appearing, but you’re simply not seeing the data. If so, you’re evidently using storyboards. The TimesTablesViewController(nibName:bundle:) only works if you’re using XIB/NIBs and manually presenting new view controller.
If you’re really using storyboards, simplify your butClick method:
#IBAction func butClick(_ sender: UIButton) {
NSLog("Button Pressed")
performSegue(withIdentifier: "pushSegue", sender: self)
}
But implement prepare(for:sender:):
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? TimesTablesViewController {
destination.passedValue = "xx"
}
}
Assuming the above fixes your problem, I might suggest a further simplification. Notably, if your butClick(_:) method is really only calling performSegue, you can segue to this next scene without any #IBAction method at all:
remove butClick(_:) entirely;
remove the connection between the button and the butClick method in IB, on the “Connections Inspector” tab in the right panel; and
control-drag from the button previously hooked up to butClick(_:) to the scene for TimesTablesViewController.
That will simplify your code further.

Pass image from one view controller to another controller gives nil

I am trying to send an image to the preview controller as shown below. My issue is that in the preview controller newImage is nil
Below is the code:
FirstViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("In here")
if segue.identifier == "showImage" {
let secondViewController = self.storyboard!.instantiateViewController(withIdentifier: "showImage") as! showImageController
secondViewController.newImage = UIImage(named: imageArray[0])
}
}
PreviewController
#IBOutlet weak var imageHolder: UIImageView!
var newImage: UIImage!
override func viewDidLoad() {
super.viewDidLoad()
imageHolder.image = newImage
// Do any additional setup after loading the view.
}
You should use segue.destination not create a new one , like this
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
print("In here")
if segue.identifier == "showImage" {
let next = segue.destination as! showImageController
next.newImage = UIImage(named: imageArray[0])
}
}
instantiateViewController calls viewDidLoad() so you will not have setup your image when it gets called. Move the code to viewWillAppear() and it will work

Passing data between View Controllers (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