简体   繁体   中英

Sending data from one uiviewcontroller to another navigationcontroller using present modally

I am new to swift i am using storyboard and have used navigationcontrollers to connect from one viewcontroller to another. I want to send the name of the image clicked to the next viewcontroller which is connected modally in storyboard from the imageView. I searched lot about transferring data from oneviewcontroller to another viewcontroller connected with navigationcontroller modally but no solution was available. Please let me know if any of the code is required as i dont know how to go ahead with it. I know this might be silliest question but posting this after searching a lot on net.

EDIT according to @uraimo reply.

Do i need to provide name to every segue i created on storyboard?. I have 2 fixed images on viewcontrollerA and i have placed a uibutton with transparent background and no text on each of them and then ctrl drag to navigation controller of viewcontrollerB for presenting modally and unwinding the backbutton ie UIBarButtonItem to viewcontrollerA by ctrl drag the back button of viewcontrollerB to exit of the viewcontrollerB and unwinding it.

This is how i have created navigation from any of the image click out of 3 images of viewcontrollerA to viewcontrollerB and back to viewcontrollerA on back button click of viewcontrollerB. Please let me know if i am doing anything wrong and will your prepareForSegue code be useful in accomplishing my task.

Basically, both using IB or when you do it programmatically, you have to configure your new viewcontroller with all the data it needs before the segue is performed (or the controller is presented via code).

In your case, just set the image name (your custom view controller class YourViewController should have a specific String property to hold this value) overriding prepareForSegue in the current view controller class:

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        if segue.identifier == "yourModalSegueIdentifier" {
            let imgName= (sender as! UIImageView)
            let destination = segue.destinationViewController as UINavigationController
            let yourController = destination.topViewController as YourViewController
            yourController.imageName= <name here>
        }
}

This solves the passing data question.

But in your case, you need the name of the clicked image, and that can be only obtained adding a click event through a UIGestureRecognizer to the UIImageView .

So, you'll need a uigesturerecognized that on click will perform the segue you've created. And also, you will not be able to get the name of the image asset (the one you use the creating an UIImage using imageNamed: ) because it's not available anymore, and yo'll have to use the accessibilityIdentifier .

This makes everything way more complicated, it seems it could be done for the most part graphically here and here (but it's not really clear how to do it), but it's usually done via code.

Simplifying it a bit using a global variable:

var currentImage = ""

func viewDidLoad(){
    ...
    ...
    let aTap = UITapGestureRecognizer(target: self, action: Selector("imageTapped:"))
    aTap.numberOfTapsRequired = 1

    //For every image, configure it with recognizer and accessibilityId:
    firstImage.userInteractionEnabled = true
    firstImage.addGestureRecognizer(aTap)
    firstImage.accessibilityIdentifier = "firsImage"
    ...
}

func imageTapped(recognizer:UITapGestureRecognizer) {
    let imageView = recognizer.view as! UIImageView
    currentImage = imageView.accessibilityIdentifier
    self.performSegueWithIdentifier("yourModalSegueIdentifier", sender: self)
}

And change this:

yourController.imageName= <name here>

to this:

yourController.imageName= currentImage

Update:

  • Do i need to provide name to every segue i created on storyboard?

Yes, it's the only way to identify them, every UIStoryboardSegue has an identifier. But remember, segues are not the only way to go from a controller to another, if you do it completely programmatically (no segues) you usually call "presentViewController". Segues are a storyboard concept.

Again, regarding the segue name/identifier, you didn't need it until now because you never referenced that segue from your code, you need it for both prepareForSegue and performSegueWithIdentifier. Just select the segue and give it a name on the right inspector pane.

The structure you describe seems ok, the only thing it's that i'm not so sure that the UIButtons are really needed, try with a modal segue from the imageview or directly from the viewcontroller to the destination view controller.

Update 2:

If you are starting and need a free course that will teach you the basics and also make you build a few interesting ios apps i recommend hackingwithswift .

check out how I did this

// In a storyboard-based application, you will often want to do a little preparation before navigation
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

      super.prepare(for: segue, sender: sender)

      switch(segue.identifier ?? "") {

      case "AddItem":
          let destination = segue.destination as? UINavigationController
          guard let itemViewController = destination?.topViewController as? ItemViewController else {
              fatalError("Unexpected destination: \(segue.destination)")
          }
          itemViewController.collection = collection

      case "EditCollection":
          guard let collectionViewController = segue.destination as? EditCollectionViewController else {
              fatalError("Unexpected destination: \(segue.destination)")
          }
          collectionViewController.collection = collection

      default:
          fatalError("Unexpected Segue Identifier; \(segue.identifier)")
      }
  }

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