代表团出席VC,来自Popover

时间:2016-02-07 13:05:55

标签: ios swift delegates uipopovercontroller

我将一个ViewController拖到我的故事板上,用一个popover segue塞进它,并在呈现VC的prepareForSegue中设置大小和样式。我的问题是,如果我的popover有几个按钮,他们的代码应该在哪里执行?

  1. 例如,我是否应该使用委托模式,在prepareForSegue中,我将委托引用作为self传递?然后委托后退?
  2. 或者,我应该为popover创建一个新的viewController,然后把代码放在那里运行?
  3. 我也读过this tutorial,有人说......
  4.   

    "你可以使用它来掌握内容控制器   popoverPresentationController.presentedViewController方法中的   UIPopoverPresentationController

    对我来说什么是理想的,因为我想要的代码会改变一些presentVC变量,将代表回到呈现的VC。

2 个答案:

答案 0 :(得分:0)

只需在prepareForSegue中设置popover按钮操作以指向presentationVC中的函数。

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

答案 1 :(得分:0)

Ultimately just went with delegation in the competition block of the dismiss popover call:

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 ()
}

With those three functions located in the main VC.