Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-17-2011, 01:28 PM   #1 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 2
xthenkx is on a distinguished road
Default 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?

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

Last edited by xthenkx; 07-17-2011 at 01:33 PM.
xthenkx is offline   Reply With Quote
Old 07-17-2011, 02:28 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by xthenkx View Post
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.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 07-17-2011, 04:39 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 2
xthenkx is on a distinguished road
Default

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.
xthenkx is offline   Reply With Quote
Reply

Bookmarks

Tags
nsmutablearray, uislider, uitextfield

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 399
16 members and 383 guests
blasterbr, buggen, Clouds, dre, EvilElf, HemiMG, jeroenkeij, jimmyon122, jonathandeknudt, LEARN2MAKE, Mah6447, n00b, nyoe, pungs, Sami Gh, stanny
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,668
Threads: 94,121
Posts: 402,900
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jonathandeknudt
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:54 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0