IPhone app. How can I view the invocation method in another view?

I am making a simple iPhone app for a tab bar. It has two tabs, one with a UIWebView, and another with a few text fields to save settings, and a button to save settings.

What I want to do is reload the UIWebView when the user clicks on the tab / view settings. I already have the settings saving, I just need to figure out how to enable the call to update the UIWebView.

I am confused as to how a view can send messages to another view.

+1


a source to share


5 answers


The fact that you are using these two views within the UITabBarController is irrelevant to the core of the problem; namely that you want one part of your statement to say that something happened in another part. A typical way to do this with KVO or with NSNotificationCenter:



// Register interest in finding out when the settings are changed
[[NSNotificationCenter defaultCenter] addObserver:myWebViewController 
    selector:@selector(refreshWebView:) name:@"settingsSaved" object:nil];

// Notify interested parties that the settings have changed
[[NSNotificationCenter defaultCenter] postNotificationName:@"settingsSaved" object:nil];

      

+5


a source


I made them talk to each other via AppDelegate.

For instance:

View1 will have a function that does:

MyAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    [appDelegate myFunction];

      

MyAppDelegate has a function myFunction that will either do something or call:

[viewController myViewFunction];

      



I've created a pretty nice system to load multiple views (and navigate back and forth as if they were different screens). I just didn't have time to write this and publish it on pushplay.net - but I'll try to do it in the next couple of days ...

===

EDIT: Ok, I posted what I have. Any comments would be appreciated.

http://pushplay.net/blog_detail.php?id=27

I understand what the commenters below are saying, but I have not yet seen an example that works without issue, based on the assumption that multiple view controllers have to navigate among themselves.

+4


a source


It depends on the complexity of the particular application and the object model in that application:

  • If views are related to each other and messages are specific to views, they can send messages to each other directly. For example, a view can (and should) send messages directly to its sub-views, and they can send messages directly to their parent.

  • If they are unrelated but show data about the same information, you should proxy messages, although the viewController is clearly why you have a view controller.

  • If you have a complex application with a strong MVC architecture, communication should be done indirectly by updating model objects and cascading this to views through their viewControllers.

+1


a source


In this case, you would probably be better off if your app delegate assigns the controller holding the web view as a delegate to your other view controller when the app starts - look at using a protocol to make it clean, but for something simple you can always just declare a delegate type as your view controller.

Notifications are also a great idea and not too small a task for them, but you should know that it is important to opt out of notifications in places like dealloc. For the top level of the tab bar display controller, it really doesn't matter.

+1


a source


You can always set up an instance variable in the tab view controller that points to the web view. Just set it as IBOutlet, wire it up in the Interface Builder and then call your method on an instance variable.

.h file:

IBOutlet UIWebView *webView;

@property (nonatomic, retain) UIWebView *webView;

      

.m file:

@synthesize webView;

//Then later in your method
[webView refresh]; //or however you do it

      

Hello,

Kyle

0


a source







All Articles