我的问题是我想将一个对象从子视图(控制器)返回到父视图(控制器)。我通过这样的调用传递了对象:
parentView.someObject=objectFromChild;
一切都没问题,直到子视图被删除(因为它被加速并且没有指针显示),但是从子视图传递到父视图的对象也会被删除。任何人都可以告诉我如何保存我的对象(即使创建它的视图被删除)?使用NSString,我的方法非常有效......但是对于对象,我总是得到EXC_BAD_ACCESS
答案 0 :(得分:0)
确保父对象保留它。
答案 1 :(得分:0)
听起来有一些挑战,我会尝试提供一些简单的提示。
传递数据
如果要将对象从子项向上传递给父项,请设计Child类,以使对象或变量是公共属性。然后,任何其他对象(像拥有Child 的Parent对象)都可以访问该属性。
保持数据活跃
通常EXC_BAD_ACCESS表示该对象已被系统删除。通过在属性声明中设置“strong”来告诉系统您想要挂起对象,这将解决您的EXC_BAD_ACCESS问题。
请查看以下代码,了解如何实现非常简单的父/子数据关系并保留数据的示例。
//****** Child.h
@interface Child : NSObject
// Child has a public property
// the 'strong' type qualifier will ensure it gets retained always
// It's public by default if you declare it in .h like so:
@property (strong, nonatomic) NSString *childString;
@end
//****** ParentViewController.h
#import <UIKit/UIKit.h>
#import "Child.h"
@interface ParentViewController : UIViewController
@property (strong, nonatomic) Child *myChild;
@end
//****** ParentViewController.m
@implementation ParentViewController
@synthesize myChild;
- (void)viewDidLoad {
[super viewDidLoad];
// create child object
self.myChild = [[Child alloc] init];
// set child object (from parent in this example)
// You might do your owh setting in Child's init method
self.myChild.childString = @"Hello";
// you can access your childs public property
NSLog(@"Child string = %@", self.myChild.childString;
}