Loop through UITextField's and store double values - iphone

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

Related

Inserting 8 or more objects into array crashes app

Simple question,
I loop thru my table view cells and add the objects to an array:
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [self.ammoTable numberOfSections]; ++j) // loop thru sections
{
for (NSInteger i = 0; i < [self.ammoTable numberOfRowsInSection:j]; ++i)//in each section loop thru the cells
{
[cells addObject:[self.ammoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
This works PERFECT, as long as the table view has only 7 or less cells, if i add 8 or more, the app crashes with this log to the console:
'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
Any help is greatly appreciated!
The cellForRowAtIndexPath: method of UITableView returns nil if the index path refers to a cell that is currently not visible, therefore
[cells addObject:[self.ammoTable cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
crashes in that case.
it lead to crash because you have only seven rows at the screen, when it all added to array after that it returns nil. for solving what you can do is create a method which take input as indexpath and returns cell object without using reuse cell.
The magic is in cellForRowAtIndexPath:. For certain values this is returning nil. Check this method and fix it.

App crash in button

I have face strange problem on UIButton.
When i tap button the app is crash .
I wrote below code for that...
-(IBAction)renameTest:(id)sender
{
NSLog(#"Tapped");
// UIButton *button = (UIButton *)sender;
NSUInteger row = 1;//button.tag;
NSString * titlename = [titleArray objectAtIndex:row];
RenameTest *renameVC = [[RenameTest alloc]initWithNibName:#"RenameTest" bundle:nil];
renameVC.titlespell = titlename;
NSLog(#"titlespell = %#",renameVC.titlespell);
NSLog(#"title = %#",titlename);
// [button release];
[self.navigationController pushViewController:renameVC animated:YES]; //here APP is cresh
[renameVC release];
}
I check also my .Xib file name .It is ok and files are there.
error msg is below :
2012-07-11 14:28:29.079 TestApp[238:207] -[__NSCFDictionary _isNaturallyRTL]: unrecognized selector sent to instance 0x73d8a80
Thanks in Advance.
[button release] is causing the problem. Remove it and check.
_isNaturallyRTL is an NSString method (private), and it looks like you are passing a dictionary instead of a string somewhere.
Breaking on the exception and showing us the call stack at that point would help tremendously.
If u have created the button in xib file then u cannot release it because you have not allocated it and claimed ownership.. U should call release only on objects you have allocated by calling alloc..
Remove the [button release] statement .. that should fix the crash!
You have a crash which is related to a dictionary and your titlename string is set equal to, titleArray objectAtIndex:row. I believe, without seeing the declaration of your variables, that titleArray is a dictionary, or is a NSMutableArray importing from a plist of dictionaries, either way you need to use objectForKey, when using dictionaries, like this:
[[titleArray objectAtIndex:(NSUInteger *)] objectForKey:(NSString *)]
Obviously replace (NSUInteger *) with your integer row and (NSString *) with the name of your key. This may not be the answer but from your crash report and visible code, this is what I assume.

Iteration of NSMutableArray of UITextView causes app to crash

I want to iterate Uitextview NsmutableArray but the program crashes and it gives the following error,
"""-[__NSCFString text]: unrecognized selector sent to instance 0x861c2f0"""
Please check this code , and give some possible solution...
Thank You...
for ( x = 0; x < TextViewArray.count; x++)
{
// here TextViewArray is a NSMutableArray
UITextView *textField1 = [TextViewArray objectAtIndex:x];
float diff3;
diff3 = [textField1.text floatValue]; // The program crashes in this line ,"""""" -[__NSCFString text]: unrecognized selector sent to instance 0x861c2f0""""" , here I am checking for a float value but i want to use string value instead of float value in this line how to do this?
NSLog(#"diff3 is %f",diff3);
textField1.text = [[NSString alloc] initWithFormat:#"%.2f",diff3];
TXT_First_Tag = [TextViewArray objectAtIndex:x]; // here uitextview *TXT_First_Tag;
NSLog(#"txt3 is %d",x);
TXT_First_Tag.tag = x;
NSLog(#" p is %d", x);
//textField1.text = [[NSString alloc] initWithFormat:#"%.2f",diff2];
//NSLog(#"Diff is %f",diff2);
}
Your array contains a String at the given index, hence the error """-[__NSCFString text]: unrecognized selector sent to instance 0x861c2f0""". Check that the Array only contains UITextViews
put a breakpoint before the crash and do a "po" in the gdb shell once control reaches breakpoint. This way you will no if you are trying to access right object or not

[__NSArrayM synchronize]: unrecognized selector exception

I used some NSMutableArray with UIImageView objects.
When I quit the app I get this exception message:
[__NSArrayM synchronize]: unrecognized selector sent to instance 0x4b3a910
In AppDelagate.m
NSMutableArray * aViewArray;
...
currentView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:displayImageNamed]];
[aViewArray addObject:currentView];
[currentView release];
In ViewController.m
UIImageView *myImage = [aViewArray objectAtIndex:i];
myImage.xxx = yyy ;
Looking at your comment regarding NSUserDefaults, you should not be releasing both languages and defaults since you don't have ownership (you didn't alloc/init them). Try removing those two calls to release and see if that resolves your issue.

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";
}