2017-09-08 4 views
0

Ich möchte eine variadic Methode machen, um eine beliebige Anzahl von Textfeldern und einem UIColor als Eingaben zu akzeptieren, und meine Absicht ist, eine untere Zeile dieser bestimmten Farbe zu allen so textfields innerhalb Methode hinzuzufügen.Erstellen Sie eine variadic Methode in Swift

Wie kann ich eine solche Methode in Swift machen?

Antwort

2

Wahrscheinlich wie etwas, das tun würde:

func applyColour(toTextfields textfields: UITextField..., colour: UIColor) { 
    for textfield in textfields { 
     // Apply the colour here. 
    } 
} 
+0

dem Schnellen Dokumentation: Die Werte an einen variadische Parameter übergeben werden, in der Funktion des Körpers als eine Anordnung des geeigneten Typs zur Verfügung gestellt. Zum Beispiel wird ein variadischer Parameter mit einem Namen von Zahlen und einem Typ von Double ... innerhalb des Körpers der Funktion als ein konstantes Array namens Nummern vom Typ [Double] zur Verfügung gestellt. https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Functions.html –

2

enter image description here

Common.addBottonLine(color: UIColor.white, userNameTextField, passwordTextField) 

Methode Definition

class func addBottonLine(color: UIColor, _ txtFields: UITextField...) -> Void { 

    for textF in txtFields { 

     var bottomLine = CALayer() 

     bottomLine.frame = CGRect.init(origin: CGPoint.init(x: 0, y: textF.frame.height - 2) , size: CGSize.init(width: textF.frame.width, height: 2)) 

     bottomLine.backgroundColor = color.cgColor 
     textF.borderStyle = .none 
     textF.layer.addSublayer(bottomLine) 

    } 

} 
0

Mit dieser Funktion unteren Zeile hinzufügen

func addBottomLabel(_ color: UIColor) { 
let lbl1 = UILabel() 
lbl1.backgroundColor = color 
addSubview(lbl1) 
addVisualConstraints(["H:|[label]|", "V:[label(1)]|"], forSubviews: ["label": lbl1]) 
} 

Verwendung:

let textFields = [your textfield array] 
for textFields in textFields 
{ 
textFields.addBottomLabel(.yourcolor) 
} 
Verwandte Themen