Creating UIButtons
Posted
by
Ralphonzo
on Stack Overflow
See other posts from Stack Overflow
or by Ralphonzo
Published on 2012-06-24T09:08:00Z
Indexed on
2012/06/24
9:15 UTC
Read the original article
Hit count: 166
During loadView I am creating 20 UIButtons 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.
© Stack Overflow or respective owner