我有一个要求,我必须在按钮水龙头上从第二个视图控制器调用第一个视图控制器功能。
for root, dirs, files in os.walk('python/Lib/email'):
print('Searching:', root)
print('All directories', dirs)
print('All files:', files)
print('----------------------')
我的第一个问题是我们可以直接从第二个VC启动First VC IBAction吗?有可能吗?
我想做以下
class FirstViewController: UIViewController {
@IBAction func firstButtonPressed(_ sender: Any) {
// Doing ABC
}
@IBAction func showSecondVC_ sender: Any) {
// showingSecondVC
}
}
class secondViewController: UIViewController {
@IBAction func SecondButtonPressed(_ sender: Any)
// Dismiss second vc & call First View controller method so that it does ABC.
}
如何从第二个vc ??
调用第一个vc方法答案 0 :(得分:3)
你有几个选择:
选项1 - 拆分逻辑:
http://schemas.microsoft.com/mapi/proptag/
选项2 - 创建回调:
class FirstViewController: UIViewController {
let abcPerformer = ABCPerformer()
@IBAction func firstButtonPressed(_ sender: Any) {
abcPerformer.doABC()
}
@IBAction func showSecondVC_ sender: Any) {
// showingSecondVC
}
}
class SecondViewController: UIViewController {
let abcPerformer = ABCPerformer()
@IBAction func SecondButtonPressed(_ sender: Any) {
// Dismiss second vc & call First View controller method so that it does ABC.
abcPerformer.doABC()
}
}
struct ABCPerformer {
func doABC() {
// do ABC
}
}
选项3 - 使用代理:
class FirstViewController: UIViewController {
@IBAction func firstButtonPressed(_ sender: Any) {
doABC()
}
@IBAction func showSecondVC_ sender: Any) {
// showingSecondVC
secondVC.doABC = doABC
}
func doABC() {
// do ABC
}
}
class SecondViewController: UIViewController {
var doABC: (() -> Void)?
@IBAction func SecondButtonPressed(_ sender: Any) {
// Dismiss second vc & call First View controller method so that it does ABC.
doABC?()
}
}
可能还有更多选项,但这些选项应该给你足够的选择来做出决定
答案 1 :(得分:0)
答案 2 :(得分:0)
要从First显示第二个视图控制器,您可以使用推送或当前转换。
@IBAction func firstButtonPressed(_ sender: Any) {
// call DoABC
//presenting VC
let secondVC = SecondViewController() //change this to your class name
self.presentViewController(secondVC, animated: true, completion: nil)
//for push :
navigationController?.pushViewController(SecondViewController, animated: true)
}
您可以在第二个视图中相应地使用pop / dismiss VC来返回第一个视图。
class secondViewController: UIViewController {
@IBAction func SecondButtonPressed(_ sender: Any)
// Dismiss second vc // Call Firstvc.DoABC ?? How to do this ??
//if you used Present in first step then use dismiss
self.dismissViewControllerAnimated(false, completion: nil)
//if you used push in first step then use pop
self.navigationController?.popViewController(animated: true) }
答案 3 :(得分:0)
您可以使用自定义委托,如下所示,并添加功能" presentPage"无论你想打电话的地方。
protocol MainDelegate {
func presentPage(page : Int)
}