Quote:
Originally Posted by xthenkx
So, I have the slider adding UITextFields but it's not updating/subtracting UITextField's when selecting less than the previous Slider Value. What am I doing wrong?
Code:
- (IBAction) sliderValueChanged:(UISlider *)sender {
float senderValue = [sender value];
int roundedValue = senderValue * 1;
ingredientLabel.text = [NSString stringWithFormat:@"%d", roundedValue];
NSMutableArray *textFieldArray = [[NSMutableArray array] init];
int moveYBy = 35;
int baseY = 140;
for(int y = 0; y < roundedValue; y++){
if(y >= [textFieldArray count]){
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, baseY, 227, 31)];
textField.text = [NSString stringWithFormat:@"%d", roundedValue];
textField.borderStyle = UITextBorderStyleRoundedRect;
baseY = baseY + moveYBy;
[textFieldArray addObject:textField];
[self.view addSubview:textField];
[textField release];
NSLog(@"Adding %d fields!", roundedValue);
}
while([textFieldArray count] > roundedValue){
UITextField *textField = [textFieldArray lastObject];
[textField removeFromSuperview];
[textField removeLastObject];
}
}
NSLog(@"%d", roundedValue);
}
|
What are you trying to do?
You want to create as many text fields as the current slider value, and remove text fields if the slider value decreases?
Why? This sounds like a rather screwy user interface. It's also likely to be very jerky and sluggish. Creating and releasing views is a fairly expensive operation, and doing it "live" as the user drags a slider probably won't be smooth.
Instead, I would suggest creating the maximum number of text fields as static objects in IB, and showing/hiding them as the user drags the slider. That would be much less memory-intensive, and the code would be a lot simpler as well.
You could put a tag on every text field, and then in your viewDidLoad method, loop through the tag numbers and use the viewWithTag method to find them and put them into an array. (make sure to empty the array in your viewDidUnload method to avoid a memory leak)
-----------------
Given that your current approach is probably not the best, I'm not sure how much value there is in debugging it. Since you asked, though, here's what I see:
On entry to the method you posted, you create a new array, and populate it with roundedValue new text fields. After you're done adding roundedValue new text fields to the newly created array, you then have code that tries to delete any extra text fields in the array. However, since you create the array at the beginning of the method, there will never be any extra text fields to remove.
Your code has multiple problems. You always add enough new text fields for the new value of roundedValue, but never delete any of the old text fields. You can't tell, but there are going to be an every-increasing number of stacked text fields at each location on the screen.
Then, at the end of your method, you forget about the textFieldArray, which leaks both the array and the text fields that are in the array.