Calling a method of One View controller from Anoth

2019-04-10 01:04发布

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

4条回答
Root(大扎)
2楼-- · 2019-04-10 01:27

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

查看更多
姐就是有狂的资本
3楼-- · 2019-04-10 01:29

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

查看更多
Deceive 欺骗
4楼-- · 2019-04-10 01:33

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;
查看更多
在下西门庆
5楼-- · 2019-04-10 01:49

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!

查看更多
登录 后发表回答