Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Google Sign In with OpenID
Please do not post the same thing multiple times. The board software automatically flags certain posts as needing moderator attention. This happens the most often for new users. I'm pretty sure this is made clear at the time you attempt to post. Posting the same thing over and over again just makes that many more posts the moderators have to weed through later. This makes us sad. Don't make us sad. If your post/thread doesn't appear, just wait a while. Don't post it again. If it hasn't shown up by the next day, then you can try again. I normally go through posts in the mornings, and try to check a few times throughout the day, but I'm not here 24/7. There will typically be a significant delay before posts are approved. Just be patient.

UISlider adding UITextField but not updating

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?


- (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);
}
Post edited by xthenkx on

Replies

  • Duncan CDuncan C Posts: 8,024Tutorial Authors, Registered Users
    xthenkx;355774 said:
    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?


    - (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.
    Regards,

    Duncan C
    WareTo

    mug

    Animated GIF created with Face Dancer, available for free in the app store.
  • xthenkxxthenkx Posts: 2New Users
    Alright, I'll get back to the drawing board then. I rather do it without bugs and leaks. This is basically my first app and I'm new to all of this.

    But basically all I was trying to do is add UITextFields with UISlider. For the most part half it worked. Just not the removing part when you drag to a lesser value. It seemed like a practical feat but I guess not.

    Thanks again.
Sign In or Register to comment.