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.
Related
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)
}
}
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.
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
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
I've tried a lot of methods i couldn't get it to work
ViewController 1 have : Collectionview > Cell > image inside the cell
ViewController 2 want to display the image which in VC 1
When you click on cell it has segue to push you to VC 2
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popup" {
let viewController: friendpopup = segue.destination as! friendpopup
let indexPath = sender as! NSIndexPath
let nicknamex = self.nicknameArray[indexPath.row]
let usernamex = self.userArray[indexPath.row]
let photox = self.friendsphotos[indexPath.row] // the photos PFFiles i think
viewController.snick = nicknamex
viewController.suser = usernamex
viewController.sphoto = // ????
}
nickname and user works fine only the image i couldn't display it.
I tried when you click on cell it will send the image to var but isn't working
var photovar:UIImage!
didSelectItemAt( self.photovar = cell.profilepic.image)
then in prepareSegue( viewcontroller.sphoto = self.photovar)
isn't woking, Anyone could help me to fix that to display the image? Thanks
Tightening up your code a bit....
First VC
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "popup" {
if let vc = segue.destination as? friendpopup {
let indexPath = sender as! NSIndexPath
vc.snick = self.nicknameArray[indexPath.row]
vc.suser = self.userArray[indexPath.row]
vc.sphoto = self.friendsphotos[indexPath.row]
}
}
}
Second VC:
// making assumptions on variable types
var snick:String!
var suser:String!
var sphoto:UIImage!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
// do work here
if sphoto != nil {
imageView.image = sPhoto
} else {
// set a default image here
}
}
Most of this is similar to your code, but one last note - if the first 2 properties are coming across correctly, check in the first VC that you are pulling the image out properly.