当我们使用Twitter登录时,如何将一个视图控制器移动到另一个视图控制器

时间:2015-08-28 10:04:02

标签: ios objective-c

您好我是iOS的初学者,在我的项目中,我为用户添加了Twitter登录工具,点击登录应用程序后,我们获得Twitter登录页面

然后用户点击Twitter登录页面中的“授权应用程序”按钮。 然后我想移动另一个视图控制器(即:主视图控制器到子视图控制器)为此我写了一些代码,但基于该代码我无法将一个主视图控制器移动到子视图控制器

显示异常

警告:尝试显示其视图不在窗口层次结构中!

我的代码:

#import "ViewController.h"
#import "FHSTwitterEngine.h"
#import "ViewController1.h"

@interface ViewController ()<FHSTwitterEngineAccessTokenDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    [[FHSTwitterEngine sharedEngine]permanentlySetConsumerKey:@"yyp5D4Elbo4fBAdnbnOZDaPRt" andSecret:@"WwIfOHvw8j0rIlLPtBQiPZ5r1dl44X4Lh8H6rKY5gLb1m4YM72"];

    [[FHSTwitterEngine sharedEngine]setDelegate:self];

    [[FHSTwitterEngine sharedEngine]loadAccessToken];

    //google plus login button

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];

    [button addTarget:self

               action:@selector(loginOAuth)

     forControlEvents:UIControlEventTouchUpInside];

    [button setImage:[UIImage imageNamed:@"twitter-icon.png"] forState:UIControlStateNormal];

    button.frame = CGRectMake(170, 200, 40, 40);

    CALayer * d = [button layer];

    [d setMasksToBounds:YES];

    [d setCornerRadius:20];

    [self.view addSubview:button];
}

- (void)loginOAuth {

    UIViewController *loginController = [[FHSTwitterEngine sharedEngine]loginControllerWithCompletionHandler:^(BOOL success) {

        NSLog(success?@"L0L success":@"O noes!!! Loggen faylur!!!");

        NSLog(@"User name ---->>>%@",FHSTwitterEngine.sharedEngine.authenticatedUsername);

        NSLog(@"User id ------> %@",FHSTwitterEngine.sharedEngine.authenticatedID);

ViewController1 *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"]; [dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical]; [self presentViewController:dvc animated:YES completion:nil];
    }];
    [self presentViewController:loginController animated:YES completion:nil];
}

1 个答案:

答案 0 :(得分:0)

当用户通过twitter登录时,您可以将根视图控制器设置为ViewController1。

将此方法添加到app delegate

  - (void)loginSuccess {
          ViewController1 *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
          UINavigationController *navigationController=[[UINavigationController alloc] initWithRootViewController: dvc];
            self.window.rootViewController =nil;
            self.window.rootViewController = navigationController;
        }

然后,您将在twitter登录成功时调用loginSuccess方法,如:

     UIViewController *loginController = [[FHSTwitterEngine sharedEngine]loginControllerWithCompletionHandler:^(BOOL success) {
        AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate loginSuccess];
      }];