0

I have a Navigation Bar with two buttons: Back and Add. I need to transfer data (UIImage) to second View Controller (ViewControllerFilters2), when I click on the add button. I have the "prepare for segue" function but it doesn't work. When I click on "Add", I go to the second controller, but imageView doesn't have an image.

I also have another button "Apply" on view controller, and for this button this function works but not for button on navigation bar.

I tried to change my code, but I don't have a lot of knowledge to do it. And don't know, where is the problem.

I want my Add button do the same function as Apply button, i.e. I want delete Apply button and change it to Add button

override func viewDidLoad() {
    super.viewDidLoad()

    navigationItem.rightBarButtonItem = UIBarButtonItem(title: "✔", style: .plain, target: self, action: #selector(addTapped))
}

@objc func addTapped() {

    let filterTwoController = self.storyboard?.instantiateViewController(withIdentifier: "filter2") as! ViewControllerFilters2
    self.navigationController?.pushViewController(filterTwoController, animated: true)

}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

        let nextController = segue.destination as? ViewControllerFilters2
        nextController?.filteredImage = imageView.image!

}

3 Answers3

3
@objc func addTapped() {

let filterTwoController = self.storyboard?.instantiateViewController(withIdentifier: "filter2") as! ViewControllerFilters2
filterTwoController.filteredImage = imageView.image!
self.navigationController?.pushViewController(filterTwoController, animated: true)
}

that should fix your issue, because the navigationController?.pushViewController has nothing to do with the segue

jbiser361
  • 949
  • 7
  • 20
  • I change it, but I have the same problem. It doesn't work, because image view on the filter2 controller doesn't have an image. It is nil. – kosya_kosia Oct 01 '19 at 07:08
0

In order to resolve the issue, you should do the following:

  • Make sure that there is a segue created (from the navigation item to the next view controller) on your storyboard. If you don't know how to do it, check this. You could add the bar button from the storyboard thus create the segue.

  • Currently, you are calling navigationController?.pushViewController, which is not related to the segue, what you should do instead is to call:

    self.performSegueWithIdentifier ("SecondViewController", sender: self)

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • I add button on navigation bar without storyboard. I can't do a segue in storyboard, because there I don't have a button "Add" on my ViewController. So, I do it with code in my addTapped method. And I have a segue between two controllers, when I launch my app. But I don't have image in imageView in the second controller. It is nil – kosya_kosia Oct 01 '19 at 07:27
0

Change this:

@objc func addTapped() {

let filterTwoController = self.storyboard?.instantiateViewController(withIdentifier: "filter2") as! ViewControllerFilters2
self.navigationController?.pushViewController(filterTwoController, animated: true)
}

to this:

@objc func addTapped() {

let filterTwoController = self.storyboard?.instantiateViewController(withIdentifier: "filter2") as! ViewControllerFilters2
filterTwoController.filteredImage = imageView.image!
self.navigationController?.pushViewController(filterTwoController, animated: true)
}

As Ahmad F said, you are calling navigationController?.pushViewController, which is not related to the segue, so you need to set the image before you push the view controller.

ZombieMK
  • 1
  • 1
  • I change it, but I have the same problem. It doesn't work, because image view on the filter2 controller doesn't have an image. It is nil. – kosya_kosia Oct 01 '19 at 07:04