Swift:从另一个viewcontroller调用一个ViewController func

时间:2016-09-07 02:25:23

标签: ios swift

我有一个功能" addDoneButton"在FirstViewController.swift中,我不想复制并粘贴到SecondViewController.swift中,所以我想在SecondViewController中调用它。

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

    }
}

如何实现它?提前感谢新的快捷方式。

4 个答案:

答案 0 :(得分:1)

如何创建all.js的扩展名并将UIViewController作为参数传递?

collectionTextFields

答案 1 :(得分:1)

您可以制作协议,然后将此方法放在协议扩展中,其中self:UIViewController。然后你想要启用这个函数的任何UIViewController子类,只需将CanAddDoneButton添加到它符合的协议中......但需要注意的是它已经有了collectionTextFields变量。虽然我认为您甚至可以将该变量放入协议扩展中,除非它是IBOutlet。

Sub Transfer()

Dim shSource As Worksheet
Dim cellValue As Long    
Dim formatedCellBegin As String
Dim formatedCellEnd As String

Set shSource = ThisWorkbook.Sheets("Sheet1") ' Get info from user input sheet
cellValue = shSource.Cells(4, "H") ' User input taken from "H4" on sheet1 received in regards  to what row to start transfer

formatedCellBegin = "J" & cellValue ' Add J to the that row to get the cell to start at
formatedCellEnd = "K" & cellValue ' End at cell K - (whatever they pick)

'Sheet 12 is the sheet with all the invoice info
'Sheet 11 is the sheet to put all the info    
Sheets("Sheet12").Range(formatedCellBegin & "," & formatedCellEnd).Copy Sheets("Sheet11").Range("B20")

End Sub

答案 2 :(得分:0)

我认为EridB回答是一个好方法。

或者Swift 2协议扩展怎么样?它与他的答案类似,唯一的区别是并非所有ViewControllers都能访问该方法,除非你遵守协议。

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

希望这有帮助

答案 3 :(得分:-1)

只需在SecondViewController.swift中尝试:

let firstViewController = FirstViewController()

任何时候你需要这个函数只需要调用:

firstViewController.addDoneButton()
相关问题