简体   繁体   English

在iOS中关闭ViewController

[英]dismiss viewcontroller in ios

this is the code for my facebook login, when i finish the login the FaceBookLogIn.xib don't dismiss properly, 这是我的Facebook登录名的代码,当我完成登录时,FaceBookLogIn.xib无法正确关闭,

when i exit the app and relaunch it work fine and my main view appear. 当我退出应用程序并重新启动时,它工作正常,并且出现主视图。

how can i dismiss the viewcontroller as soon as the user singin ? 用户一发出声音,我怎么能关闭视图控制器?

update 更新

i move all the code from app AppDelegate to my main view controller 我将所有代码从应用程序AppDelegate移至主视图控制器

and when i sing in i get in the nslog 当我唱歌时,我进入了nslog

"2013-12-07 13:02:49.650 barbar[2275:70b] User session found" “ 2013-12-07 13:02:49.650 barbar [2275:70b]找到用户会话”

bat still the view don't dismiss 蝙蝠仍然认为不消除

if i exit and return to the app it show the tabbarviewcontroler and i get my nslog - 如果我退出并返回到应用程序,它将显示tabbarviewcontroler,并且我得到我的nslog-

2013-12-07 13:06:42.162 barbar[2303:70b] User Logged Innnnn 2013-12-07 13:06:42.162 barbar [2303:70b]用户登录了Innnnn

   - (void)viewDidLoad
{
    [super viewDidLoad];

}

-(void)viewDidAppear:(BOOL)animated{


    if (FBSession.activeSession.state
        == FBSessionStateCreatedTokenLoaded) {

        NSLog(@"User Logged Innnnn");
        //[self dismissViewControllerAnimated:YES completion:Nil];
    }
    //create and present the login view controller
    else {
        NSLog(@"the user not login");
        FacebookConect * showfacebookconnectview = [[FacebookConect alloc]initWithNibName:@"FacebookConect" bundle:Nil];
        [self presentViewController:showfacebookconnectview animated:YES completion:Nil];


    }
    animated = YES;
}

- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"User session found");
                [self dismissViewControllerAnimated:YES completion:Nil];
            }
            break;
        case FBSessionStateClosed:
        case FBSessionStateClosedLoginFailed:
            [FBSession.activeSession closeAndClearTokenInformation];
            break;
        default:
            break;
    }

    [[NSNotificationCenter defaultCenter]
     postNotificationName:FBSessionStateChangedNotification
     object:session];

    if (error) {
        UIAlertView *alertView = [[UIAlertView alloc]
                                  initWithTitle:@"Error"
                                  message:error.localizedDescription
                                  delegate:nil
                                  cancelButtonTitle:@"OK"
                                  otherButtonTitles:nil];
        [alertView show];
    }
}

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {
    /*
     we pass this permissions array into our request
     I only request email, but there are many more options
     */
    NSArray *permissions = [[NSArray alloc] initWithObjects:
                            @"email",
                            nil];

    return [FBSession
            openActiveSessionWithReadPermissions:permissions
            allowLoginUI:allowLoginUI
            completionHandler:^(FBSession *session,
                                FBSessionState state,
                                NSError *error) {
                [self sessionStateChanged:session
                                    state:state
                                    error:error];

            }];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {
    // attempt to extract a token from the url
    return [FBSession.activeSession handleOpenURL:url];
}



@end

I wouldn't recommend using the AppDelegate to show login screens directly. 我不建议使用AppDelegate直接显示登录屏幕。 It looks as though you are using Storyboards/Xib's to show your interface. 好像您正在使用Storyboards / Xib来显示您的界面。 For this reason, I'd allow the default operation to happen (because you're preventing it by setting the rootViewController property manually). 因此,我允许进行默认操作(因为通过手动设置rootViewController属性可以防止此操作)。 Then, in your main view controller, have the check for Facebook that would bring up the dialog ( presentViewController:animated:completion: ) and can then be dismissed as above and your original main view controller will be shown. 然后,在您的主视图控制器中,检查是否有Facebook弹出对话框( presentViewController:animated:completion: ,然后可以如上所述将其关闭,并显示原始的主视图控制器。

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

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