我正在尝试在视图之间传递自定义类。我为该类设置了一个属性,但是当我访问该属性时,属性返回null:
firstview.m:
- (void)initNextEventView{
NSDate *eventDate = [eventDatePicker date];
NSString *eventName = eventNameTextfield.text;
LocateEventViewController *locView = [[LocateEventViewController alloc]init];
locView.eventDTO.name = eventName;
locView.eventDTO.date = eventDate;
[[self navigationController] pushViewController:locView animated:YES];
}
LocateEventVC.h(2。view):
@interface LocateEventViewController : UIViewController<MKMapViewDelegate, UISearchBarDelegate, BSForwardGeocoderDelegate>
@property (strong, nonatomic) IBOutlet UIImageView *imageBackground;
@property (strong, nonatomic) EventDTO *eventDTO;
所以当我记录 eventDTO 的属性时,它会返回null ..
任何帮助都会很棒
编辑1(2.查看): 多数民众赞成我如何记录:
NSLog(@"name on 2. view: %@", eventDTO.name);
self.eventDTO.place = tmpEventPlace;
// init friends view!
//passing to next view
EventMembersViewController *members = [[EventMembersViewController alloc]init];
members.eventDTO = self.eventDTO;
EventDTO.h
@interface EventDTO : NSObject
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) UserDTO * creatorIsAppUser;
@property (nonatomic, retain) ContactDTO * creatorIsNotAppUser;
@property (nonatomic, retain) NSString * description;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, readwrite) NSInteger *eventID;
@property (nonatomic, retain) CustomPlacemark * place;
@property (nonatomic, retain) NSMutableArray *invitedMembers;
@property (nonatomic, retain) NSMutableArray *acceptedMembers;
- (NSNumber*)getLatidude;
- (NSNumber*)getLongitude;
EventDTO.m
@synthesize place, date, eventID, name, acceptedMembers, description, invitedMembers;
- (NSNumber*)getLatidude{
return [NSNumber numberWithDouble:place.coordinate.latitude];
}
- (NSNumber*)getLongitude{
return [NSNumber numberWithDouble:place.coordinate.longitude];
}
答案 0 :(得分:0)
请参阅此帖子以获取完整说明。简而言之,你错了:
答案 1 :(得分:0)
在访问eventDTO
和eventName
之前,您确定已设置或分配了属性eventDate
吗?
我的猜测是eventDTO
本身就是零。在Objective-C中允许调用方法或访问nil
对象上的属性。它不像在C ++或Java中那样崩溃,它只是什么都不做,如果ht方法有返回值,则返回nil
。
答案 2 :(得分:0)
我认为属性eventDTO
未正确初始化,而且nil
本身就是。{/ p>
在Objective-C中向nil
发送消息是合法的,它只是无声地失败并返回nil
。学习Objetive-C时,这是一个常见的陷阱。访问属性只是发送消息的语法糖。
BTW,Objective-C中的“空对象”由nil
表示,而不是null
,NULL
或NO
,尽管它们都是零。
答案 3 :(得分:0)
LocateEventViewController *locView = [[LocateEventViewController alloc]init];
locView.eventDTO.name = eventName;
locView.eventDTO.date = eventDate;
当您启动locView
时,对象eventDTO不存在。你必须在那里发起一个新的eventDTO,例如:
LocateEventViewController *locView = [[LocateEventViewController alloc]init];
YourClass *eventDTO = [[YourClass alloc] init];
locView.eventDTO = eventDTO;
locView.eventDTO.name = eventName;
locView.eventDTO.date = eventDate;
或直接在您-init
的{{1}}方法中添加
LocateEventViewController