每次在iOS应用程序中呈现WEPopover时,内存分配都会增长

时间:2013-06-08 15:56:13

标签: ios heap popover allocation xcode-instruments

我的应用程序存在一些问题,我目前正在使用WEPopover库(ARCified)来创建一些自定义弹出窗口。但是,每次我呈现一个弹出框和它的相应视图(由一个视图和一个用数组填充的tableview组成),乐器中的分配就会增长,即使我只是一次又一次地重新添加相同的弹出窗口。我错过了什么,或者这是正常行为,请参阅下面的代码。我应该让表格视图变弱还是什么?

ThemesPopOverViewController.h

#import <UIKit/UIKit.h>
#import "AppDelegate.h"


@interface ThemesPopOverViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>{
    NSArray *themes;
}
@property (nonatomic, retain) UITableView *tableView;
@end

ThemesPopOverViewController.m

#import "ThemesPopOverViewController.h"
@interface ThemesPopOverViewController ()
@end

@implementation ThemesPopOverViewController
@synthesize tableView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
      themes = [[NSArray alloc] initWithObjects:kRegexHighlightViewThemeArray];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 0, self.view.frame.size.width - 40, 350)];
    [self.tableView setBackgroundColor:[UIColor clearColor]];
    [self.tableView setDelegate:self];
    [self.tableView setDataSource:self];
    [self.view addSubview:self.tableView];
}

-(void) viewDidAppear:(BOOL)animated{
    int item = [themes indexOfObject:theDelegate.codeView.currentTheme];
    NSIndexPath *indexPath = [NSIndexPath indexPathForItem:item inSection:0];
    [self.tableView selectRowAtIndexPath:indexPath animated:YES scrollPosition:UITableViewScrollPositionNone];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;    //count of section
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //NSLog(@"%d",themes.count);
    return [themes  count];    //count number of row from counting array hear cataGorry is An Array
}

- (UITableViewCell *)tableView:(UITableView *)tableView
         cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *MyIdentifier = @"MyIdentifier";
    UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:MyIdentifier];
    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                      reuseIdentifier:MyIdentifier];
        UIView *bgColorView = [[UIView alloc] init];
        [bgColorView setBackgroundColor:[UIColor colorWithRed:25/255.0f green:185/255.0f blue:152/255.0f alpha:1.0f]];
        [cell setSelectedBackgroundView:bgColorView];
    }
    [cell.textLabel setFont:[UIFont fontWithName:@"Helvetica" size:16]];
    cell.textLabel.text = [themes objectAtIndex: indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%d", indexPath.row);
    [theDelegate.codeView setHighlightThemeFromString:[themes objectAtIndex:indexPath.row]];
    [theDelegate.codeView setNeedsDisplay];
    themes = nil; 
    [theDelegate removePop];  
}
@end

我添加了来自:

的popover
-(void) settingAct:(UIButton *)sender{
    if (!popover) {
        ThemesPopOverViewController *newView = [[ThemesPopOverViewController alloc] initWithNibName:@"SettingPopOverViewController" bundle:[NSBundle mainBundle]];
        self.popover = [[WEPopoverController alloc] initWithContentViewController:newView];
        [self.popover setContainerViewProperties:[self improvedContainerViewProperties]];
        [self.popover setPopoverContentSize:CGSizeMake(128, 360)];
        [self.popover presentPopoverFromRect:CGRectMake(sender.center.x+12, sender.center.y, 0, 20)
                                      inView:self.window.rootViewController.view
                    permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown|
                                              UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
                                    animated:YES];

        newView = nil;
    }else{
        [self removePop];
    }
}

“theDelegate”中用于解散弹出窗口的删除方法:

-(void) removePop{

    [self.popover dismissPopoverAnimated:YES];
    self.popover = nil;
}

Baseline =没有popovers,Heapshot1 =添加popover,Heapshot2 = popover被忽略,Heapshot3 =再次添加相同的popover,Heapshot4 = popover被忽略。

Snapshot    Timestamp   Heap Growth # Persistent
- Baseline -    00:07.464.624   1.59 MB 25041
Heapshot 1  00:11.759.060   50.34 KB    787
Heapshot 2  00:16.278.744   0 Bytes 0
Heapshot 3  00:24.312.874   30.39 KB    608
Heapshot 4  00:28.361.293   288 Bytes   7

1 个答案:

答案 0 :(得分:0)

在你的SettingsAct按钮~action~方法中执行类似这样的代码

 if (self.popoverController)
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
else
{
    ThemesPopOverViewController *newView = [[ThemesPopOverViewController alloc] initWithNibName:@"SettingPopOverViewController" bundle:[NSBundle mainBundle]];
    self.popover = [[WEPopoverController alloc] initWithContentViewController:newView];
    [self.popover setContainerViewProperties:[self improvedContainerViewProperties]];
    [self.popover setPopoverContentSize:CGSizeMake(128, 360)];
    [self.popover presentPopoverFromRect:CGRectMake(sender.center.x+12, sender.center.y, 0, 20)
                                  inView:self.window.rootViewController.view
                permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown|
                                          UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
                                animated:YES];
}

它适用于我:)