我想在模态被解除后在视图控制器上调用一个函数。我花了好几个小时试图让它工作,我发现的所有响应都没有奏效。我遵循了其他人的指示并设置了协议,但这仍然无效。
MainController:
class ViewController: UIViewController, UIPopoverPresentationControllerDelegate, loadStoreDelegate{
然后触发我使用的模态
func displaySelectStorePopup(){
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView"){
let selectStoreController = viewController
selectStoreController.modalPresentationStyle = .Popover
if let sctrl = selectStoreController.popoverPresentationController{
sctrl.delegate = self
sctrl.sourceView = self.view
sctrl.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
sctrl.permittedArrowDirections = UIPopoverArrowDirection()
delay(0.1){
sctrl.passthroughViews = nil
}
selectStoreController.modalInPopover = true
selectStoreController.preferredContentSize = CGSizeMake(400, 400)
self.presentViewController(selectStoreController, animated: true, completion: nil)
}
}
}
然后函数id喜欢使用
func loadStore() {
print(2)
//let vc : AnyObject! = self.storyboard!.instantiateViewControllerWithIdentifier("DashboardView")
//self.showViewController(vc as! UIViewController, sender: vc)
}
ModalViewController: 协议
protocol loadStoreDelegate{
func loadStore()
}
class SelectStoreViewController: UIViewController, UITableViewDataSource, UITableViewDelegate{...
var delegate: loadStoreDelegate?
然后在tableview上调用该函数单击
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath){
self.delegate?.loadStore()
if(tableView == selectStoreTable){
currentStore = userStores[indexPath.row]
self.dismissViewControllerAnimated(false, completion: nil)
}
}
答案 0 :(得分:1)
您的SelectStoreViewController类具有delegate
实例属性。但是你永远不会将这个属性设置为任何东西。当你nil
时,它是self.delegate?.loadStore()
。所以自然没有任何事情发生。
我想你想要这样的东西:
func displaySelectStorePopup(){
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView") as? SelectStoreViewController {
let selectStoreController = viewController
selectStoreController.delegate = self // *
// ... and so on ...
答案 1 :(得分:0)
经过大量阅读后,似乎使用popover是阻止我的代表所需的结果。我最后通过为工作站创建一个类来解决这个问题,该工作站包含对当前视图的引用,然后是一个运行我需要的操作的函数。
类别:
class Workstation{
var currentView: UIViewController? = nil
var currentStore : Store? = nil
func loadInStore(store: Store){
currentStore = store
if let view = currentView{
let vc : AnyObject! = view.storyboard!.instantiateViewControllerWithIdentifier("DashboardView")
view.showViewController(vc as! UIViewController, sender: vc)
}
}
class var workstationInstance: Workstation{
struct Static {
static let instance = Workstation()
}
return Static.instance
}
}
SecondController:
override func viewDidDisappear(animated: Bool) {
if let store = selectedStore{
Workstation.workstationInstance.loadInStore(store)
}
}
在我的主控制器中,我只是在弹出窗口加载并设置当前视图
override func viewDidAppear(animated: Bool) {
Workstation.workstationInstance.currentView = self
}
func displaySelectStorePopup(){
if let viewController = self.storyboard?.instantiateViewControllerWithIdentifier("SelectStoreView"){
let selectStoreController = viewController
selectStoreController.modalPresentationStyle = .Popover
if let sctrl = selectStoreController.popoverPresentationController{
sctrl.sourceView = self.view
sctrl.sourceRect = CGRectMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds),0,0)
sctrl.permittedArrowDirections = UIPopoverArrowDirection()
delay(0.1){
sctrl.passthroughViews = nil
}
selectStoreController.modalInPopover = true
selectStoreController.preferredContentSize = CGSizeMake(400, 400)
self.presentViewController(selectStoreController, animated: true, completion: nil)
}
}
}