简体   繁体   中英

Got an image from one ViewController, how can I display it on the other ViewController

I have two ViewController, one is called ListViewController, the other is called UploadViewController. ListViewController is the first view after launching, and by pressing "new" button on it, it will jump to UploadViewController. My final question is: I got an image through UploadViewController.cs, and I want to display it on the ListViewController, how can I do that?

My way is to pass image under UploadViewController to ListViewController, through a function.

In UploadViewController, I defined "listView", and its type is ListViewController, so that I can use the function in ListViewController. (That's my aim, not sure whether this step is correct.)

ListViewController listView = new ListViewController((IntPtr)null);

Then in UploadViewController,

protected void Handle_FinishedPickingMedia (object sender, UIImagePickerMediaPickedEventArgs e)
        {
            UIImage originalImage = e.Info[UIImagePickerController.OriginalImage] as UIImage;
            selectedImage = originalImage;
            listView.SetImage (selectedImage);
            this.imagePicker.DismissModalViewController (true); 
        }

When implementing listView.SetImage (selectedImage) , it will jump back to ListViewController, and execute:

public void SetImage (UIImage image) 
        {
            imageView4.Image = image; //imageView4 is a imageView defined in mainStoryboard
        }

Problem is: the app will crash after implementing SetImage. Information shows imageView4 or ListViewController not defined or being disposed. But why? Both of them are initially created in mainStoryboard, when are they disposed? How can I avoid this? Or, I shouldn't just jump back to a viewController and change its look without any segue?

I will really appreciate it for answers to any of my questions! Thanks!

You have to put an Image box in your ListViewController . Should look something like this:

Image img = new Image();
img.Name = "myImage";
listView.Add.Children(img);
myImage.ItemSource = imageView4;

Assign image to UIImageView after "viewDidLoad" of your controller, to which belongs that UIImageView.

For example you can create private variable, and store there image until viewDidLoad method gets called.

private UIImage imageToShow;

public void SetImage (UIImage image) 
{
     this.imageToShow = image; 
}

public override void ViewDidLoad ()
{
     base.ViewDidLoad ();
     this.imageView4.Image = this.imageToShow;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM