0

I have a facebook variable in my ViewController that I want to refer to in my AppDelegate method. How do I do this?

In my AppDelegate I want to put this, since it uses UIApplication and all other refs are in this class:

    // For iOS 4.2+ support
    - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
      sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
        return [facebook handleOpenURL:url]; 
    }

The facebook variable is in my ViewController .h file:

    #import <UIKit/UIKit.h>
    #import "FBConnect.h"

    @interface ImportPicViewController : UIViewController <FBSessionDelegate, FBDialogDelegate>{
        Facebook *facebook;
    }
    @property (nonatomic, retain) Facebook *facebook;
    @end

and synthesized in the .m file:

    @implementation ImportPicViewController
    @synthesize facebook;

But it is alloc init in the ViewController:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        facebook = [[Facebook alloc] initWithAppId:@"224207854355440" andDelegate:self];

Any idea?

2 Answers 2

2

I have a facebook variable in my ViewController that I want to refer to in my AppDelegate method.

That doesn't sound like a good plan -- it'll mean that your app delegate is dependent on this view controller.

A better design might have the app delegate create the instance of Facebook and keep a reference to it in its own ivar. Then, when it creates the view controller, it can pass in a reference to the instance of Facebook. This way, both the app delegate and the view controller are using the same Facebook object, but they're not trying to access each other's instance variables.

Sign up to request clarification or add additional context in comments.

Comments

1

Declare a public method in AppDelegate which accepts a pointer to your ViewController. Then store the received pointer in AppDelegate. Once you need the ViewController, just use the stored pointer.

An uglier way to use 'facebook' would be to place it in a global variable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.