简体   繁体   中英

Creating UIButtons

During loadView I am creating 20 UIButton s that I would like to change the title text of depending on the state of a UIPageControl .

I have a pre-save plist that is loaded into a NSArray called arrChars on the event of the current page changing, I set the buttons titles to their relevant text title from the array. The code that does this is:

for (int i = 1; i < (ButtonsPerPage + 1); i++) {

    UIButton *uButton = (UIButton *)[self.view viewWithTag:i];

    if(iPage == 1) {
        iArrPos = (i - 1);
    } else {
        iArrPos = (iPage * ButtonsPerPage) + (i - 1);
    }

    [uButton setAlpha:0];

    NSLog(@"Trying: %d of %d", iArrPos, [self.arrChars count]);
    if (iArrPos >= [self.arrChars count]) {                    
        [uButton setTitle: @"" forState: UIControlStateNormal];
    } else {

        NSString *value = [[NSString alloc] initWithFormat:@"%@", [self.arrChars objectAtIndex:iArrPos]];
        NSLog(@"%@", value);

        [uButton setTitle: [[NSString stringWithFormat:@"%@", value]    forState: UIControlStateNormal];

        [value release];

        //////Have tried:
        //////[uButton setTitle: value forState: UIControlStateNormal];

        //////Have also tried:
        //////[uButton setTitle: [self.arrChars objectAtIndex:iArrPos] forState: UIControlStateNormal];

        //////Have also also tried:
        //////[uButton setTitle: [[self.arrChars objectAtIndex:iArrPos] autorelease] forState: UIControlStateNormal];
    }

    [uButton setAlpha:1];
}

When setting the Title of a button it does not appear to be autoreleasing the previous title and the allocation goes up and up. What am I doing wrong?

I have been told before that tracking things by allocations is a bad way to chase leaks because as far as I can see, the object is not leaking in Instruments but my total living allocations continue to climb until I get a memory warning. If there is a better way to track there I would love to know.

Forgot to mention that instead of using the value I retrieve from my array I just set the title to @"Test" - it is fine and there is no endless increase every time I change the page.

You're doing a lot of unnecessary string creation and formatting. You can replace these lines:

NSString *value = [[NSString alloc] initWithFormat:@"%@", [self.arrChars objectAtIndex:iArrPos]];
NSLog(@"%@", value);

[uButton setTitle: [[NSString stringWithFormat:@"%@", value] forState: UIControlStateNormal];

with these:

NSString *title = [self.arrChars objectAtIndex:iArrPos];

[uButton setTitle:title forState:UIControlStateNormal];
[title release];

Using setTitle: on title increases the retain count - be sure to release it after.

After many long nights I decided to bite the bullet and send this one into the Apple's DTS.

Although the response was useful, it was not what I had hoped. Within arrChar were occasionally glyphs that when loaded I am told do not get unloaded. Full explanation:

As far as I know, iOS doesn't unload the glyphs after they've been loaded into memory. That is why you see in Instrument.app the memory allocation rises when loading new characters, and keeps flat when you going back and forward later on. It is pretty much the design behavior of iOS that we can't change anything. If you really want to control the memory usage, you may alternately make the characters as images and use UIImage (imageWithContentsOfFile:) to load and release.

Hope this helps someone out there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM