Find the empty textfields through the loop - iphone

I am using the below code to check the textfields whether they are empty or not through loop.
Below is my code, it is giving the following error.
-[UIImageView text]: unrecognized selector sent to instance 0x997c930
2014-03-19 16:54:49.227 IS[2837:a0b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImageView text]: unrecognized selector sent to instance 0x997c930'
//code
NSString *strText;
NSMutableArray *arrMsgs = [[NSMutableArray alloc] init];
[arrMsgs addObject:#"*Manifest Type is reuired."];
[arrMsgs addObject:#"*Suffix Code is required."];
[arrMsgs addObject:#"*Aircraft Registry is required."];
[arrMsgs addObject:#"*Flight Number is required."];
[arrMsgs addObject:#"*Embarkation ICAO/IATA required."];
[arrMsgs addObject:#"*ETD date (Local) is required."];
[arrMsgs addObject:#"*ETD Time (Local) is required."];
[arrMsgs addObject:#"*ETA date (Local) is required."];
[arrMsgs addObject:#"*ETA Time (Local) is required."];
NSLog(#"array:%d", [arrMsgs count]);
for(int i=1; i<=9;i++)
{
UITextField *tf= (UITextField *)[self.view viewWithTag:i];
int k=0;
UILabel *lbl = (UILabel *)[self.view viewWithTag:k];
if([tf.text length] == 0)
{
strText = [arrMsgs objectAtIndex:i-1];
lbl.text = strText;
NSLog(#"strText1:%#", strText);
k++;
}
}
I am not getting where i am wrong, please guide for above. It crashes at the if condition with above mentioned error.

Here your k is integer variable whose value is 0:
But when u fetch [self.view viewWithTag:k] it gives UIImageView not UILabel. Text property is of UILabel and not of UIImageView
Check if its UILabel or not using iskindOfClass like this :
id viewTag = [self.view viewWithTag:k];
if([viewTag iskindOfClass:[UIImageView class]])
NSLog(#"image view");
else if([viewTag iskindOfClass:[UILabel class]])
NSLog(#"label");
else if([viewTag iskindOfClass:[UITextField class]])
NSLog(#"txtField");
else
NSLog(#"%#",viewTag);
Solution is add unique tag to UILabel like 999 or 9999 or 99999.

Related

How can I read UITextField value in an IBAction. I'm creating UITextField programmatically

How can I read UITextField value in an IBAction? I'm creating UITextField programmatically. So I can't set #property and #synthesize using Xcode. The code to generate UITextField is as follows:
for(i=0; i<[fieldName count]; i++)
{
UITextField *name = [fieldName objectAtIndex:i];
frame = CGRectMake(fromLeft, fromTop, totalWidth, totalHeight);
name = [[[UITextField alloc] initWithFrame:frame] autorelease];
name.borderStyle = UITextBorderStyleRoundedRect;
name.returnKeyType = UIReturnKeyDone;
//name.placeholder = [fieldName objectAtIndex:i];
name.autocapitalizationType = UITextAutocapitalizationTypeWords;
name.adjustsFontSizeToFitWidth = TRUE;
name.keyboardType = UIKeyboardTypeDefault;
[name addTarget:self action:#selector(doneEditing:) forControlEvents:UIControlEventEditingDidEndOnExit];
[scroller addSubview:name];
fromTop = fromTop + 40;
}
Now I want to read values of each textbox in a button click (IBAction). Can anyone help me please?
You could use something like this to loop through all UITextFields that are subviews of self.view and add their text to a NSMutableArray:
for (UITextField *field in self.view.subviews) {
if ([field isKindOfClass:[UITextField class]]) {
if ([[field text] length] > 0) {
[someMutableArray addObject:field.text];
}
}
}
if your doneEditing: looks like this doneEditing:(id)sender then you can say:
UITextField *field = (UITextField *)sender;
NSString *myText = field.text;
EDIT:
To access a UITextField without setting as an instance variable you need to tag it when you create it:
[textField setTag:1];
then whenever you want to access it you can get it from its parent view by the tag:
UITextField *myTextField = [scroller viewWithTag:1];
NSString *myString = myTextField.text;
in your case, set the tag to i+1 for example to have all the textfield with unique tags.
implement the IBAction function like the following:
-(IBAction) doneEditing:(UITextField*)sender
{
NSString * val = sender.text;
}
try using the UITextFieldDelegate , i think its better for your case.
add each UITextField a Tag and by that you will recognise the UITextField.
NSString *value = sender.text;
If inside an IBAction, of course.
Which ignores that there is a set of text fields, and a single button action.
The simplest solution to the overall problem would be to store a set (or array) of text fields in an instance variable, and iterate over that set in the button action. But that is a rather coarse approach; it is probably better to use the text field delegate method and store text values directly in an array, using the button to trigger the save.
In addition, Apple HIG would tell you that you should update your data model as the text fields are edited, rather than use a "Save" button - which is poor UX design - unless, of course, the values of individual fields can interact.

Loop through UITextField's and store double values

I'm having trouble looping through a set of dynamically created UITextFields and storing those values as a double to be added to an array later on. I'm still pretty new to obj-c programming so bear with me if this question seems trivial. Thanks. This is what I have so far.
NSMutableArray *textFieldCashArray = [[NSMutableArray alloc] init];
double textFieldCash;
for (int y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
P.S and here is the error message I'm getting
Pro[962:b303] -[UIControl text]: unrecognized selector sent to instance 0x680f850
2012-04-01 16:05:46.305 iFinance Pro[962:b303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIControl text]: unrecognized selector sent to instance 0x680f850'
*** Call stack at first throw:
I think what's going on here is that the loop variable is being used as the tag, and the loop starts at zero. viewWithTag will answer either the receiver or one of it's subviews with a given tag, so if the view controller's main view has tag==0 (which it probably does), your first text request is being sent to that top-level view.
Try setting the text field tags to some non-zero value, starting at SOME_OFFSET. Then in your loop:
for (y=0; y<25; y++) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y+SOME_OFFSET];
// ...
}
danh is most certainly right about the cause and solution to this problem. Just to add a little, cases like this can be somewhat avoided by checking the Class before casting.
if ([[self.view viewWithTag:y] isKindOfClass:[UITextField class]]) {
UITextField *myLabel = (UITextField *)[self.view viewWithTag:y];
textFieldCash = [myLabel.text doubleValue];
[textFieldCashArray addObject:[NSNumber numberWithDouble:textFieldCash]];
}
You should create array at first:
NSMutableArray *textFieldCashArray = [NSMutableArray array];
Edit
Your error log shows that you are receiving some another UIControl object (UIBtton for example) instead of UITextField. Check your tags on xib (or algorith if you set programmatically) and be sure that UITextField objects has corresponding tags

Why is NSLog causing EXC_BAD_ACCESS?

I have a tablecell with a button and I want to hook this in to a method call in my main class.
I have it working, but I need to identify the button pressed. SO I have done the following:
in cellForRowAtIndexPath I do the following:
cell.myBtn.tag = indexPath.row;
[cell.myBtn addTarget:self
action:#selector(viewClick:)
forControlEvents:UIControlEventTouchUpInside];
And I created the selector method like so:
- (void)viewClick:(id)sender
{
UIButton *pressedButton = (UIButton *)sender;
// EXC_BAD_ACCESS when running NSLog
NSLog(#"button row %#",pressedButton.tag);
if(pressedButton.tag == 1)
{
// NSString filename = #"VTS_02_1";
}
}
The problem is I get EXC_BAD_ACCESS when it hits this line: NSLog(#"button row %#",pressedButton.tag);
specify %i for int value
you have to use %# for only object, but int is not object,NSNumber is an object for that you can use %#.
NSLog(#"button row %i",pressedButton.tag);
try NSLog(#"button row %d", pressedButton.tag);
tag property is an int not an object.

[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'

i am creating custom cell.
In that i added 3 textfields. so i want to store that textfield values into nsmutablearray.
when i am trying this code
UITextField *valueField = [[UITextField alloc] initWithFrame:CGRectMake(165,6, 135, 30)];
valueField.borderStyle = UITextBorderStyleRoundedRect;
[cell addSubview:valueField];
[array1 addObject:valueField.text];
[valueField release];
i am getting error like this
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSMutableArray insertObject:atIndex:]: attempt to insert nil object at 0'
so please tell me the reason
thanks in advance.
UITextField *valueField = [[UITextField alloc] initWithFrame:CGRectMake(165,6, 135, 30)];
valueField.borderStyle = UITextBorderStyleRoundedRect;
valueField.text = #"";
[cell addSubview:valueField];
[array1 addObject:valueField.text];
[valueField release];
the above code will work fine for you.
The text attribute of UITextField is nil by default. Set it to an empty string before adding it to your array, though I think this is not what you are trying to achieve.
Just add this condition to check if the textfield is empty (i.e. textfield.text = nil):
if (valueField.text != nil) {
[array1 addObject:valueField.text];
}
else {
[array1 addObject:#""];
}
This will check if textfield is empty it will add an empty string. If you dont want that just skip the else part.
I noticed you said you declared your array like so:
" declared as like NSMutableArray *array1;"
Have you alloc your array before adding objects to it?
In other words have you done this ?
NSMutableArray *array1 = [[NSMutableArray alloc] init];
If you only did
NSMutableArray *array1;
That will only declare a pointer to a NSMutableArray object. It doesn't point to an instance of a NSMutableArray object.
When you put [array1 addObject:valueField.text];, you are adding a nil object to your array, just as your error says. How can valueField.text be equal to something when you just initialized and allocated valueField?

Accessing a UITextField from an array in Objective-C

I have 4 UITextFields that I'm dynamically creating, in the viewDidLoad, which works good. I want to reference those objects when the UISlider value changes. Right now I'm storing those objects in a NSMutableArray and accessing them like so from the sliderChanged method:
NSInteger labelIndex = [newText intValue];
labelIndex--;
NSUInteger firstValue = (int)0;
NSMutableArray *holeArray = [pointsArray objectAtIndex:labelIndex];
UITextField *textField = [textFieldArray objectAtIndex:firstValue];
NSString *newLabel1Text = [[NSString alloc] initWithString:[[holeArray objectAtIndex:firstValue] stringValue]];
[textField setText: newLabel1Text];
[newLabel1Text release];
Everything is working good, but the program crashes on the setText: method. The last message I get from the program is: [UILabel drawTextInRect:] and then I get a EXC_BAD_ACCESS failure.
I want to be able to acces that dynamically created UITextField, but I must be going about it the wrong way.
Thanks!
Uh, yea, you create a text field, but you aren't displaying the field itself, just creating it.
If you want to do what I think you want to do, I would just do if statements.
ex.
if (firstValue == 1)
{
fieldone.text = #"whatever";
}
else if (firstValue == 2)
{
fieldtwo.text = #"whatever";
}