问题描述
我来自AS3的背景,因此可能更容易向我展示我要使用AS3进行的操作.我有一个UIViewController(root),里面有一个ContainerView.我的印象是容器视图的UIViewController是UIViewController(root)的子级.我想在子视图控制器(容器视图)上按下一个按钮,然后将该事件冒泡到父视图(根UIViewController).在AS3中,我会遇到这样的情况
I'm coming from as AS3 background so it might be easier for me to show you what I'm trying to do with AS3. I have a UIViewController(root) and inside that I have a ContainerView. I am under the impression that the container view's UIViewController is a child of the UIViewController(root). I would like a button to be pressed on the child view controller (container view) and bubble that event up to the parent(root UIViewController). In AS3 I would have something like this
根类创建子类
var childClass = new ChildClass()
childClass.addEventListener("buttonWasPressed", callThisFunction);
private function callThisFunciton(e:Event):void
{
// move the child view
TweenLite.to(childClass,1,{x:100});
}
在子类中,我有一个按钮函数,该函数将部署此事件并将其扩展到父级.
And in the Child Class I have a button function that would deploy this event that would bubble up to the parent.
dispatchEvent(new Event("buttonWasPressed", true));
我不确定该怎么做是让根VC监听该事件.因为我使用的是containerView,所以我不确定如何为该子VC设置出口,并听听该子在做什么.我可以控制从IB到VC的拖动,但这只是为代表容器视图的UIView创建了一个出口.当我打印一些文本时,我可以看到在父VC之前先实例化了子视图控制器.
What I'm not sure how to do is get the root VC listening for that event. Because I'm using a containerView I'm not sure how to set up an outlet to that child VC and listen to things that the child is doing. I can control drag from the IB to the VC, but that just created an outlet for a UIView that represents the container view. When I println some text, I can see that the child view controller is being instantiated first before the parent VC.
我发现我认为此帖子指向了正确的方向. https://craiggrummitt.wordpress.com/2014/07/14/communication-between-objects-in-objective-c-and-swift-compared-with-actionscript-part-5/
I found this post that I think is pointing in the right direction.https://craiggrummitt.wordpress.com/2014/07/14/communication-between-objects-in-objective-c-and-swift-compared-with-actionscript-part-5/
但是我遇到了一个错误,很可能是因为我不确定如何在容器视图内建立从父VC到子VC的连接.我环顾四周,似乎找不到关于该主题的太多信息.
But I'm getting an error, most likely because I'm not sure how to make the connection from parent VC to child VC that's inside the container view. I have looked around and I can't seem to find much information on the topic.
感谢您的帮助!
推荐答案
有两种方法:
1)使用委托协议(推荐)
a)在您的孩子中,创建一个委托协议,并在将保留委托的子VC上创建一个可选属性.
a) In your child, create a delegate protocol, and an optional property on the child VC that will hold the delegate.
protocol ChildViewControllerDelegate {
}
class ChildViewController: UIViewController {
var delegate:ChildViewControllerDelegate?
}
b)在委托协议中,创建一个将在按下按钮时调用的方法,并在子代中实现一个buttonWasPressed()
方法,该子方法在委托上调用此方法. (您需要将此方法与情节提要中的按钮关联起来)
b) In the delegate protocol create a method that will be called when the button was pressed, and implement a buttonWasPressed()
method in the child that calls this method on the delegate. (You'll want to connect this method up with a button in the storyboard)
protocol ChildViewControllerDelegate {
func childViewControllerDidPressButton(childViewController:ChildViewController)
}
class ChildViewController: UIViewController {
var delegate:ChildViewControllerDelegate?
@IBOutlet weak var button: UIButton!
@IBAction func buttonWasPressed(sender: AnyObject) {
self.delegate?.childViewControllerDidPressButton(self)
}
}
c)使您的父视图控制器符合子协议
c) Make your parent view controller conform to the child protocol
class ParentViewController: UIViewController, ChildViewControllerDelegate {
func childViewControllerDidPressButton(childViewController: ChildViewController) {
// Do fun handling of child button presses!
}
}
c)当孩子被嵌入父母中时,会运行一种特殊的segue,称为嵌入segue.您可以在情节提要中看到它-这是将孩子与父母联系起来的那一行:
c) When the child is embedded in the parent a special kind of segue is run called an embed segue. You can see it in the storyboard - it’s the line that connects the child to the parent:
在情节提要中为该序列添加标识符:
Add an identifier to that segue in the storyboard:
以及在父视图控制器中为其设置的常量:
And a constant for it in the parent view controller:
struct Constants {
static let embedSegue = "embedSegue"
}
class ParentViewController: UIViewController, ChildViewControllerDelegate {
func childViewControllerDidPressButton(childViewController: ChildViewController) {
// Do fun handling of child button presses!
}
}
d)然后在父视图控制器中,覆盖prepareForSegue()
方法,并检查segue.identifier
是否与您设置为标识符的内容匹配.如果是这样,则可以通过segue.destinationViewController
获得对子视图控制器的引用.将此作为您的子视图控制器,然后可以将父级设置为其委托:
d) Then in the parent view controller, override the prepareForSegue()
method and check if segue.identifier
matches what you had set as the identifier. If it does, then you can get a reference to the child view controller through segue.destinationViewController
. Cast this as your child view controller, then you can set the parent to be its delegate:
struct Constants {
static let embedSegue = "embedSegue"
}
class ParentViewController: UIViewController, ChildViewControllerDelegate {
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == Constants.embedSegue {
let childViewController = segue.destinationViewController as ChildViewController
childViewController.delegate = self
}
}
func childViewControllerDidPressButton(childViewController: ChildViewController) {
// Do fun handling of child button presses!
}
}
e)赢!
2)使用NSNotification和NSNotificationCenter
您可以将其视为与ActionScript事件类似,但是它们不会像AS这样在视图层次结构中自动冒泡.而是通过NSNotificationCenter
在全局范围内分发通知.我更喜欢仅在有多个对象需要侦听一个特定事件时使用它.
You can think of these as similar to ActionScript events, however they don’t automatically bubble up through the view hierarchy like AS. Instead notifications are dispatched globally through NSNotificationCenter
. I prefer to only use this when there are multiple objects that need to listen for one particular event.
您可以在此处找到有关NSNotification
和NSNotificationCenter
的更多信息: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/
You can find more information about NSNotification
and NSNotificationCenter
here: https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/
这篇关于迅速将事件调度到父ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!