2013-03-15 37 views
0

我的代碼丟失,有我在對象的NSMutableArray得到「丟失」的問題。 我會更詳細地解釋它:的iOS:指針得到坡平回parentView時的iOS 6.1與ARC

  1. 有是一種具有的NSMutableArray應保持某些對象(地址)
  2. 每次我按下按鈕的陣列的數據將與NSLog的被顯示的父視圖然後導航控制器推到ChildView
  3. 的childview產生一個新的地址對象

如果我這樣做,然後按在視圖中後退按鈕,並希望嘗試同樣的情況(再次按下按鈕)a中的數據rray丟失,我得到一個EXC_BAD_ACCESS

我的假設:當我回去parentView ARC將釋放它的第二個觀點和一切。但我的地址對象會從數組引用,使該對象的ARC計數器不應該是0

誰能幫助我? 這裏是混凝土代碼

ParentViews控制器H:

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController 
@property (nonatomic, strong) NSMutableArray *adresses; 
@property int count; 
-(void)goFurther; 
@end 

ParentViews控制器M:

#import "ViewController.h" 
#import "SecondViewController.h" 
#import "Address.h" 

@interface ViewController() 
@end 

@implementation ViewController 

- (void)viewDidLoad 
{ 
    [email protected]"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控制器H:

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

@interface SecondViewController : UIViewController 
@property ViewController * viewCon; 
-(id) initWithView: (ViewController*) view; 
@end 

ChildViews控制器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]; 
    [email protected]"Door"; 
    [self.viewCon.adresses addObject: a]; 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [email protected]"Second View"; 
    [super viewDidLoad]; 
} 

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

@end 

回答

0

你必須駁回子視圖之前將數據發送回父視圖。

在您的子控制器的.h文件中使用方法didDismissWithData:聲明適用於您(可能是字符串或字典)的某種數據類型的@protocol(稱爲ChildControllerDelegate)。讓父視圖控制器符合協議。創建一個名爲id<ChildControllerDelegate>

類型的delegate孩子的@property然後解僱孩子之前,請撥打

[self.delegate didDismissWithData:newData]; 

在父控制器,處理此消息,並保存新的地址。

+0

我試了一下,並在childs控制器和協議中創建了一個委託對象,並在父視圖中添加了該協議......父級方法如下所示: - (void)addAddress:(Address *)address NSLog(@「嘗試添加地址」); NSLog(address.street); [self.adresses addObject:address]; } 這工作正常,只要我的子視圖不處理。但是當子視圖被刪除時,地址對象(保存在數組中的指針)不再可用 – user2173784 2013-03-16 08:10:01

+0

當然不是!這就是爲什麼你先把它發送給父母的原因。 – Mundi 2013-03-16 13:08:05

+0

問題是,當我嘗試訪問父對象(在將其傳回給委託後,並且在處理了childViewController之後)時,該對象被刪除,我得到一個EXC_BAD_ACCESS。我是否宣佈對象不正確? – user2173784 2013-03-17 08:15:48