C.Johns
C.Johns

Reputation: 10245

sending data around complex navigational stacks

I would like some opinion, example and code on how everyone dose this..

So far I have been getting on fine using protocols and delegates for passing information back up through the navigaion controller from view to view.

However just recently I have had to pass some data across multiple views on the controller stack and just cannot figure out how to achive this with delegates and protocols.. I have asked some other question that have helped me under stand alot about what im trying to do but I want to come up with a application wide solution instead of several hashed together ideas that only just do what I need..

Basicly what I'm looking to do is is this

Navigation stack
- First View
-- second View
--- Third View - current view.

Pass data from Third to first view is the goal. I know how to pop to the first view controller.. I just don't know how to get information to it over that distance... The majour thing holding me back atm is that I cannot declare the delegate in the First View for the Third View delegate protocol with out the application falling over.

Upvotes: 0

Views: 76

Answers (4)

sElanthiraiyan
sElanthiraiyan

Reputation: 6268

Depending upon the data that you like to pass back you can use a Singleton, NSUserDefault, pass it via AppDelegate, use delegation pattern, or just use set of properties. The decision should be made by you according to the design and the requirement. Assuming that you just wanna pass back a string you can do the below steps,

Declare a property in the FirstViewController.

@property (nonatomic, retain) NSString *thePassbackValue;

Access the FirstViewController's instance in the ThirdView.

FirstViewController *fvc = [self.navigationController.viewControllers objectAtIndex:0];

Set the value to the property,

fvc.thePassbackValue = @"someValue";

Access the value back in the first view,

//In your first view,

if(self.thePassbackValue != nil)
{
//Use the value
NSLog(@"the value %@", self.thePassbackValue);
}

Upvotes: 0

Collin Price
Collin Price

Reputation: 5850

Have you considered storing the data on the device and accessing it separately. You could store all your data in an sqlite database and refresh the data when the view controller calls:

– viewWillAppear:

Upvotes: 0

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

This how to do it if your delegate is defined in the Third View,

In your third view:

 YourFirstViewController *fvc = [self.navigationController.viewControllers objectAtIndex:0];
    self.delegate = fvc;
    [self.delegate yourMethodHere];

make sure your firstviewcontroller conforms to the protocol

<thirdViewDelegate>

and import third view in first view.h

Upvotes: 1

AechoLiu
AechoLiu

Reputation: 18408

I suppose to use observer pattern with NSNotificationCenter class and NSNotification class. You can read the apple official document here. That document page contains a NSNotification Programming Topics link inside. The data can be send to observers by a NSDictionary for userInfo.

With observer pattern, those view controller can be loose coupling. It is more flexible when changing original designs while developing project.

Upvotes: 0

Related Questions