2016-09-17 87 views
1

所以我的代碼看起來像這樣,針對每個已知的文本字段並對它們應用相同的修改器。是否可以選擇視圖中的所有文本字段?

有沒有什麼辦法可以選擇視圖中的所有文本框?

import UIKit 

class CreateAccount: UIViewController, UITextFieldDelegate, UIScrollViewDelegate { 

    @IBOutlet weak var scrollViewer: UIScrollView! 
    @IBOutlet weak var usernameTextField: UITextField! 
    @IBOutlet weak var emailTextField: UITextField! 
    @IBOutlet weak var passwordTextField: UITextField! 
    @IBOutlet weak var reenterPasswordTextField: UITextField! 

    let textFieldBorderColor: UIColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0) 
    let textFieldBorderFocusedColor: UIColor = UIColor(red: 31.0/255.0, green: 111.0/255.0, blue: 217.0/255.0, alpha: 1.0) 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view. 


     usernameTextField.layer.borderColor = textFieldBorderColor.CGColor 
     emailTextField.layer.borderColor = textFieldBorderColor.CGColor 
     passwordTextField.layer.borderColor = textFieldBorderColor.CGColor 
     reenterPasswordTextField.layer.borderColor = textFieldBorderColor.CGColor 

     usernameTextField.layer.borderWidth = 1.0 
     emailTextField.layer.borderWidth = 1.0 
     passwordTextField.layer.borderWidth = 1.0 
     reenterPasswordTextField.layer.borderWidth = 1.0 

     scrollViewer.delegate = self 


    } 
} 

回答

1

您可能需要一個UITextField,然後從這個子類所有的UITextField元素的繼承。

然後,您只需要爲該子類應用屬性/樣式。

例如(寫在雨燕3.0):

class MyCustomTextField: UITextField { 
    required init(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
     self.layer.borderWidth = 3.0 
     self.layer.borderColor = UIColor.red.cgColor 
    } 
} 

@IBOutlet weak var usernameTextField: MyCustomTextField! 
... 

func viewDidLoad() { 
    // you don't need to apply style properties here anymore 
} 
0

我不知道的方式立即選擇所有的文本字段,但如果你正在尋找一種方式,以節省空間,您可以嘗試將所有文​​本字段添加到數組,然後循環執行更改。

import UIKit 

class CreateAccount: UIViewController, UITextFieldDelegate, UIScrollViewDelegate { 



@IBOutlet weak var scrollViewer: UIScrollView! 
@IBOutlet weak var usernameTextField: UITextField! 
@IBOutlet weak var emailTextField: UITextField! 
@IBOutlet weak var passwordTextField: UITextField! 
@IBOutlet weak var reenterPasswordTextField: UITextField! 



let textFieldBorderColor: UIColor = UIColor(red: 170.0/255.0, green: 170.0/255.0, blue: 170.0/255.0, alpha: 1.0) 
let textFieldBorderFocusedColor: UIColor = UIColor(red: 31.0/255.0, green: 111.0/255.0, blue: 217.0/255.0, alpha: 1.0) 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view. 

    var textFieldArray = [usernameTextField, emailTextField, passwordTextField, reenterPasswordTextField] 

    for textField in textFieldArray { 

     textField.layer.borderColor = textFieldBorderColor.CGColor 
     textField.layer.borderWidth = 1.0 
    } 



    scrollViewer.delegate = self 


} 

}

0

這將選擇一個視圖的所有子視圖的UITextField:

let textfields = view.subviews.filter({$0.isKindOfClass(UITextField)}) 
0

您可以使用@IBOutlet集合屬性應用到多個類似的特性。

您可以連接兩次UI元素。

一旦其獨特的@IBOutlet連接,一旦到@IBOutlet收集

enter image description here

這表示爲[UITextField!]在斯威夫特和在IBOutletCollection(UITextField) Objective-C的

所以......

Swift

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var text1: UITextField! 
    @IBOutlet weak var text2: UITextField! 
    @IBOutlet weak var text3: UITextField! 


    @IBOutlet var allTextFields: [UITextField]! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     for field in allTextFields { 
      field.backgroundColor = UIColor.blueColor() 
     } 

    } 
} 

Objective-C的

@interface ViewController() 

@property (weak, nonatomic) IBOutlet UITextField *text1; 
@property (weak, nonatomic) IBOutlet UITextField *text2; 
@property (weak, nonatomic) IBOutlet UITextField *text3; 

@property (strong, nonatomic) IBOutletCollection(UITextField) NSArray *allTextFields; 


@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    for (UITextField *field in self.allTextFields) { 
     field.backgroundColor = [UIColor greenColor]; 
    } 
} 


@end 

消除了子類和維護需要手工陣列

,因爲你需要您也可以作爲通用的定義集合彷彿的UIView束你需要。

@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *allViews;

相關問題