是否可以选择视图中的所有文本字段?

时间:2016-09-17 20:29:18

标签: swift

所以我的代码看起来像这样,针对每个已知的文本字段并对它们应用相同的修饰符。

有什么方法可以选择视图中的所有文本字段吗?

@Consumes(MediaType.APPLICATION_JSON)

4 个答案:

答案 0 :(得分:1)

您可能需要子类一个UITextField,然后从该子类继承所有UITextField元素。

然后,您将需要仅为此子类应用属性/样式。

例如(用 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 
}

答案 1 :(得分: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


}

}

答案 2 :(得分:0)

这将选择视图的所有UITextField子视图:

css

答案 3 :(得分:0)

您可以使用@IBOutlet集合将属性应用于多个类似的属性。

您可以连接两次UI元素。

一次到@IBOutlet连接,一次到@IBOutlet集合

enter image description here

在Swift中表示为[UITextField!],在Objective-C中表示为IBOutletCollection(UITextField)

因此...

<强>夫特

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

    }
}

<强>目标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;