-4

I have two viewController : first and second. Now, first viewcontroller have a tableView, when I click a cell, jump to the second viewController and I want to send a image name from first to second, then second controller presentation the image how to do this?

The language is swift. tips: do not use storyboard,just code!!

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kevn liu
  • 33
  • 1
  • 7

2 Answers2

0

First, I'll let you know there are a ton of examples of this.

In whatever method you are telling the second view controller to present:

let secondViewController: SecondViewController = SecondViewController() secondViewController.imageFromFirstViewController = self.imageToPass self.presentViewController(secondViewController, animated:true, completion:nil)

The syntax may not be perfect because I haven't done much Swift development but you should get the idea.

AdamPro13
  • 7,232
  • 2
  • 30
  • 28
  • thank you, my question is how to issue a variable let other viewController know, then i can do like this: `second.image = self.imageToPass` – kevn liu Oct 31 '14 at 23:26
  • Yeah just create a non-private `var` in the second view controller. Then the first view controller can access it. – AdamPro13 Oct 31 '14 at 23:36
  • er. i mean you idea, i issue a var before `class secondViewController:UIViewController { }` but, the first cant use second.var from second to use. it's mean first cant see the var – kevn liu Nov 01 '14 at 00:01
0

Create a property in your second view, lets say:

@property UIImage *myImage

Now, in your first view, you have the image from your cell that was tapped - so all thats left is to tell the second view what it is.

let myNewView: ViewClassGoesHere = ViewClassGoesHere()
myNewView.myImage = imageFromFirstView

As you can see, it's pretty much the same as in Objective-C, only the way you instantiate the second view is slightly different.

Jason Coulls
  • 182
  • 1
  • 1
  • 10
  • my xcode version is 6.1, int the seconde viewController i cant use "@property" to issue , i don't know why . in my project, only use swift – kevn liu Oct 31 '14 at 23:23