有没有办法通过函数执行委托?

时间:2016-09-01 01:07:31

标签: swift

为了避免重复,你怎么能转换这样的东西:

 self.upperTextField.delegate = self
 self.lowerTextField.delegate = self

进入:

 func setDelegates(textField: UITextField){
        textField.delegate = self
        print("delegates set")
    }

1 个答案:

答案 0 :(得分:2)

您可以修改函数以接受UITextField个对象数组:

func setDelegates(delegate: UITextFieldDelegate, textFields: [UITextField]) {
    for textField in textFields {
        textField.delegate = delegate
    }
    print("delegates set")
}

然后这样称呼:

setDelegates(self, textFields: [upperTextField, lowerTextField])