简体   繁体   English

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

[英]How to make my method run when the view re-load

I want to create a login button. 我想创建一个登录按钮。 When I click it the facebook launches, I accept my application, the Facebook application closes and my application reloads. 当我单击它时,facebook启动,我接受我的应用程序,facebook应用程序关闭,我的应用程序重新加载。 My problem is that the second time my application starts, after accepting on Facebook, I cannot find where to disable the login button. 我的问题是我的应用程序第二次启动,在Facebook上接受后,我找不到在何处禁用登录按钮。

I wrote this code: 我写了这段代码:

-(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");
    }

}

The viewWillAppear works the first time, but when I give permissions in the Facebook application, my application restarts but not viewWillAppear method! viewWillAppear首次运行,但是当我在Facebook应用程序中授予权限时,我的应用程序将重新启动,但无法执行viewWillAppear方法!

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM