Reputation: 2457
So my code looks like this, targeting each known textfield and applying the same modifier to them.
Is there some way I can just select all textfields in the view?
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
}
}
Upvotes: 1
Views: 262
Reputation: 17372
You can use @IBOutlet
collection to apply properties to multiple like properties.
You can connect UI elements twice.
Once to their unique @IBOutlet
connection and once to the @IBOutlet
collection
This expresses as [UITextField!]
in Swift and as IBOutletCollection(UITextField)
in Objective-C
So...
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
removes the need for subclassing and maintaining manual arrays
You can also get as generic as you need to and define the collection as a bundle of UIView
if you need to.
@property (strong, nonatomic) IBOutletCollection(UIView) NSArray *allViews;
Upvotes: 0
Reputation: 1198
This would select all the UITextField subviews of a view:
let textfields = view.subviews.filter({$0.isKindOfClass(UITextField)})
Upvotes: 0
Reputation: 323
I am not aware of a way to instantly select all text-fields but if you are looking for a way to save space you can try adding all your text fields to an array an then looping through to make the changes.
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
}
}
Upvotes: 0
Reputation: 12910
You may need to subclass a UITextField
and then inherit from this subclass all of your UITextField elements.
Then you will need to apply properties / styles for this subclass only.
For example (written in Swift 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
}
Upvotes: 1