从另一个视图控制器调用One View控制器的方法 [英] Calling a method of One View controller from Another View Controller

查看:147
本文介绍了从另一个视图控制器调用One View控制器的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在OneViewController.h中声明了一个方法someMethod

I have a method "someMethod" declared in OneViewController.h

@interface OneViewController
{
UIView *tempView;
..

}
-(void) someMethod ;
@end

并在OneViewController.m文件中实现

and implemented in OneViewController.m file

@implementation OneViewController

-(void) someMethod 
{
tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 200, 250)];
tempView.backgroundColor = [UIColor yellowColor];
if([[self.view subviews] containsObject:tempView])
[tempView removeFromSuperView];
   else
   [self.view addsubview:tempView];

}

我想打电话给 someMethod 当出现在不同的viewController - secondViewController

I want to call someMethod when present at different viewController - secondViewController

(类似 [OneViewController someMethod] ),这样当我回来到OneViewController我可以看到 someMethod 所做的更改。

(something like [OneViewController someMethod]), So that when I get back to OneViewController I can see the changes made by someMethod.

我是否需要使用appDelegate方法?

Do I need to use appDelegate methods?

我试过以下但是它不起作用。

I have tried following but it doesn't work.

neViewController *newViewController = [[OneViewController alloc] init];
[newViewController someMethod];

感谢您提前获得任何帮助..

Thanks for any help in advance..

推荐答案

在SecondViewController中,声明OneViewController类的引用。您可以指定属性。在移动到SecondViewController之前设置引用。现在有了引用,你可以调用实例方法 [_ oneView someMethod]

In the SecondViewController, declare a reference for OneViewController class. You can have assign property. Set the reference before you move to SecondViewController. Now with the reference, you can call the instance method [_oneView someMethod].

编辑:

声明

OneViewController *_oneView;

同时添加assign属性,

Also add the assign property,

@property(nonatomic,assign) OneViewController *_oneView;

在.m文件中合成变量。

Synthesize the variable in .m file.

从OneViewController显示SecondViewController时,只需添加以下行。

While showing the SecondViewController from OneViewController, just add the following line.

secondView._oneView = self;

这篇关于从另一个视图控制器调用One View控制器的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆