问题描述
我的第一个视图控制器上有一个名为 showSettings
的可选 bool
变量,名为 ViewController
,我从 SecondViewController
弹回 ViewController
。
I have an optional bool
variable called showSettings
on my first view controller which is called ViewController
, and I'm popping from SecondViewController
back to ViewController
.
在我弹出之前,我想将bool设置为true。由于 ViewController
在内存中,因此实例化另一个视图控制器似乎有误。
Before I pop, I want to set the bool to true. Seems wrong to instantiate another view controller since ViewController
is in memory.
最好的方法是什么?我没有使用故事板,如果这对你的答案很重要。
What's the best way to do this? I'm not using storyboards, if that's important for your answer.
感谢您的帮助
推荐答案
所以我想出来了,主要来自这篇文章 -
So I figured it out, based mostly from this post – http://makeapppie.com/2014/09/15/swift-swift-programmatic-navigation-view-controllers-in-swift/
在 SecondViewController
中,在类声明上方,添加以下代码:
In SecondViewController
, above the class declaration, add this code:
protocol SecondVCDelegate {
func didFinishSecondVC(controller: SecondViewController)
}
然后在 SecondViewContoller
里面添加一个类变量:
Then inside of SecondViewContoller
add a class variable:
var delegate:MeditationVCDelegate! = nil
然后在您的按钮所针对的函数内部添加:
Then inside of your function that your button targets, add this:
self.navigationController?.popViewControllerAnimated(true)
delegate.didFinishSecondVC(self)
我们在这里做的是在 SecondViewController
中执行pop,而不传递任何数据,但是我们定义了一个协议,我们将在 ViewController
中使用它来处理数据。
What we're doing here is doing the pop in SecondViewController
, and not passing any data, but since we've defined a protocol, we're going to use that in ViewController
to handle the data.
接着,在 ViewController
中,添加你在 SecondViewController中定义的协议
到类列表 ViewController
继承自:
So next, in ViewController
, add the protocol you defined in SecondViewController
to the list of classes ViewController
inherits from:
class ViewController: UIViewController, SecondVCDelegate { ... your code... }
你需要添加我们在新协议中定义的函数,以使编译器满意。在 ViewController
的类中,添加:
You'll need to add the function we defined in the new protocol in order to make the compiler happy. Inside of ViewController
's class, add this:
func didFinishSecondVC(controller: SecondViewController) {
self.myBoolVar = true
controller.navigationController?.popViewControllerAnimated(true)
}
在 SecondViewController
我们正在调用 didFinishSecondVC
,我们正在调用这个方法在 ViewController
类中,我们正在弹出的控制器。它类似于我们在 SecondViewController
中编写此代码,但我们已经在 ViewController
中编写了它,我们就是使用委托来管理两者之间的消息传递。
In SecondViewController
where we're calling didFinishSecondVC
, we're calling this method inside of the ViewController
class, the controller we're popping to. It's similar to if we wrote this code inside of SecondViewController
but we've written it inside of ViewController
and we're using a delegate to manage the messaging between the two.
最后,在 ViewController
中,在函数中我们是定位到推送到 SecondViewController
,添加以下代码:
Finally, in ViewController
, in the function we're targeting to push to SecondViewController
, add this code:
let secondVC = secondViewController()
secondVC.delegate = self
self.navigationController?.pushViewController(secondVC, animated: true)
就是这样!您应该设置为在不使用故事板的情况下在两个视图控制器之间传递代码!
That's it! You should be all set to pass code between two view controllers without using storyboards!
这篇关于Swift - 使用popViewController并将数据传递给您要返回的ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!