M Swapnil
M Swapnil

Reputation: 2381

Why my ViewDidLoad Loads every time?

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.

Upvotes: 2

Views: 524

Answers (2)

M Swapnil
M Swapnil

Reputation: 2381

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.

Upvotes: 0

Lord Zsolt
Lord Zsolt

Reputation: 6557

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

Upvotes: 2

Related Questions