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