Calling a method of One View controller from Another View Controller

怎甘沉沦 提交于 2019-12-21 17:29:52

问题


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..


回答1:


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

OneViewController *_oneView;

Also add the assign property,

@property(nonatomic,assign) OneViewController *_oneView;

Synthesize the variable in .m file.

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

secondView._oneView = self;



回答2:


sometimes calling a method directlry creating [classObject methodName] does not refelect the changes in views. Like if you want to change a UIScrollView property from scrollEnble = NO; to scrollEnable = YES;, it does not refelect. You should use singleton of UIApplication.

Suppose you want to call ViewController1's method - (void)myMethod in ViewController2 then here are the steps:

  • In you AppDelegate import ViewController1 and create its object *vc. Delare property @property (strong, nonatomic) ViewController1 *vc;. synthesize also.
  • Now come to Viewcontroller1 class. Import AppDelegate.h in you Viewcontroller1's and in viewDidLoad write like this:

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    appDelegate.vc1 = self;

  • Go to your ViewController2.h and import AppDelegate.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




回答3:


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..!!




回答4:


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!



来源:https://stackoverflow.com/questions/7873834/calling-a-method-of-one-view-controller-from-another-view-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!