如何从导航栏上的右键将数据传输到另一个ViewController?

问题描述 投票:0回答:3

我有一个带有两个按钮的导航栏:“上一步”和“添加”。单击添加按钮时,我需要将数据(UIImage)传输到第二个View Controller(ViewControllerFilters2)。我有“准备查询”功能,但它不起作用。当我单击“添加”时,我转到第二个控制器,但是imageView没有图像。

我在视图控制器上还有另一个按钮“应用”,对于此按钮,此功能有效,但对导航栏上的按钮不起作用。

我试图更改我的代码,但是我没有足够的知识来做。而且不知道问题出在哪里。

我希望我的添加按钮执行与应用按钮相同的功能,即我想删除应用按钮并将其更改为添加按钮

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!

}
ios swift xcode uiviewcontroller uiimageview
3个回答
0
投票

为了解决问题,您应该执行以下操作:

  • 请确保在情节提要板上创建了一个segue(从导航项到下一个视图控制器)。如果您不知道如何操作,请检查this。您可以从情节提要中添加条形按钮,从而创建segue。

  • [目前,您正在呼叫navigationController?.pushViewController,与not,您应该做的是呼叫:

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


0
投票

更改此:

@objc func addTapped() {

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

对此:

@objc func addTapped() {

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

正如艾哈迈德·F所说,您正在调用navigationController?.pushViewController,它与segue无关,因此您需要在按下视图控制器之前设置图像。


0
投票
@objc func addTapped() {

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

这应该可以解决您的问题,因为navigationController?.pushViewController与 [nothing”>

© www.soinside.com 2019 - 2024. All rights reserved.