sending data to previous view in iphone

一个人想着一个人 提交于 2019-12-02 17:07:51

问题


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.


回答1:


There are several ways to achieve data sharing, with Singleton Objetcs being one of the most popular:

Objective C Singleton




回答2:


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 :)).




回答3:


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.



来源:https://stackoverflow.com/questions/3417946/sending-data-to-previous-view-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!