Search code examples
objective-cxcodeuiviewdealloc

clean up the instance of view after change


I found out, that every time I change the view the new instance of them will be created. (Memory increase every change of the view). I would like to dealloc the actually view, if I go the the previous one. This can not be done by dealloc, because I am using ARC. link between the views

The "BACK"-Button is just linked to the Config Menu. At the time I need to dealloc the error view to create the new instance next time.
The init of the Error view looks like:

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

The other buttons of the config menu are also just linked to the next view. So I do not create the instance of them manually. If you need some other parts of code, I will put it in.

MORE CODE:

Config View:(almost empty, only 3 linked buttons)

#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

Solution

  • I found the way. It was much easier as I expected.

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

    That way I really go back to the previous view and release the allocated memory. + enter image description here