简体   繁体   English

iPhone应用程序-一个视图如何在另一个视图中调用方法?

[英]iPhone Application - How can one View call a method in another View?

I'm making a simple tab bar iPhone application. 我正在制作一个简单的标签栏iPhone应用程序。 It has two tabs, one with a UIWebView and the other with a few text fields to hold settings, and a button to save the settings. 它有两个选项卡,一个带有UIWebView,另一个带有一些文本字段来保存设置,还有一个按钮来保存设置。

What I want to do is reload the UIWebView when the user clicks save on the settings tab/view. 我想做的是,当用户单击“设置”选项卡/视图上的“保存”时,重新加载UIWebView。 I already have it saving the settings, I just need to figure out how to put in a call to make the UIWebView refresh itself. 我已经保存了设置,只需要弄清楚如何进行调用即可使UIWebView刷新。

I'm confused about how a view can send messages to another view. 我对一个视图如何将消息发送到另一个视图感到困惑。

The fact that you're using these two views inside the framework of a UITabBarController is irrelevant to the core of the problem; 您在UITabBarController的框架内使用这两个视图的事实与问题的核心无关。 namely, that you'd like one part of your application to be told that something has occurred in another part. 也就是说,您希望您的应用程序的一部分被告知在另一部分中发生了某些事情。 The typical way of doing this is with KVO, or by using the NSNotificationCenter like so: 这样做的典型方法是使用KVO或使用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];

What I've done is have them talk to each other via the AppDelegate. 我要做的是让他们通过AppDelegate互相交谈。

For example: 例如:

View1 will have a function that does: View1将具有以下功能:

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

MyAppDelegate has a function called myFunction that will either do something, or call: MyAppDelegate有一个名为myFunction的函数,该函数可以执行某些操作或调用:

[viewController myViewFunction];

I've set up a pretty nice system to load multiple views (and navigate back and forth, as if they were different screens). 我已经建立了一个非常不错的系统来加载多个视图(并来回导航,就像它们是不同的屏幕一样)。 I just haven't made time to write this up and post it to pushplay.net -- but I'll try to do so in the next couple of days... 我只是没有时间写这篇文章并将其发布到pushplay.net上-但我会在接下来的几天内尝试这样做...

=== ===

EDIT: Okay, I've posted what I have. 编辑:好的,我已经发布了我所拥有的。 Any comments would be appreciated. 任何意见,将不胜感激。

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

I understand what the commenters below are saying, but I have yet to see an example that works seamlessly, based on having multiple viewcontrollers needing to navigate between each other. 我理解下面的评论者在说什么,但是我还没有看到一个可以无缝运行的示例,该示例基于多个视图控制器之间需要进行导航。

It depends on the complexity of the specific application and the object model in that application: 这取决于特定应用程序的复杂性以及该应用程序中的对象模型:

  • If the views are related and the messages relevant only to the views then they can send each other messages directly. 如果视图相关并且消息仅与视图相关,则它们可以直接彼此发送消息。 For example a view can (and should) send messages directly to it's sub views and they can send messages direcly to their parent. 例如,一个视图可以(并且应该)直接向其子视图发送消息,并且它们可以直接向其父级发送消息。

  • If they views are not related but show data about the same information you should proxy messages though the viewController - this is exacly why you have a view controller. 如果它们的视图不相关,但显示有关相同信息的数据,则应通过viewController代理消息-这就是为什么要有视图控制器的原因。

  • If you have a complicated application with a strong MVC architecture, the communication should be done indirectly by updating the model objects and cascading this out to the views through their viewControllers. 如果您的应用程序具有强大的MVC体系结构,则应通过更新模型对象并通过其viewControllers将其级联到视图中来间接进行通信。

In this case you're probably better off having your app delegate assign the controller holding the web view as a delegate of your other view controller on application launch - look into using a protocol to make it clean, but for something simple you can always just declare the delegate type as your web view holding view controller. 在这种情况下,最好将应用程序委托分配给持有Web视图的控制器作为应用程序启动时其他视图控制器的委托-考虑使用协议使其变得干净,但对于简单的事情,您总是可以声明委托类型为您的Web视图保存视图控制器。

Notifications are also a fine idea, and there is no task too small for them - but you should be aware that it's important to unsubscribe from notifications in places like dealloc. 通知也是一个好主意,对于他们来说,任务不算太小-但您应该意识到,取消订阅通知(在诸如dealloc之类的地方)很重要。 For the top level of a tab bar view controller that does not really matter though. 对于标签栏的顶层视图控制器来说,这并不重要。

You can always set up an instance variable in your tab view controller that points to the web view. 您始终可以在选项卡视图控制器中设置一个指向Web视图的实例变量。 Just set it up as an IBOutlet, hook it up in Interface Builder and then call your method on the instance variable. 只需将其设置为IBOutlet,将其连接到Interface Builder中,然后在实例变量上调用您的方法。

.h file: .h文件:

IBOutlet UIWebView *webView;

@property (nonatomic, retain) UIWebView *webView;

.m file: .m文件:

@synthesize webView;

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

Regards, 问候,

Kyle 凯尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从一个视图调用onclick方法到另一个iphoneprogess集线器iphone sdk - Call an onclick method from one view to another iphoneprogess hub iphone sdk 无法从Objective C,iPhone SDK中的另一个视图控制器调用方法 - Can not call method from another view controller in Objective C, iPhone SDK 如何在所有视图控制器中调用一个方法? - How can I call the one method in all the view controllers? 如何在iPhone中将数组从一个视图传递到另一个视图 - How to pass array form one view to another view in iphone 如何在 iphone sdk 中从一个视图翻转到另一个视图 - how to flip from one view to another view in iphone sdk 如何在Xcode 4.2中将一个视图控制器调用到另一个视图控制器 - how to call one view controller to another view controller in Xcode 4.2 如何在父视图中调用方法? (iPhone / Objective-C) - How can I call a method in parent view? (iPhone/ Objective-C) 如何将一个视图的值传递给另一个视图? (苹果手机) - How to pass a value from one view to another? (iPhone) 如何从一个视图导航到另一个视图而无需导航? 在iPhone中 - how to navigate from one view to another without navigation? in iphone iPhone-在标签栏中切换视图控制器并调用方法 - iPhone - switching a view controller in the tabbar and call a method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM