调用viewWillAppear时显示视图控制器的问题被解除 [英] Issue with calling viewWillAppear of presenting view controller when presented one is dismissed

查看:453
本文介绍了调用viewWillAppear时显示视图控制器的问题被解除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个基本视图控制器说AVC,它提供另一个视图控制器说BVC当前上下文如下:

I currently have a base view controller say AVC which present another view controller say BVC over current context like this:

let bvc: BVC = sb.instantiateViewControllerWithIdentifier("BVC") as! BVC
bvc.modalPresentationStyle = UIModalPresentationStyle.OverCurrentContext
self.presentViewController(bvc, animated: true, completion: nil)

然后我在BVC和dismiss中设置一个值,使用相同的值在AVC的viewWillAppear中执行一个函数。但是,我注意到在呈现OverCurrentContext时,在关闭时,viewWillAppear不会被调用。

Then I am setting a value in BVC and on dismiss, am using the same value to perform a function in the viewWillAppear of AVC. However, I noticed that when presenting OverCurrentContext, on dismiss, the viewWillAppear does not called.

我该如何解决这个问题?我需要一个半透明的呈现视图,因此需要使用OverCurrentContext。

How do I go about fixing this? I need a semi-transparent presented view and hence would need to use OverCurrentContext.

谢谢!

推荐答案

bhalla,

两种方式,

方式1

您可以使用委托和协议:)

You can make use of delegates and protocol for this :)

在BVC中声明协议并在AVC中确认。当您在BVC完成调用协议方法并让AVC解除BVC时,您也可以将数据从BVC传递到AVC。

Declare a protocol in BVC and confirm it in AVC. And when you are done in BVC call the method of protocol and let the AVC dismiss BVC and by doing that you can pass the data from BVC to AVC as well.

示例:

在BVC中声明如下的协议

Declare a protocol like the one below in BVC

protocol letsCallParent{
    func amDoneHere(dataIwantToPass : String)
}

声明属性保持视图控制器在BVC中确认此协议的引用

Declare a property to hold the reference of view controller confirming to this protocol in BVC

var myParent : letsCallParent?

当你完成BVC并且你必须解雇你自己在委托中调用方法并通过你希望传递的数据:)现在,期望父母(AVC)解雇BVC而不是自己调用dismissViewController是一个很好的编码习惯。)

And when you are done with BVC and you have to dismiss youself call the method in delegate and pass the data you want to pass :) Now it would be good coding practice to expect the parent (AVC) to dismiss BVC rather then call dismissViewController on self :)

if let parent = myParent {
    parent.amDoneHere("Hi am Done here")
}

在AVC中使用

class AVC: UIViewController,letsCallParent{

并且在呈现BVC之前将其指定为BVC的myParent:)

and after instantiating BVC before presenting it assign the self as myParent of BVC :)

let bvc: BVC = sb.instantiateViewControllerWithIdentifier("BVC") as! BVC
bvc.myParent = self

并最终实施协议方法:)

and finally implement the protocol method :)

func amDoneHere(dataIwantToPass: String) {
   print("\(dataIwantToPass)")
   //if you want to dismiss BVC call 
   //if you have pushed bvc
   //self.navigationController?.popToViewController(self, animated: true)
   //if you have presented
   //self.dismissViewControllerAnimated(true, completion: nil)
}

WAY 2

利用unwind segue :)利用unwind segue将消除编写委托和协议的需要而且所有:)也提供了父视图控制器也有机会访问子视图控制器:)
一旦你有权访问儿童VC这是BVC你可以从中读取数据:)

Make use of unwind segue :) making use of unwind segue will eliminate the need to write delegate and protocols and all :) also provides the parent view controller an opportunity to access the child view controller as well :) Once you have an access to child VC which is BVC you can read the data from it :)

示例:

声明展开segue :)
如果你希望BVC在获得d时调用AVC的metod imissed,在AVC中声明一个unwind segue方法:)

Declare an unwind segue :) If you want BVC to call a metod of AVC when it gets dimissed, declare a unwind segue method in AVC :)

@IBAction func cancelChildVC(segue:UIStoryboardSegue) {
        let childVC : SecondViewController = segue.sourceViewController as! SecondViewController
        print(childVC.name)
        self.dismissViewControllerAnimated(true, completion: nil)
}

该方法的签名是固定的:)它必须是@IBAction并且它必须接受segue作为参数:)

The signature of the method is fixed :) It has to be a @IBAction and it has to accept segue as paramter :)

只有在声明了这个方法之后:)转到你的故事板并选择BVC并按住控制并从按钮或你在BVC中的任何其他视图中将其解除,将其移至BVC中的退出选项:)

Only after declaring this method :) go to your story board and select the BVC and hold control and drag from the button or any other view you have in BVC to dismiss it, to Exit option in BVC :)

请查看下面提供的gif以便清楚了解:)

Have look at gif provided below for clear understanding :)

如果你做得正确,你会看到你在AVC声明的方法出现了弹出选项:)选择它:)这就是它:)每当你点击按钮或者从中将控件拖动到Exit的视图,AVC中的方法获取调用:)

If you do it correctly you will see the method that you have declared in AVC coming up as pop up option :) select it :) Thats it :) Whenever you tap on the button or the view from which you have dragged the control to Exit, the method in your AVC gets called :)

在AVC的方法中,您可以访问BVC实例,因为现在您可以访问segue实例:)记住直接segues的prepareForSegue方法,这类似于for unwind segue:)

In AVC's method you can access the BVC instance because now you have the access to segue instance :) remember prepareForSegue method for direct segues, this is similar to that for unwind segue :)

let childVC : SecondViewController = segue.sourceViewController as! SecondViewController
print(childVC.name)
self.dismissViewControllerAnimated(true, completion: nil) 

一旦有权访问BVC访问其数据,请注意 segue.sourceViewController 的用法:)

Notice the usage of "segue.sourceViewController" once you have the acces to BVC access its data :)

希望我的回答有所帮助:)

Hope my answer helped :)

这篇关于调用viewWillAppear时显示视图控制器的问题被解除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆