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
Related
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.
I'm using segue to open a new window in a xcode app.
The segue is correct, when I tap on the button the new window is shown.
I want to pass a string variable from FirstViewController to DetailViewController but I can't set the variable in the DetailViewController.
This is the code in FirstViewController:
if control == view.rightCalloutAccessoryView {
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as! DetailViewController
controller.Name = "test"
}
performSegue(withIdentifier: "showdetail", sender: self)
}
And this the DetailViewController code
class DetailViewController: UIViewController, WKNavigationDelegate {
var Name: String = ""
override func viewDidLoad() {
super.viewDidLoad()
print(Name)
}
}
Where am I doing wrong?
The method prepare(for must be on the top level of the class.
And please name variables according to the guidelines with starting lowercase letter
func someMethod()
{
if control == view.rightCalloutAccessoryView {
performSegue(withIdentifier: "showdetail", sender: self)
}
}
func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let controller = segue.destination as! DetailViewController
controller.name = "test"
}
}
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)
}
}
I have a problem with my second viewController when trying to send data with segue. The code of Login is:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let secondView = segue.destination as! DataBaseConn
secondView.user=TextUsername.text!
secondView.pass=Pass.text!
}
override func viewDidLoad() {
super.viewDidLoad()
self.performSegue(withIdentifier: "sendData", sender: self)
}
I get the error at this line:
let secondView = segue.destination as! DataBaseConn
Both views have the custom class set.
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