从弹出框委派 VC
Delegation to Presenting VC, From a Popover
我将 ViewController 拖到我的情节提要板上,使用 popover segue 对其进行 segue,并在 presentingVC 的 prepareForSegue 中设置大小和样式。我的问题是,如果我的弹窗有多个按钮,它们的代码应该在哪里执行?
- 例如,我是否应该使用委托模式,在 prepareForSegue 中,我将委托引用作为 self 传递?然后向后委托?
- 或者,我是否应该为弹出框创建一个新的 viewController,然后将代码 运行 放在那里?
- 我也看了this tutorial有人说...
" And you get hold of the the content controller by using the
popoverPresentationController.presentedViewController method in the
UIPopoverPresentationController
什么对我来说是理想的,因为我想要的代码会改变一些呈现VC变量,将委托回呈现VC。
只需将 prepareForSegue 中的弹出按钮操作设置为指向 presentingVC 中的函数即可。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let dest = segue.destinationViewController as? MyPopoverViewController {
let myButton = dest.view.viewWithTag(MY_BUTTON_TAG) as! UIButton
myButton.addTarget(self, action: "onMyButton", forControlEvents: .TouchUpInside)
}
}
func onMyButton() {
...
}
或者使用像这样的 UIButton 扩展的闭包更好:
https://www.mikeash.com/pyblog/friday-qa-2015-12-25-swifty-targetaction.html
最终在 dismiss popover 调用的竞争块中与代表团一起去了:
class NavigationViewController: UIViewController {
var presentingVC_Delegate: mainLists_PopoverDelegation!
var whatToDo = "Placeholder"
@IBOutlet var shareBtn: UIButton!
@IBOutlet var clearBtn: UIButton!
@IBOutlet var settingsBtn: UIButton!
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Menu Button Functions
//***** ----- ***** ------ ***** ----- ***** ----- *****
@IBAction func shareBtn_Pressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: { finished in
self.presentingVC_Delegate.call_ActivityVC()
})
}
@IBAction func clearBtn_Pressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: { finished in
self.presentingVC_Delegate.deleteList()
})
}
@IBAction func settingsBtn_Pressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: { finished in
self.presentingVC_Delegate.presentSettingsVC()
})
}
}
protocol mainLists_PopoverDelegation {
func call_ActivityVC ()
func deleteList ()
func presentSettingsVC ()
}
这三个函数位于主 VC。
我将 ViewController 拖到我的情节提要板上,使用 popover segue 对其进行 segue,并在 presentingVC 的 prepareForSegue 中设置大小和样式。我的问题是,如果我的弹窗有多个按钮,它们的代码应该在哪里执行?
- 例如,我是否应该使用委托模式,在 prepareForSegue 中,我将委托引用作为 self 传递?然后向后委托?
- 或者,我是否应该为弹出框创建一个新的 viewController,然后将代码 运行 放在那里?
- 我也看了this tutorial有人说...
" And you get hold of the the content controller by using the popoverPresentationController.presentedViewController method in the UIPopoverPresentationController
什么对我来说是理想的,因为我想要的代码会改变一些呈现VC变量,将委托回呈现VC。
只需将 prepareForSegue 中的弹出按钮操作设置为指向 presentingVC 中的函数即可。
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let dest = segue.destinationViewController as? MyPopoverViewController {
let myButton = dest.view.viewWithTag(MY_BUTTON_TAG) as! UIButton
myButton.addTarget(self, action: "onMyButton", forControlEvents: .TouchUpInside)
}
}
func onMyButton() {
...
}
或者使用像这样的 UIButton 扩展的闭包更好:
https://www.mikeash.com/pyblog/friday-qa-2015-12-25-swifty-targetaction.html
最终在 dismiss popover 调用的竞争块中与代表团一起去了:
class NavigationViewController: UIViewController {
var presentingVC_Delegate: mainLists_PopoverDelegation!
var whatToDo = "Placeholder"
@IBOutlet var shareBtn: UIButton!
@IBOutlet var clearBtn: UIButton!
@IBOutlet var settingsBtn: UIButton!
//***** ----- ***** ------ ***** ----- ***** ----- *****
//Menu Button Functions
//***** ----- ***** ------ ***** ----- ***** ----- *****
@IBAction func shareBtn_Pressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: { finished in
self.presentingVC_Delegate.call_ActivityVC()
})
}
@IBAction func clearBtn_Pressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: { finished in
self.presentingVC_Delegate.deleteList()
})
}
@IBAction func settingsBtn_Pressed(sender: AnyObject) {
self.dismissViewControllerAnimated(true, completion: { finished in
self.presentingVC_Delegate.presentSettingsVC()
})
}
}
protocol mainLists_PopoverDelegation {
func call_ActivityVC ()
func deleteList ()
func presentSettingsVC ()
}
这三个函数位于主 VC。