Swift:从另一个视图控制器调用ViewController func

问题描述:

我在FirstViewController.swift中有一个函数“addDoneButton”,我不想复制并粘贴到SecondViewController.swift中,所以我想在SecondViewController中调用它。Swift:从另一个视图控制器调用ViewController func

func addDoneButton() { 
    let keyboardToolbar = UIToolbar() 
    keyboardToolbar.sizeToFit() 
    let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, 
             target: nil, action: nil) 
    let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done, 
             target: view, action: #selector(UIView.endEditing(_:))) 
    keyboardToolbar.items = [flexBarButton, doneBarButton] 
    for textField in self.collectionOfTextField! as [UITextField] { 
     textField.keyboardType = .DecimalPad 
     textField.inputAccessoryView = keyboardToolbar 

    } 
} 

如何实现它?预先感谢一个新swifter。

如何创建UIViewController的扩展名并将collectionTextFields作为参数?

extension UIViewController { 
    func addDoneButton(collectionTextFields: [UITextField]) { 
     let keyboardToolbar = UIToolbar() 
     keyboardToolbar.sizeToFit() 
     let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, 
              target: nil, action: nil) 
     let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done, 
              target: view, action: #selector(UIView.endEditing(_:))) 

     keyboardToolbar.items = [flexBarButton, doneBarButton] 
     for textField in collectionTextFields { 
      textField.keyboardType = .DecimalPad 
      textField.inputAccessoryView = keyboardToolbar 
     } 
    } 
} 

刚刚尝试这一点在你的SecondViewController.swift:

let firstViewController = FirstViewController() 

而你需要的任何时间的函数只要致电:

firstViewController.addDoneButton() 
+0

Tthats创建FirstViewController的新实例,而不是正确的方法。 – crashoverride777

你可以做一个协议,然后把这个方法在自我:UIViewController的协议扩展中。然后,您要启用此函数的任何UIViewController子类,只需将CanAddDoneButton添加到它所符合的协议即可。注意它已具有该collectionTextFields变量。尽管我认为你甚至可以将该变量放入协议扩展中,除非它是IBOutlet。

protocol CanAddDoneButton { 
    var collectionTextFields: [UITextField] 
    func addDoneButton() 
} 

extension CanAddDoneButton where Self: UIViewController { 
    func addDoneButton() { .... } 
} 

我觉得EridB的答案是一个好方法。

或者,Swift 2协议扩展是什么?它与他的答案类似,唯一的区别是,除非符合协议,否则并非所有ViewController都可以访问该方法。

protocol Button { } 
extension Button where Self: UIViewController { 

    func addDoneButton(forCollectionTextFields collectionTextFields: [UITextField]) { 
     let keyboardToolbar = UIToolbar() 
keyboardToolbar.sizeToFit() 
     let flexBarButton = UIBarButtonItem(barButtonSystemItem: .FlexibleSpace, 
            target: nil, action: nil) 
     let doneBarButton = UIBarButtonItem(barButtonSystemItem: .Done, 
            target: view, action: #selector(UIView.endEditing(_:))) 
     keyboardToolbar.items = [flexBarButton, doneBarButton] 
for textField in collectionTextFields { 
      textField.keyboardType = .DecimalPad 
      textField.inputAccessoryView = keyboardToolbar 

     } 
    } 

} 

比你的viewControllers需要调用这个方法你符合协议,只是调用它。

class SomeViewController: UIViewController, Button { 

    override func viewDidLoad() { 
    super.viewDidLoad() 

     addDoneButton(forCollectionTextFields: self.collectionOfTextField) 
    } 
    } 

希望这有助于