2

I have a UIViewController in my IPhone application.For some animation i make a view like rootview with in that and added all the elements in that.When clicking on a button i removed that rootview and added another view controllers view.The problem is i was only loaded the view.I want to execute a function with in the new view controller.For that i need to set the first view controller as a delegate of the second view controller?can anybody help me ?

Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
hacker
  • 8,919
  • 12
  • 62
  • 108

2 Answers2

2

in secondVC, define something like the following:

@protocol secondVCDelegate

@interface secondVC : UIViewController
    @property (nonatomic, assign) id<secondVCDelegate> delegate;
@end


@optional
-(void)someDelegateMethod:(secondVC*)viewController;

@end

at the time of creating the instance of secondVC you must assign the delegate property of secondVC to self! something like this:

// in firstVC
secondVC vc = [[secondVC alloc]...];
vc.delegate = self;
[navcontroller pushVC:vc];

the line vc.delegate = self; does the trick.

hope it helps...

Obaid Maroof
  • 1,523
  • 2
  • 19
  • 40
0

Not sure from your typing that you want to call method of which controller from which controller?

Two scenarios:

A. In the first view controller you want to call a method of second view controller, use this:

[instantOfSecondViewController methodInSecondVC];

B. In the second view controller, you want to call a method of the first view controller. In this case you need to use The delegate pattern.

I have posted an example of both in this SO.

Community
  • 1
  • 1
user523234
  • 14,323
  • 10
  • 62
  • 102