0

当 Facebook 登录成功时,我需要添加一个带有 ViewControllers 的 tabbarcontroller。但不明白怎么做?

我在 appDelegate.m 中有:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

SearchView *first=[[SearchView alloc]
                                initWithNibName:@"SearchView" bundle:nil];


    Login *second=[[Login alloc]initWithNibName:@"Login" bundle:nil];
    second.title=@"Login";
    NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];
    tabBarController=[[UITabBarController alloc] init];
    [tabBarController setViewControllers:viewArray animated:NO];
    [self.window addSubview:tabBarController.view];
    [self.window makeKeyAndVisible];
    [viewArray release];
    [first release];
    [second release];    
    return YES;


}




}
- (void)sessionStateChanged:(FBSession *)session
                      state:(FBSessionState) state
                      error:(NSError *)error
{
    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                FBLogin *fblogin=[[FBLogin alloc]initWithNibName:@"FBLogin" bundle:nil];
               [self.window addSubview:fblogin.view];
 }
            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];




    }
}

In FBLogin.m :
-(void)viewDidAppear:(BOOL)animated
{

            SearchView *searchViewController=[[SearchView alloc]initWithNibName:@"SearchView" bundle:nil];
            UserProfile *userprofile=[[UserProfile alloc]initWithNibName:@"UserProfile" bundle:nil];
            userprofile.title=@"My Profile";
            LogOut *logout=[[LogOut alloc]initWithNibName:@"LogOut" bundle:nil];
            logout.title=@"Sign Out";
            tab=[[UITabBarController alloc]init];
            tab.viewControllers=[NSArray arrayWithObjects:searchViewController,userprofile,logout, nil];
            [self presentModalViewController:tab animated:NO];


}

但是我看不到 fBLogin 中添加的 tabbarcontroller。我可以看到一个空白的视图。

是这样吗?我怎样才能实现它?

4

2 回答 2

3

只需设置并初始化UITabBarControllerin AppDelegate,当您成功登录时,只需使用我们的自定义方法 For Ex调用UITabBarControllerrootViewController ......window

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
           UIViewController *viewController1 = [[[yourViewController1 alloc] initWithNibName:@"yourViewController1" bundle:nil] autorelease];
            UINavigationController *navviewController1=[[UINavigationController alloc]initWithRootViewController:viewController1];


            UIViewController *viewController2 = [[[yourViewController2 alloc] initWithNibName:@"yourViewController2" bundle:nil] autorelease];
            UINavigationController *navviewController2=[[UINavigationController alloc]initWithRootViewController:viewController2];


            UIViewController *viewController3 = [[[yourViewController3 alloc] initWithNibName:@"yourViewController3" bundle:nil] autorelease];
            UINavigationController *navviewController3=[[UINavigationController alloc]initWithRootViewController:viewController3];


            UIViewController *viewController4 = [[[yourViewController4 alloc] initWithNibName:@"yourViewController4" bundle:nil] autorelease];
            UINavigationController *navviewController4=[[UINavigationController alloc]initWithRootViewController:viewController4];


            UIViewController *viewController5 = [[[yourViewController5 alloc] initWithNibName:@"yourViewController5" bundle:nil] autorelease];
            UINavigationController *navviewController5=[[UINavigationController alloc]initWithRootViewController:viewController5];


            self.tabBarController = [[[UITabBarController alloc] init] autorelease];
            self.tabBarController.viewControllers = [NSArray arrayWithObjects:navviewController1, navviewController2,navviewController3,navviewController4,navviewController5, nil];

        SearchView *first=[[SearchView alloc]
                            initWithNibName:@"SearchView" bundle:nil];


        Login *second=[[Login alloc]initWithNibName:@"Login" bundle:nil];
        second.title=@"Login";
        NSArray *viewArray=[[NSArray alloc] initWithObjects: first,second,nil];
        yourTabBarController=[[UITabBarController alloc] init];
        [yourTabBarController setViewControllers:viewArray animated:NO];
        [self.window addSubview:yourTabBarController.view];

        [self.window makeKeyAndVisible];
        return YES;
    }

当您成功登录时,只需调用下面我们的自定义方法..

-(void)loadTabBarFromDelegate 
{
    self.window.rootViewController = self.tabBarController;
    [self.window makeKeyAndVisible];

    CATransition *animation = [CATransition animation];
    [animation setDelegate:self];   
    [animation setType:kCATransitionFade];
    [animation setDuration:0.5];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:
                                  kCAMediaTimingFunctionEaseInEaseOut]];
    [[self.window layer] addAnimation:animation forKey:@"transitionViewAnimation"];
}

当你想调用这个方法时,只需创建对象并像下面这样调用这个方法......

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate loadTabBarFromDelegate];

我希望这可以帮助你...

于 2012-12-18T10:41:01.167 回答
0

尝试将您的代码viewDidLoadviewDidAppear.

于 2012-12-18T09:50:52.607 回答