xcode iphone开发中调用父函数

发布于 2024-09-25 23:52:23 字数 453 浏览 0 评论 0原文

我们有 2 个文件“MainViewController”和“View01”
第一个文件 addSubView 第二个文件。

在第一个文件中,我们有一个名为 displayView 的函数。

我希望能够从 View01 文件调用此函数,以下代码是

View01 文件的正确部分

#import "MainViewController.h"

- (IBAction) changeView:id(sender){
      MainViewControlle *appDelegate = [[UIApplication sharedApplication] delegate];
      [appDelegate displayView:2];
}

,这是正确的方法吗?

目前我使用 NSNotificationCenter 来发送激活功能:)

We have 2 files 'MainViewController' and 'View01'
The first file addSubView the second one.

In the first file we have a function named displayView.

I want to be able to call this function from the View01 file is the following code correct

part of View01 file

#import "MainViewController.h"

- (IBAction) changeView:id(sender){
      MainViewControlle *appDelegate = [[UIApplication sharedApplication] delegate];
      [appDelegate displayView:2];
}

is this the correct way to do it?

at the moment I use the NSNotificationCenter to send activate the functions :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只为一人 2024-10-02 23:52:23

您应该尝试使用委托方法:-)

它的工作原理如下:

// Delegate
@protocol MyViewDelegate <NSObject>
- (void)myViewAskedForSomethingWithOrWithoutParameters;

@end

在您的视图中,您必须具有此参数:

id<MyViewDelegate>  delegate;

然后在您的控制器中,您必须实现委托

@interface MainViewController : UIViewController <MyViewDelegate> {
}

在您的实现中,您需要添加一个:

myView.delegate = self;

最后,当您需要时,在您的视图中要调用该函数,只需执行以下操作:

[ delegate myViewAskedForSomethingWithOrWithoutParameters ];

祝你好运!

You should try using delegate methods :-)

It works like that :

// Delegate
@protocol MyViewDelegate <NSObject>
- (void)myViewAskedForSomethingWithOrWithoutParameters;

@end

In your view you must have this parameter :

id<MyViewDelegate>  delegate;

And then in your Controller you must implement the delegate

@interface MainViewController : UIViewController <MyViewDelegate> {
}

In your implementation you need to add a :

myView.delegate = self;

And finally in your view when you need to call the function, just do :

[ delegate myViewAskedForSomethingWithOrWithoutParameters ];

Good Luck !

少钕鈤記 2024-10-02 23:52:23

如果我没记错的话,你的代码不应该工作。你自己尝试过吗?

[[UIApplication sharedApplication].delegate]

将返回您的 AppDelegate,其名称类似于 MyAppDelegate 而不是 MainViewController。但是,根据您使用或创建的模板,您的 AppDelegate 可能包含 MainViewController 属性,最有可能称为 viewController,因此您可以使用

 [appDelegate.viewController displayView:2];

这是执行此操作的快速方法。对于更简洁的方式,请参阅 Vinzius 的回答。

If I'm not mistaken, your code shouldn't work. Did you try it your self?

[[UIApplication sharedApplication].delegate]

will return your AppDelegate, called something like MyAppDelegate and not MainViewController. However, depending on the template you used or created, your AppDelegate might contain a MainViewController-property, most likely called viewController so you could use

 [appDelegate.viewController displayView:2];

This is the quick way to do it. For a more tidy way, see Vinzius' answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文