更改后清理视图实例

时间:2014-05-06 12:11:28

标签: objective-c xcode uiview dealloc

我发现,每次我更改视图时,都会创建它们的新实例。 (内存会增加视图的每次更改)。 如果我去上一个,我想取消实际视图。 这不能通过dealloc来完成,因为我正在使用ARC。 link between the views

" BACK" -Button只链接到配置菜单。当时我需要释放错误视图以在下次创建新实例 Error视图的init如下所示:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

配置菜单的其他按钮也只链接到下一个视图。所以我不手动创建它们的实例。如果您需要其他部分代码,我会将其放入。

更多代码:

配置视图:(几乎为空,只有3个链接按钮)

#import "ConfigMenuViewController.h"

@interface ConfigMenu ()

@end

@implementation ConfigMenu

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

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];

}

@end

ErrorView.m:

#import "Error.h"
#import "CANlinkWLAN.h"
#import "SocketConnectionControllerThread.h"

@interface Error ()

@end

@implementation Error
@synthesize canBufferOverflow;
@synthesize canTransmitTimeout;
@synthesize canErrorcounterOverflow;
@synthesize canBusOffError;
@synthesize usbtors232SyntexError;
@synthesize usbtors232FormatError;
@synthesize usbtors232BufferOverflow;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter ]addObserver:self selector:@selector(receivedMessage:) name:@"GET_ERROR_STATUS" object:nil];
}

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


- (IBAction)readError:(id)sender {
    [[SocketConnectionControllerThread sharedInstance] sendCommand: GET_ERROR_STATUS];
}

- (void) receivedMessage: (NSNotification *) note{
    NSDictionary *userInfo = [note userInfo];
    unsigned char error = [(NSNumber *)[userInfo objectForKey:@"GET_ERROR_STATUS"] integerValue];
    NSLog(@"Error: %d", (int)error);
    [self receivedError: &error];
}

- (void) receivedError:(unsigned char *) msg{

    //change some colors ( not important) ; 
}
- (void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"GET_ERROR_STATUS" object:nil];
}

@end

ErrorView.h

#import "ViewController.h"

@interface System : ViewController
{
    UILabel *serverEcho;
    UILabel *messageEcho;
    UILabel *usbtors232Output;
}

- (IBAction)reset:(id)sender;
- (IBAction)setAutobaud:(id)sender;
- (IBAction)readFeedback:(id)sender;
- (void) receivedMessage: (NSNotification *) note;
@property (nonatomic, retain) IBOutlet UILabel *usbtors232Output;
@property (nonatomic, retain) IBOutlet UILabel *messageEcho;
@property (nonatomic, retain) IBOutlet UILabel *serverEcho;

@end

2 个答案:

答案 0 :(得分:1)

ARC会发生的情况是,计数器会自动增加所有指向对象的强指针。如果将所有强指针设置为nil,ARC将自动释放为该对象分配的内存。你可以做的是在你的ErrorView中实现dealloc方法并记录一些东西。然后看看它是否已取消分配。只需设置为nil指向此对象的所有指针,内存将被释放。

修改更多代码:

我认为你的dealloc方法永远不会被调用,因为你将自己添加为UINotification的观察者,并且你在dealloc方法(从不调用)中将它自己从中删除,所以它创建了一个你永远不会离开的“内存循环” 。尝试拨打

[[NSNotificationCenter defaultCenter]removeObserver:self name:@"GET_ERROR_STATUS" object:nil];

例如,按后退按钮时。

答案 1 :(得分:0)

我找到了方法。比我预想的要容易得多。

- (IBAction)goBack:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

这样我真的回到上一个视图并释放分配的内存。 + enter image description here

相关问题