简体   繁体   English

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

[英]Is there a way to pass data back to a view controller when swiping down to dismiss another view controller?

I have a view controller, lets call it vc1, which passes some data to another (vc2) using prepare for segue, and then calling performSegue.我有一个视图 controller,我们称它为 vc1,它使用 prepare for segue 将一些数据传递给另一个(vc2),然后调用 performSegue。

Is there a way to pass some data back from vc2 to vc1 when vc2 is dismissed by swiping down?当通过向下滑动关闭 vc2 时,有没有办法将一些数据从 vc2 传递回 vc1?

Thanks,谢谢,

Edit --编辑 -

Apologies for the lack of information, very new to swift so unsure of the correct question to ask in this situation.对于缺乏信息,swift 非常新,因此不确定在这种情况下要问的正确问题。

To elaborate, the root of the issue at the moment is that vc2 is not dismissed programatically.详细地说,目前问题的根源是 vc2 没有以编程方式被解雇。 ie there is currently no function called, it is simply dismissed by the user swiping down.即当前没有调用 function,它只是被用户向下滑动而解除。

Is there some function that I can include to capture this dismissal, and use it to send data back to vc1?是否有一些 function 可以包含以捕获此解雇,并使用它将数据发送回 vc1?

I would prefer not to add any buttons to vc2 if possible.如果可能的话,我不希望向 vc2 添加任何按钮。

Apologies again, and I appreciate all the help given already!再次道歉,我感谢已经提供的所有帮助!

One way yo do it is to create another file that it the controller of everything and then have a delegate that always notifies the view controllers when new changes are available.这样做的一种方法是创建另一个文件,它是 controller 的所有内容,然后有一个委托,当有新的更改可用时,它总是通知视图控制器。 I will walk it through.我会走过去的。

     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)
            }
        }
    }

Now in your viewcontroller class where you would like the data to be avaiable (as you said when you swipe down)现在在您的视图控制器 class 中,您希望数据可用(正如您在向下滑动时所说的那样)

    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)
    }
}

In the class where you want to pass data you just do it like this.在您想要传递数据的 class 中,您只需这样做。 The beauty of this is that you can have multiple classes or structs that writes to the head controllers data (just make sure you do it thought the shared instance).这样做的好处是您可以拥有多个写入头控制器数据的类或结构(只要确保您认为共享实例就可以了)。 It is also a good pracice according to we to use the delegate pattern.根据我们的说法,使用委托模式也是一种很好的做法。

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
    }
}

Hope this answer helps.希望这个答案有帮助。 If you have any questions please let me know.如果您有任何问题,请告诉我。

UPDATE -- EASIER SOLUTION更新——更简单的解决方案

Here is an easier solution but is less scalable as the previous one according to me.这是一个更简单的解决方案,但根据我的说法,它的可扩展性不如前一个。

In prepareForSegue simply pass over your current viewContorller as a field in the destination view controller.在 prepareForSegue 中,只需将当前 viewContorller 作为目标视图 controller 中的字段传递。 Then when viewDidDissapear in the new view controller you can simply pass back the data.然后当 viewDidDissapear 在新视图 controller 中时,您可以简单地传回数据。 Not to worry, I will show you!不用担心,我会告诉你的!

In prepare for Segue为 Segue 做准备

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

And declare the secondViewContorller as following.并声明 secondViewContorller 如下。 The ViewDidDisappear method will be called when the view has dismissed, and therefore can you pass over the data to the view controller you have set before using the prepare for segue method. ViewDidDisappear 方法将在视图关闭时调用,因此您可以将数据传递给您在使用 prepare for segue 方法之前设置的视图 controller。

    class SecondViewController: UIViewController {

    var viewController: UIViewController?

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

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

}

Then you could update the UI using a didSet, which simply will be called when the property is set, which will be done in the view did disappear method.然后,您可以使用 didSet 更新 UI,它只会在设置属性时被调用,这将在视图的 did 消失方法中完成。

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

Hope this helps!希望这可以帮助!

Try This尝试这个

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" 

}


}
  • Using Protocol And Delegate You Do or Other Option is NSotificationcenter .使用您所做的协议委托或其他选项是NSotificationcenter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM