2

I am Creating a App in which I implemented FB login. So i write a FBLogin button code in my ViewDidLoad in ViewController.m file.

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

FBSDKLoginButton *loginButton = [[FBSDKLoginButton alloc] init];
loginButton.center = self.view.center;
[self.view addSubview:loginButton];

}

but when i ran this code. fb buttons appears in all pages of my app, it means my viewController.m ViewDidLoad method call every time.Why? I am looking for solution last 4 hours but didnt get any result yet. please help me out.

M Swapnil
  • 2,361
  • 3
  • 18
  • 33
  • Do your view controllers inherit from this view controller? Also, without seeing more code, we can only take blind shots at it. – Lord Zsolt May 08 '15 at 07:56
  • The view appearing in multiple pages is an indication of reuse of this class, not `viewDidLoad` being called multiple times – saurabh May 08 '15 at 08:00
  • @Lord Zsolt- yes. my view controllers inherit from this view controller. – M Swapnil May 08 '15 at 08:05
  • instantiating view controller only results calling viewdidLoad..so it means your current ViewController.m is instantiating somewhere..so figure out that – LC 웃 May 08 '15 at 08:09
  • @anish - all my classes are inherit with this view controller. like interface contestViewController : ViewController. Can this be a reason for my error? – M Swapnil May 08 '15 at 08:12
  • yeah..thats the point..so figureout the solution that helps instanitating view controller only once..one example is here http://stackoverflow.com/questions/29699001/how-to-make-viewdidload-method-of-viewcontroller-residing-inside-containerview-c – LC 웃 May 08 '15 at 08:15
  • @anish- thnx, I will post if i find any solution. thnx for your suggestions. – M Swapnil May 08 '15 at 08:16
  • yeah..if you cant, try posting your project...we will give a try – LC 웃 May 08 '15 at 08:17
  • never ask for +1 on SO..try to get it auto....but i will do it for you – LC 웃 May 08 '15 at 08:21

2 Answers2

2

As you've said, your view controllers inherit from this view controller, thus every view controller who calls [super viewDidLoad] will execute this method. (The solution isn't the removal of the super call).

Instead of creating this ViewController and every other view controller inherit from this one you could:

  • Create a CommonViewController which implements everything that is common between your view controllers
  • Create a FacebookLoginViewController which also inherit from CommonViewController.

Thus, your inheritance tree would look like:

CommonViewController
      /          \
     /            \
    /              \
FacebookLoginVC    OtherViewControllers

Instead of:

ViewController (which is equivalent to FacebookViewController)
     |
     |
     |
OtherViewControllers
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
0

Here is the Answer for my question

I made a small mistake when i inherit all Other ViewControllers and main ViewController class with ViewController. i modified my code with

 @interface ViewController : UIViewController


 @end

instead of

 @interface ViewController : ViewController

 @end

and all other classes too like

   @interface homeViewController : UIViewController

   @interface contestViewController : UIViewController


   @interface menuViewController : UIViewController 

etc.

This simply solved my question. thanks for the help guys.

M Swapnil
  • 2,361
  • 3
  • 18
  • 33