Hi,
What are the possible ways to send data to previous view in iphone. Without using Appdelegate. Because there are chances for my view class to be instantiated again.
Hi,
What are the possible ways to send data to previous view in iphone. Without using Appdelegate. Because there are chances for my view class to be instantiated again.
There are several ways to achieve data sharing, with Singleton Objetcs being one of the most popular:
If the view you want to communicate with is a parent view (e.g. the previous view's view controller is where you created this view) then you probably want to handle dismissing the view in the previous view controller. When you do that, you can read the data that has changed and update the previous view controller with the new data.
Then in the viewWillAppear: method of the previous view controller, update the actual views to reflect the current state of the view controller.
Edit: I've just noticed that your newView is transparent. If this is the case, then you certainly want to route all logic through your view controller. You should only have one view controller with visible views at a time.
I believe the best approach is using the NSNotificationCenter
class.
Basically what you do is register an object (as an observer) with a notification center.
So for example if you have objects A
and B
. A
registers as an observer. Now lets say A
is the "previous" object you are talking about, you can have B
send a notification (data or message) to the notification center which then notifies object A
(and any other registered observers).
Example:
In file ClassA.m
register as shown below:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSomething:) name:@"SomethingHappened" object:nil];
didSomething
is the method which receives the notification sent by object B
. This will look something like
- (void) didSomething: (NSNotification *) notify {
...
}
Finally you send the message below from whatever method in ClassB.m
to notify/send data to object A
[[NSNotificationCenter defaultCenter] postNotificationName:@"SomethingHappened" object:self userInfo:your_data];
Seems convoluted but it's the best approach in my opinion (and quite simple once you understand it :)).