简体   繁体   中英

The right way to hand over an object to a view?

i'm a bit new to iphone development. I made it to develop a nice App which loads XML Data from a feed, displays this data in a UITableView and if a user taps a row there should be a detail view which displays the data.

Thats where i got stuck a little bit. It's not clear for me how to hand over the data of the entry selected by the user to my detail view. The Detail-View is called via presentModalView...

I thought about:

  1. Calling a "setDetails:(PostingData *)myPosting" function of the viewController of my detail view.

  2. presenting the detailView to the user by calling presentModalViewAnimated.

The view is presented, but the setDetails: function crashes without any output to the debugger console.

MY QUESTION:

What is the right way to hand over Data (in custom objects as instance of a self written class) from my view Controller to a View Controller which should display detail data.

Any hint or help is appreciated. I can't pay you for your help, but i'm on my way becoming better and then helping others too :-).

Method 1: Pass it in custom init method

In your Header File declare a property

@property (nonatomic, retain) id myDataObject;

And in your implementation use a custom init like this

 -(id)initCustom:(id)myObject;
    if(self = [super init]) {
        myDataObject = [myObject retain];
    }
    return self;
  }

Method 2: Use a property

Use @property in your Header and @synthesize in your .m Implementation File

   [CustomUIViewController* newViewController = [[CustomUIViewControlleralloc] init];
   newViewConroller.myDataObject = myObject;
   [view addSubview:newViewController.view];
   [newViewController release];
  1. define the custom object in your class. @property(nonatomic, retain) MyClass * myClass;
  2. load the feed into a NSMutableDictionary and provide that to your class
 [YOUR_VIEW_CONTROLLER *yourViewController = [[YOUR_VIEW_CONTROLLER alloc] init]; yourViewController.PROPERTY_DEFINED_BEFORE = yourObject; [view addSubview:yourViewController.view]; [yourViewController release]; 

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM