从另一个视图控制器调用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;