Loop Through Subview to Check for Empty Uitextfield - Swift

Loop through subview to check for empty UITextField - Swift

Swift 5 and Swift 4: -
A Very simple answer you can understand easyly : - You can handle all kind of Objects like UILable, UITextfields, UIButtons, UIView, UIImages . any kind of objecs etc.

for subview in self.view.subviews
{
if subview is UITextField
{
//MARK: - if the sub view is UITextField you can handle here
if subview.text == ""
{
//MARK:- Handle your code
}
}
if subview is UIImageView
{
//MARK: - check image
if subview.image == nil
{
//Show or use your code here
}

}
}

//MARK:- You can use it any where, where you need it
//Suppose i need it in didload function we can use it and work it what do you need

override func viewDidLoad() {
super.viewDidLoad()
for subview in self.view.subviews
{
if subview is UITextField
{
//MARK: - if the sub view is UITextField you can handle here
if subview.text == ""
{
//MARK:- Handle your code
}
}
if subview is UIImageView
{
//MARK: - check image
if subview.image == nil
{
//Show or use your code here
}
}
}
}

checking whether all text field in my view is empty or not objective c

You can loop through all subviews of your view, and pick all of the UITextField instances.

//replace self.view to whatever your own view

- (void)checkAllTextField {
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)view;
NSLog(@"This is a UITextField, length: %lu", textField.text.length);
} else {
NSLog(@"not a UITextField");
}
}
}

How to loop Through each UITextfield of a Section?

My suggestion is not doing that. You cannot rely on the content of a UITextField that is embedded in a cell of a tableView, due to reusability.

Your view controller should be directly tied to the model, thus, changing the text within a textField should update your model immediately, so, when you scroll and a cell gets reused, the proper value is assigned. And when you hit "forward" you just query the content of the model (which, by all means, could probably be a simple NSMutableArray).

Let's add an NSMutableArray with as many empty strings as there will be textFields (for example 5):

- (instancetype)initWithWhatever...
{
self = [super initWithWhatever];

if (self)
{
_textFieldValues = [NSMutableArray arrayWithCapacity:5];

for (NSInteger i=0; i < 5; i++)
{
_textFieldValues[i] = @"";
}
}

return self;
}

Make sure every textfield points back to your viewController, on cellForRowAtIndexPath:.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
...

myCell.textField.text = _textFieldValues[indexPath.row];
myCell.textField.tag = 100 + indexPath.row;
myCell.textField.delegate = self;

return myCell
}

The tag is there to identify each cell, other option is to make your own UITextField subclass or use a custom setValueForKey:.

Whatever you pick, when you get the delegate method (UITextFieldDelegate), check against your preferred method, tag in my case:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *replaced = [textField.text stringByReplacingCharactersInRange:range withString:string];

_textFieldValues[(textField.tag - 100)] = replaced;

return YES;
}

That will keep the model up to date always, and you don't need to iterate text fields, just over the array:

// Method that gets called when hitting forward
- (IBOutlet)didPressForwardButton:(UIButton*)sender
{
for (NSString *text in _textFieldValues)
{
NSLog(@"UITEXTFIELD TEXT = %@", text);
}
}

Lesson: When working with a UITableView you should be always mapping what's on the view to what's on the model, and update immediately, you cannot rely on the view to hold the data. In fact, this should extend to anything you do in iOS, views are for shows. If you cannot modify the real model immediately, you would have to encapsulate it in a "transaction" intermediary object and then update the real model.

How to check if UITextFields are empty?

I am supposing UITextField variable as *tfield so here is the solution

if (tfield.text.length > 0 || tfield.text != nil || ![tfield.text isEqual:@""])
{
//do your work
}
else
{
//through error
}

How to loop through view outlets in a UIViewController with Swift?

This is what Outlet Collections are for. Drag all your textfields in the same Outlet Collection in InterfaceBuilder and create an @IBOutlet to that collection in your class file:

To create the outlet collection in InterfaceBuilder, ctrl-drag from the first UITextField to your class file in the assistant editor. Then choose Outlet Collection:

Sample Image

ctrl-drag the next UITextField on that @IBOutlet to add it to the collection:

Sample Image

Repeat that for all your textFields.

@IBOutlet var textFields: [UITextField]!

func checkTextFields() {
for textField in self.textFields {
... // do your checks
}
}

How can I loop through all subviews of a UIView, and their subviews and their subviews

Use recursion:

// UIView+HierarchyLogging.h
@interface UIView (ViewHierarchyLogging)
- (void)logViewHierarchy;
@end

// UIView+HierarchyLogging.m
@implementation UIView (ViewHierarchyLogging)
- (void)logViewHierarchy
{
NSLog(@"%@", self);
for (UIView *subview in self.subviews)
{
[subview logViewHierarchy];
}
}
@end

// In your implementation
[myView logViewHierarchy];

UITextField added to SubView when refreshing

Yes this will keep on adding a text field each time.
You can check that using textview.subviews.count and see for yourself that the count is increasing

Rather than adding a text field each time you should add one text field at the beginning and just set the string value to the date each time the user clicks the button

For-in loop and type casting only for objects which match type

You can use a for-loop with a case-pattern:

for case let item as YourType in array {
// `item` has the type `YourType` here
// ...
}

This will execute the loop body only for those items in the
array which are of the type (or can be cast to) YourType.

Example (from
Loop through subview to check for empty UITextField - Swift):

for case let textField as UITextField in self.view.subviews {
if textField.text == "" {
// ...
}
}

Make all the uitextfields empty in viewWillAppear / viewWillDisappear

Try this:

for (UIView *subview in [yourView subviews])
if([subview isKindOfClass:[UITextField class]])
[(UITextField*)subview setText:@""];


Related Topics



Leave a reply



Submit