从另一个视图控制器调用一个视图控制器的方法
方法“someMethod”,
@interface OneViewController
{
UIView *tempView;
..
}
-(void) someMethod ;
@end
我有一个在 OneViewController.h 中声明并在 OneViewController.m 文件中实现的
@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];
}
当出现在不同的 viewController - secondaryViewController 时,我想调用 someMethod
(类似于 [OneViewController someMethod]),这样当我返回 OneViewController 时,我可以看到 someMethod 所做的更改。
我需要使用 appDelegate 方法吗?
我已经尝试遵循但它不起作用。
neViewController *newViewController = [[OneViewController alloc] init];
[newViewController someMethod];
感谢您提前提供任何帮助..
I have a method "someMethod" declared in OneViewController.h
@interface OneViewController
{
UIView *tempView;
..
}
-(void) someMethod ;
@end
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];
}
I want to call someMethod when present at different viewController - secondViewController
(something like [OneViewController someMethod]
), So that when I get back to OneViewController I can see the changes made by someMethod.
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..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 SecondViewController 中,声明 OneViewController 类的引用。您可以分配财产。在移动到 SecondViewController 之前设置引用。现在有了引用,您就可以调用实例方法
[_oneView someMethod]
。编辑:
声明
同时添加分配属性,
在.m文件中综合变量。
从 OneViewController 显示 SecondViewController 时,只需添加以下行。
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]
.Edit:
Declare
Also add the assign property,
Synthesize the variable in .m file.
While showing the SecondViewController from OneViewController, just add the following line.
有时直接调用创建
[classObject methodName]
的方法不会反映视图中的更改。就像如果您想将UIScrollView
属性从scrollEnble = NO;
更改为scrollEnable = YES;
一样,它不会反映。您应该使用
UIApplication
的单例。假设您想在
ViewController2
中调用ViewController1
的方法- (void)myMethod
那么步骤如下:ViewController1
并创建其对象*vc
。 Delare 属性@property(强、非原子)ViewController1 *vc;
。也合成
。现在来到
Viewcontroller1
类。在Viewcontroller1
中导入AppDelegate.h
并在viewDidLoad
中编写如下:AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.vc1 = self;
ViewController2.h
并导入AppDelegate.h
转到您要调用的行
ViewController2
的方法并这样写:AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate vc1] myMethod]; // 允许滚动
sometimes calling a method directlry creating
[classObject methodName]
does not refelect the changes in views. Like if you want to change aUIScrollView
property fromscrollEnble = NO;
toscrollEnable = YES;
, it does not refelect.You should use singleton of
UIApplication
.Suppose you want to call
ViewController1'
s method- (void)myMethod
inViewController2
then here are the steps:ViewController1
and create its object*vc
. Delare property@property (strong, nonatomic) ViewController1 *vc;
.synthesize
also.Now come to
Viewcontroller1
class. ImportAppDelegate.h
in youViewcontroller1
's and inviewDidLoad
write like this:AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.vc1 = self;
ViewController2.h
and importAppDelegate.h
Go to the line where you want to call
ViewController2
's method and write like this:AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate vc1] myMethod]; // to allow scrolling
理想情况下,您应该创建一个协议并委托方法来完成您正在寻找的任务。
创建一个协议,在 secondaryViewController 中实现它,并将协议委托设置为firstViewController,然后使用委托方法调用 secondaryViewController 中的相关方法
我希望它对你有用..!!
Ideally, you should create a protocol and delegate methods to accomplish what you are looking for.
Create a protocol, implement it in secondViewController and set the protocol delegate to firstViewController and then use the delegate methods for invoking the relevant methods in secondViewController
I hope it works for you..!!
一种方法是在 OneViewController.h 中将声明更改为
+(void) someMethod ;
,并在实现文件中相应地将减号更改为加号。这将使其成为类方法而不是实例方法。然后在 SecondViewController.m 文件中,确保将@class OneViewController;
放在实现声明之前;然后你可以调用 [OneViewController someMethod] 它应该执行。干杯!One way of doing it is to change the declaration to
+(void) someMethod ;
in your OneViewController.h and change the minus to a plus correspondingly in the implementation file. This will make it a class-method and not an instance method. Then in your SecondViewController.m file, make sure to put@class OneViewController;
before the implementation declaration; then you can call [OneViewController someMethod] and it should execute. Cheers!