iOS:当弹出回到父View时,指针会丢失

时间:2013-03-15 12:01:26

标签: pointers ios6 nsmutablearray automatic-ref-counting

我使用ARC编写iOS 6.1代码并遇到问题,即我在nsmutablearray中的对象会丢失"丢失"。 我将更详细地解释它:

  1. 有一个父视图,它有一个NSMutableArray,它应该包含一些对象(地址)
  2. 每当我点击按钮时,数据的数据将与NSLog一起显示,然后导航控制器将推送到ChildView
  3. 子视图生成新的地址对象
  4. 如果我这样做然后按下视图中的后退按钮并想要尝试相同的情况(再次按下按钮)数组中的数据丢失,我得到一个EXC_BAD_ACCESS

    我的假设:当我回到parentView ARC时,取消分配第二个视图及其中的所有内容。但我的Address对象将从数组中引用,以便此对象的ARC计数器不应为0.

    任何人都可以帮助我吗? 这是具体的代码

    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控制器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];
        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
    

1 个答案:

答案 0 :(得分:0)

在解除子视图之前,您必须将数据发送回父视图。

在子控制器的ChildControllerDelegate文件中使用方法.h声明一个@protocol(比如称为didDismissWithData:),其中包含一些适合您的数据类型(可能是字符串或字典) 。让父视图控制器符合协议。创建名为delegate

类型id<ChildControllerDelegate>的子项的@property

然后在解雇孩子之前,请致电

[self.delegate didDismissWithData:newData]; 

在父控制器中,处理此消息并保存新地址。