首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >每当WEPopover出现在iOS应用程序中时,内存分配就会增加。

每当WEPopover出现在iOS应用程序中时,内存分配就会增加。
EN

Stack Overflow用户
提问于 2013-06-08 15:56:12
回答 1查看 204关注 0票数 0

我的应用程序有一些问题,我目前正在使用WEPopover库(ARCified)创建一些定制的弹出程序。然而,每次我呈现一个popover和它对应的视图(包括一个视图和一个填充数组的表视图),仪器中的分配就会增加,即使我只是一次又一次地重新添加相同的弹出视图。我缺少什么,或者这是正常行为,请参阅下面的代码。我是不是应该把桌子的视图变弱什么的?

ThemesPopOverViewController.h

代码语言:javascript
复制
#import <UIKit/UIKit.h>
#import "AppDelegate.h"


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

ThemesPopOverViewController.m

代码语言:javascript
复制
#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

我把波波夫添加到:

代码语言:javascript
复制
-(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“中用于删除弹出框的删除方法:

代码语言:javascript
复制
-(void) removePop{

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

基线=无弹出,Heapshot1 =添加popover,Heapshot2 = popovers,Heapshot3 =再次添加相同的弹出,Heapshot4 =popovers。

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2013-09-17 08:16:37

在您的SettingsAct按钮~action~方法中,代码如下所示

代码语言:javascript
复制
 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];
}

它对我来说很好:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17001280

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档