0

I'm trying to pass a custom class between views. I have set a property for that class but when i access the property, the attributes return always 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;

so when i log a attribute of eventDTO it returns null..

any help would be great

EDIT 1 (2. view): Thats how I log:

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];
}
SaifDeen
  • 862
  • 2
  • 16
  • 33

4 Answers4

0

Please see this post for a full explanation. In short, you're going about it wrong:

Passing Data between View Controllers

Community
  • 1
  • 1
Jordan
  • 2,992
  • 2
  • 20
  • 29
  • i did the same ... it is working with a string property.. but not with that class.. This Class is a subclass of NsManagedObject. is that maybe the problem? – SaifDeen Apr 25 '13 at 19:30
  • Could be. You would have to post all the code in order to tell for sure. The data contained in that link should get you going in the right direction though. – Jordan Apr 25 '13 at 19:33
  • We have to see all the code in order to figure it out. What about EventDTO.h and .m? – Jordan Apr 25 '13 at 19:43
0

Are you sure that the property eventDTO is set or allocated before accessing eventName and eventDate?

My guess is that eventDTO itself is nil. Calling method or accessing properties on nil objects is allowed in Objective-C. It does not crash as it would do in C++ or Java, it simply does nothing and returns nil if ht method has a return value.

J_D
  • 3,526
  • 20
  • 31
0

I guess the property eventDTO is not properly initialized and it is nil itself.

Sending message to nil is legal in Objective-C, it simply fails silently and returns nil. It's a common pitfall when learning Objetive-C. And accessing property is just a syntactic sugar of sending messages.

BTW, "empty object" in Objective-C is represented by nil, not null, NULL or NO, although they are all zero.

leafduo
  • 144
  • 1
  • 5
  • Add a line like `self.eventDTO = [[EventDTO alloc] init];` in `-init` method of `LocateEventViewController`. – leafduo Apr 25 '13 at 19:45
0
LocateEventViewController *locView = [[LocateEventViewController alloc]init];
locView.eventDTO.name = eventName;
locView.eventDTO.date = eventDate;

When you initiating locView, the object eventDTO doesn't exists. You have to initiate a new eventDTO right there, e.g.:

LocateEventViewController *locView = [[LocateEventViewController alloc]init];
YourClass *eventDTO = [[YourClass alloc] init];
locView.eventDTO = eventDTO;
locView.eventDTO.name = eventName;
locView.eventDTO.date = eventDate;

or directly in the -initmethod of your LocateEventViewController by adding

-(id)init {
...
self.eventDTO = [[YourClassHere alloc] init];
...
}
Philip
  • 112
  • 14
  • thanks.. that did it... ty. but i want to know why ? why i dont need init a NSSTring for example? – SaifDeen Apr 25 '13 at 19:53
  • You can init a string by multiple ways. One of these is to do something like the following: `NSString *myString = @"Hello, World!";`. I assume you did something like `SomeViewConroller.someString = @"Hello, Universe";`. So by adding a @-String to a property, it is initialized at the same time. That was a very short asnwer, but i should be understandable. – Philip Apr 25 '13 at 19:55