2016-09-07 70 views
1

我在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。

回答

1

如何創建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 
     } 
    } 
} 
-1

剛剛嘗試這一點在你的SecondViewController.swift:

let firstViewController = FirstViewController() 

而你需要的任何時間的函數只要致電:

firstViewController.addDoneButton() 
+0

Tthats創建FirstViewController的新實例,而不是正確的方法。 – crashoverride777

1

你可以做一個協議,然後把這個方法在自我:UIViewController的協議擴展中。然後,您要啓用此函數的任何UIViewController子類,只需將CanAddDoneButton添加到它所符合的協議即可。注意它已具有該collectionTextFields變量。儘管我認爲你甚至可以將該變量放入協議擴展中,除非它是IBOutlet。

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

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

我覺得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) 
    } 
    } 

希望這有助於

相關問題