向下滑动以关闭另一个视图控制器时,有没有办法将数据传回视图控制器?

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

我有一个视图控制器,我们称之为 vc1,它使用 prepare for segue 将一些数据传递给另一个 (vc2),然后调用 performSegue。

当通过向下滑动关闭 vc2 时,是否有办法将一些数据从 vc2 传回 vc1?

谢谢,

编辑——

抱歉缺乏信息,对 swift 非常陌生,所以不确定在这种情况下要问的正确问题。

详细地说,目前问题的根源是 vc2 没有以编程方式被解雇。即当前没有调用函数,用户向下滑动即可将其关闭。

我可以包含一些功能来捕获这种解雇,并使用它来将数据发送回 vc1 吗?

如果可能,我宁愿不向 vc2 添加任何按钮。

再次道歉,我感谢所有已经提供的帮助!

ios swift xcode xcode-storyboard
4个回答
1
投票

试试这个

class VCOne: UIViewController {

//Create a shared instance of VCOne

 static var sharedInstance:VCOne?

 //Let the data to be passed back to VCOne is of type string

  var dataToBePassedBack:String?

  override func viewDidLoad() {
    super.viewDidLoad()

   //set the sharedInstance to self
    VCOne.sharedInstance = self

    }

}

Class VCTwo:UIViewController{

 //function in which you are dismissing your current VC you can use the shared 
 instance to pass the data back

func dismissVC(){

  //before dismissing the VCTwo you can set the value for VCOne

VCOne.sharedInstance?.dataToBePassedBack = "data" 

}


}

1
投票
  • 使用ProtocolDelegate你做或其他选项是NSotificationcenter.

0
投票

你这样做的一种方法是创建另一个文件,它是一切的控制器,然后有一个委托,当有新的更改可用时,它总是通知视图控制器。我会走一遍。

     protocol HeadControllerDelegate {
        // Create a function that sends out the data to the delegates when it is called
        // You can use your custom struct here to pass more data easly
        func didReciveNewData(myData: String?)
    }

    struct HeadController {

        // Create a shared instance so that the viewcontroller that conforms to the view as well as when we sends out the data the delegate is correct
        static var shared = HeadController()
        // Creates the delegate, every view can asign it to
        public var delegate: HeadControllerDelegate?

        // Add all your values here you want to pass back
        var myValue: String? {
            // The didSet gets called every time this value is set, and then is it time to call the delegate method
            didSet {
                // Calls the delegates didReciveMethod to notify the delegates that new data exsists
                delegate?.didReciveNewData(myData: myValue)
            }
        }
    }

现在在您的视图控制器类中,您希望数据可用(正如您向下滑动时所说的)

    class ViewController: UIViewController {

    // Here you create a property of the shared instance
    let headController = HeadController.shared

    override func viewDidLoad() {
        super.viewDidLoad()

        // Set yourself as the delegate for the headController delegate to recive data
        headController.delegate = self

    }

}

extension ViewController: HeadControllerDelegate {

    // here will the data be recived
    func didReciveNewData(myData: String?) {
        // handle the data here, you have now got newData

        print(myData)
    }
}

在你想传递数据的类中,你只需这样做。这样做的好处是你可以有多个类或结构写入头控制器数据(只要确保你这样做是认为共享实例)。根据我们的说法,使用委托模式也是一个很好的实践。

class Sender {

    var headController = HeadController.shared

    func sendData(data: String) {

        // Here you change the data of the headcontroller wich will send the data to all the delegates
        headController.myValue = data
    }
}

希望这个答案对您有所帮助。如果您有任何问题,请告诉我。

更新——更简单的解决方案

根据我的说法,这是一个更简单的解决方案,但可扩展性不如前一个。

在 prepareForSegue 中,只需将当前的 viewContorller 作为目标视图控制器中的字段传递。然后当 viewDidDissapear 在新的视图控制器中时,您可以简单地传回数据。别担心,我会告诉你!

准备Segue

 override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if let dc = segue.destination as? SecondViewController {
        dc.viewController = self
    }
}

然后声明第二个ViewContorller如下。当视图关闭时将调用 ViewDidDisappear 方法,因此您可以将数据传递给您在使用 prepare for segue 方法之前设置的视图控制器。

    class SecondViewController: UIViewController {

    var viewController: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    override func viewDidDisappear(_ animated: Bool) {
        (viewController as? ViewController)?.value = 2
    }

}

然后您可以使用 didSet 更新 UI,它会在设置属性时简单地调用,这将在视图 did disappear 方法中完成。

var value: Int = 0 {
    didSet {
        print(value)
        text?.text = "\(value)"
    }
}

希望这有帮助!


0
投票
//SecondViewController
class SecondViewController: UIViewController {
    var dataPass: ((_tempNum : Int) -> Void)?
  
    @IBAction btnCloseClicked(_ sender: UIButton){
        self.dismiss(animanted: true){
           if let callback = dataPass{
              callback(5)
           }
        }
     }     

}

//FirstViewController
class FirstViewController: UIViewController{

    @IBAction btnNextClicked(_ sender: UIButton){
       let storyBoard = UIStoryBoard(name : "Main") 
       let vc = storyBoard.instantiateViewController(withIdentifier: 
       "SecondViewController") as! SecondViewController
       vc.dataPass = { (number) -> in
          //do your code
        }
       self.present(vc, animated: true, completion: nil)

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