【问题标题】:Creating UIButtons创建 UIButton
【发布时间】:2012-06-24 09:08:00
【问题描述】:

loadView 期间,我正在创建20 个UIButtons,我想根据UIPageControl 的状态更改标题文本。

我有一个预保存 plist,它在当前页面发生更改时加载到名为 arrCharsNSArray 中,我将按钮标题设置为数组中的相关文本标题。执行此操作的代码是:

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

在设置按钮的标题时,它似乎不会自动释放上一个标题,并且分配会不断增加。我做错了什么?

之前有人告诉我,通过分配跟踪事物是追踪泄漏的不好方法,因为据我所知,该对象在 Instruments 中没有泄漏,但我的总生存分配继续攀升,直到我收到内存警告。如果有更好的跟踪方法,我很想知道。

更新

忘了提一下,我没有使用从数组中检索到的值,而是将标题设置为 @"Test" - 这很好,每次更改页面时都不会无限增加。

【问题讨论】:

    标签: iphone ios memory-management uibutton


    【解决方案1】:

    你做了很多不必要的字符串创建和格式化。您可以替换这些行:

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

    这些:

    NSString *title = [self.arrChars objectAtIndex:iArrPos];
    
    [uButton setTitle:title forState:UIControlStateNormal];
    [title release];
    

    title 上使用 setTitle: 会增加保留计数 - 请务必在之后使用 release

    【讨论】:

    • 谢谢。我最近才添加了这一点,因为我认为它可以让我自己释放内存,而不是依赖自动释放。关于 stringValue 添加,这现在使我的代码崩溃('-[__NSCFString stringValue]: unrecognized selector sent to instance 0x1c13d0')——这是否意味着我的 plist 没有正确设置为字符串?
    • 我将该部分改回stringWithFormatself.arrChars中存储了哪些类型的对象?
    • 数组加载如下: self.arrChars = [[NSArray alloc] initWithContentsOfFile: [[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/chars.plist"]];并包含字符组合(一些特殊字符)。结构为:123....so on...
    • 好的,我想我找到了问题所在。见上文。
    【解决方案2】:

    经过许多漫长的夜晚,我决定硬着头皮将这个发送到 Apple 的 DTS。

    虽然回复很有用,但不是我所希望的。在 arrChar 中偶尔会有字形,当加载时我被告知不要被卸载。完整解释:

    据我所知,iOS 不会在将字形加载到内存后将其卸载。这就是为什么您在 Instrument.app 中看到内存分配在加载新字符时会增加,并且在您以后来回时保持不变。 iOS 的设计行为几乎是我们无法改变的。如果你真的想控制内存使用,你可以交替将字符制作成图片,并使用 UIImage (imageWithContentsOfFile:) 来加载和释放。

    希望这对那里的人有所帮助。

    【讨论】:

      猜你喜欢
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      相关资源
      最近更新 更多