0

I am working on adding facebook SSO and the SDK to my project. All of my main facebook code is in my AppDelegate.m and AppDelegate.h. In my view controller [[[UIApplication sharedApplication] delegate] performSelector:@selector(callLogin)]; is called when I press a button. The callLogin method in my AppDelegate.m looks like this:

- (void)callLogin{

facebook = [[Facebook alloc] initWithAppId:@"XXXXX" andDelegate:self];

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:@"FBAccessTokenKey"] 
    && [defaults objectForKey:@"FBExpirationDateKey"]) {
    facebook.accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
    facebook.expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
}

if (![facebook isSessionValid]) {
    [facebook authorize:nil];
    }
}

Then in my - (void)fbDidLogin method I call the method setInfo which is located in my viewcontroller.

//this method is located in AppDelegate.m
- (void)fbDidLogin {
NSLog(@"FACEBOOK DID LOGIN");
ViewController * vc = [[ViewController alloc]init];
[vc setInfo]; 
}

Finally, here is my -(void)setInfo code which is located in ViewController.m

-(void)setInfo{
infoL.text = [NSString stringWithFormat: @"Connected to Facebook!"];
NSLog(@"%@",infoL);
//NSLog returns null
} 

From setInfo I am unable to change the label and NSLog returns that infoL is null. I can update the label through methods like ViewDidLoad, but not setInfo. What am I doing wrong and how can I fix this?

4

2 回答 2

1

The view controller won't get updated because you just created some random instance of the class that is not on your navigation stack. I suppose you could call

 [ self.viewController setInfo ]

assuming that is the property name for the vc you pushed on the stack in applicationDidFinishLaunchingWithOptions although this wouldn't be considered great design. Id factor Facebook delegate stuff out out app delegate, create a Facebook controller singleton class that your view controller can feed off

于 2012-06-08T15:32:44.697 回答
0

我假设 infoL 代表一个 UILabel。您是在代码中创建此标签还是自动创建的插座?

如果您在代码中创建 infoL,它在哪里分配/初始化?在您的 setInfo 方法运行之前的任何时候它都不是 nil 吗?

于 2012-06-07T16:58:07.943 回答