I code for iOS 6.1 with ARC and have a problem that my objects in the nsmutablearray get "lost".
I will explain it in more detail:
- There is a parent view which has a NSMutableArray which should hold some objects(addresses)
- Everytime I hit the button the data of the Array will be displayed with NSLog and then the navigation controller pushes to the ChildView
- The childview generates a new address object
If I do this and then press the back button in the view and wants to try the same case (again pressing the button) the data in the array is lost and I get a EXC_BAD_ACCESS
My assumption: When I go back to parentView ARC deallocates the second view and everything in it. But my Address object will be referenced from the array so that the ARC counter for this object should not be 0.
Can anyone help me?
Here is the concrete code
ParentViews Controller h:
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@property (nonatomic, strong) NSMutableArray *adresses;
@property int count;
-(void)goFurther;
@end
ParentViews Controller m:
#import "ViewController.h"
#import "SecondViewController.h"
#import "Address.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
self.title=@"First View";
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithTitle:@"Add address" style:UIBarButtonItemStylePlain
target:self action:@selector(goFurther)];
[self.navigationItem setLeftBarButtonItem:addButton];
self.adresses=[[NSMutableArray alloc] init];
self.count=0;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) goFurther{
for (Address *a in self.adresses) {
NSLog(@"Adresse:");
NSLog(a.street);
NSLog(a.door);
}
self.count++;
SecondViewController *second=[[SecondViewController alloc]initWithView:self];
[self.navigationController pushViewController:second animated:true];
}
@end
ChildViews Controller h:
#import <UIKit/UIKit.h>
#import "ViewController.h"
@interface SecondViewController : UIViewController
@property ViewController * viewCon;
-(id) initWithView: (ViewController*) view;
@end
ChildViews Controller m:
#import "SecondViewController.h"
#import "Address.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(id) initWithView: (ViewController*) view{
self = [super init];
self.viewCon=view;
Address *a=[Address alloc];
a.street=[NSString stringWithFormat:@"Street %d", self.viewCon.count];
a.door=@"Door";
[self.viewCon.adresses addObject: a];
return self;
}
- (void)viewDidLoad
{
self.title=@"Second View";
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Copyright Notice:Content Author:「user2173784」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/15431935/ios-pointer-get-lost-when-poping-back-to-parentview