我有一个UIView
,在UIVIew
内添加了一个UIScrollView
,其中包含另一个UIView
和UITextFields
列表。我正在使用以下代码尝试遍历所有视图并选择UITextfields
。问题是,尽管正在发生循环,但找不到任何UITextfields
。
- (void)styleUITextFields {
for (UIView *view in [self.view subviews]) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
// Do whatever you want with the text field.
}
}
这就是我将
UIScollView
添加到UIView
的方式- (void)addForm {
[self styleSegmentController];
[self styleUITextFields];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
[self.scrollViewForm setContentSize:CGSizeMake(screenWidth, self.scrollViewForm.frame.size.height)];
[self.scrollViewForm setFrame:CGRectMake(0, 56, screenWidth, screenHeight - 56)];
[self.view addSubview:self.scrollViewForm];
}
关于为什么这可能不起作用的任何建议。
最佳答案
- (void)styleUITextFields:(UIView*) view {
for (UIView *subview in [view subviews]) {
if ([subview isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
// Do whatever you want with the text field.
} else { // look insde the subview for more views.
[self styleUITextFields: subview];
}
}
- (void)addForm {
[self styleSegmentController];
[self styleUITextFields: self.view];
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;
[self.scrollViewForm setContentSize:CGSizeMake(screenWidth, self.scrollViewForm.frame.size.height)];
[self.scrollViewForm setFrame:CGRectMake(0, 56, screenWidth, screenHeight - 56)];
[self.view addSubview:self.scrollViewForm];
}
关于ios - 循环遍历和编辑UITextField占位符不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35063713/