0

我正在尝试在视图之间传递一个自定义类。我已经为该类设置了一个属性,但是当我访问该属性时,属性总是返回 null:

第一视图.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.视图):

@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;

事件DTO.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;

事件DTO.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];
}
4

4 回答 4

0

请参阅此帖子以获取完整说明。简而言之,你做错了:

在视图控制器之间传递数据

于 2013-04-25T19:16:21.580 回答
0

您确定在访问andeventDTO之前设置或分配了该属性吗?eventNameeventDate

我的猜测是它eventDTO本身是零。nil在 Objective-C 中允许调用方法或访问对象的属性。它不会像在 C++ 或 Java 中那样崩溃,它只是什么都不做,nil如果 ht 方法有返回值则返回。

于 2013-04-25T19:16:43.077 回答
0

我猜该属性eventDTO没有正确初始化,它nil本身就是。

在 Objective-C 中发送消息nil是合法的,它只是默默地失败并返回nil。在学习 Objetive-C 时,这是一个常见的陷阱。访问属性只是发送消息的语法糖。

顺便说一句,Objective-C 中的“空对象”由 、not 或 表示,nil尽管它们都为零。nullNULLNO

于 2013-04-25T19:30:15.873 回答
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您的方法中LocateEventViewController添加

-(id)init {
...
self.eventDTO = [[YourClassHere alloc] init];
...
}
于 2013-04-25T19:47:22.767 回答