如何在视图重新加载时运行我的方法

时间:2011-11-21 22:20:00

标签: iphone objective-c facebook

我想创建一个登录按钮。当我点击它启动facebook时,我接受我的应用程序,Facebook应用程序关闭,我的应用程序重新加载。我的问题是我的应用程序第二次启动,在Facebook上接受后,我找不到禁用登录按钮的位置。

我写了这段代码:

-(IBAction) login:(id)sender;
{

    AutoAppDelegate *delegate = (AutoAppDelegate *) [[UIApplication sharedApplication] delegate];
    // Check and retrieve authorization information
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        [delegate facebook].accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        [delegate facebook].expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }
    if (![[delegate facebook] isSessionValid]) {
        [delegate facebook].sessionDelegate = self;
        NSArray *permissions = [[NSArray alloc] initWithObjects:
                                @"user_likes", 
                                @"read_stream",
                                nil];
        [[delegate facebook] authorize:permissions];
    } else {
        logIn.hidden=YES;
        lbl.text=@"";
        lbl.text=@"Connected";
        [self showLoggedIn];
    }
}

- (void) showLoggedIn {
    NSLog(@"11 Logged In ");
    lbl.text=@"";
    lbl.text=@"Connected";
}

- (void) showLoggedOut {
    NSLog(@"11 Not Logged In ");
    lbl.text=@"";
    lbl.text=@"Not Connected";
}



- (void) viewWillAppear:(BOOL)animated
{
    //[self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];

    AutoAppDelegate *delegate = (AutoAppDelegate *) [[UIApplication sharedApplication] delegate];
    // Check and retrieve authorization information
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    if ([defaults objectForKey:@"FBAccessTokenKey"] 
        && [defaults objectForKey:@"FBExpirationDateKey"]) {
        [delegate facebook].accessToken = [defaults objectForKey:@"FBAccessTokenKey"];
        [delegate facebook].expirationDate = [defaults objectForKey:@"FBExpirationDateKey"];
    }
    if (![[delegate facebook] isSessionValid]) {
       // [self showLoggedOut];
        NSLog(@"Not Connected");
    } else {
       // [self showLoggedIn];
        NSLog(@"Connected");
    }

}

viewWillAppear第一次运行,但是当我在Facebook应用程序中授予权限时,我的应用程序重新启动,但不是viewWillAppear方法!

2 个答案:

答案 0 :(得分:0)

在Facebook事情完成后,您是否尝试在视图上调用setNeedsDisplay?

答案 1 :(得分:0)

您需要将代码放在应用程序委托(applicationWillEnterForeground)中,因为这是在重新打开应用程序时自动获得通知的唯一方法(您的视图不一定会被重新绘制)。