我有3个班级
首先 - > MainViewController:
@interface MainViewController : UIViewController {
UtilityBadah *utility;
}
@property (strong, nonatomic) NSManagedObjectContext *managedObjectContext;
- (IBAction)option;
@end
@implementation MainViewController
@synthesize managedObjectContext = _managedObjectContext;
(IBAction)option{
UtilityBadah *util = [[UtilityBadah alloc] initWithContext:_managedObjectContext];
OptionController *ovc = [[OptionController alloc] init];
ovc.util = util;
ovc.managedObjectContext = _managedObjectContext;
[self.navigationController pushViewController:ovc animated:YES];
[util release];
[ovc release];
}
@end
第二 - > UtilityBadah:
@interface UtilityBadah : NSObject {
NSManagedObjectContext *managedObjectContext;
NSString *kitab;
NSString *lagu;
NSString *font;
NSString *sizefont;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) NSString *kitab;
@property (nonatomic, retain) NSString *lagu;
@property (nonatomic, retain) NSString *font;
@property (nonatomic, retain) NSString *sizefont;
(id) initWithContext: (NSManagedObjectContext *) context;
@end
@implementation UtilityBadah
@synthesize managedObjectContext;
@synthesize kitab;
@synthesize lagu;
@synthesize font;
@synthesize sizefont;
-(id) initWithContext: (NSManagedObjectContext *) context {
NSError *err;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *enDesc = [NSEntityDescription entityForName:@"OptionDB" inManagedObjectContext:context];
[request setEntity:enDesc];
NSArray *arrData = [context executeFetchRequest:request error:&err];
for (OptionDB *data in arrData) {
lagu = data.lagu;
kitab = data.kitab;
font = data.font;
sizefont = data.sizefont;
}
return self;
}
Thrid - > OptionController:
@interface OptionController : UIViewController{
NSManagedObjectContext *managedObjectContext;
UtilityBadah *util;
}
@property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain) UtilityBadah *util;
@end
(UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
bla..bla..
NSLog(@"value is %@",self.util.kitab);
cell.textLabel.text = [listData objectAtIndex:row];
return cell;
}
我想知道为什么这段代码:
NSLog(@"value is %@",self.util.kitab);
导致错误“已收到信号:”EXC_BAD_ACCESS“? 我想知道我将UtilityBadah对象从MainViewController传递给我的OptionController的方式是错误的。
非常感谢您的回答。
P.S我的xcode 4.2错误信息有什么问题,为什么它总是显示“程序接收信号:”EXC BAD ACCESS“?它不能提供信息?
答案 0 :(得分:1)
我认为你的init方法是错误的,尝试在开始时将自己分配给[super init],并在检查self!= nil后进行所有设置。这是init的基本形式:
- (id)init
{
self = [super init];
if (self)
//do setup
return self;
}
另外,您正在使用NSObject
的子类并将托管对象上下文与其关联,您应该更详细地查看核心数据,您应该使用NSManagedObject
s
答案 1 :(得分:0)
OptionController
中的媒体资源名称为util
而不是utility
如果您想访问utility
课程中的UtilityBadah
媒体资源,则需要执行以下操作:
self.util.utility
答案 2 :(得分:0)
在UtilityBadah
班级initWithContext
方法中,按以下方式更改for
循环
for (OptionDB *data in arrData) {
self.lagu = data.lagu;
self.kitab = data.kitab;
self.font = data.font;
self.sizefont = data.sizefont;
}
在您的情况下,由于您已分配而未通过该属性进行访问,因此不会保留该值。
答案 3 :(得分:0)
您需要先分配这些对象。以下是initWithContext方法的样子:
-(id) initWithContext: (NSManagedObjectContext *) context {
self = [super init];
if (self) {
NSError *err;
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *enDesc = [NSEntityDescription entityForName:@"OptionDB" inManagedObjectContext:context];
[request setEntity:enDesc];
NSArray *arrData = [context executeFetchRequest:request error:&err];
for (OptionDB *data in arrData) {
self.lagu = [NSString stringWithString:data.lagu];
self.kitab = [NSString stringWithString:data.kitab];
self.font = [NSString stringWithString:data.font];
self.sizefont = [NSString stringWithString:data.sizefont];
}
}
return self;
}