当我们在 iOS 中使用 Twitter 登录时,如何将一个视图控制器移动到另一个视图控制器
Posted
技术标签:
【中文标题】当我们在 iOS 中使用 Twitter 登录时,如何将一个视图控制器移动到另一个视图控制器【英文标题】:How to move one view controller to another view controller when we login with Twitter in iOS 【发布时间】:2015-08-28 07:55:04 【问题描述】:我是 ios 的初学者,在我的项目中,我为用户提供了 Twitter 登录功能,当我们点击登录按钮时,我们会看到 Twitter 登录页面,如下面的屏幕所示。
我们登录后,用户我点击 Twitter 登录页面中的AuthorizedApp
按钮。
然后我想为此移动另一个视图控制器(即 ViewController1),我已经编写了一些代码,但是使用该代码我无法将一个视图控制器移动到另一个视图控制器。
注意:我的主要意图是当我点击“AuthorizedApp”按钮时,我想将 ViewController 移动到 ViewController1
我的代码:
#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】:您应该在视图控制器之间建立一个转场连接,并为转场提供一个标识符。之后,您可以将 performSegueWithIdentifier 方法与您的 segue 标识符名称一起使用。在你的情况下;
- (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);
// These codes work on background thread you should move them to the main thread
/*ViewController1 *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"ViewController1"];
[dvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:dvc animated:YES completion:nil];*/
];
[self performSegueWithIdentifier:@"yourSegueIdentifierYouMustChange" sender:self]; // or sender nil
如果 performSegueWithIdentifier 方法不能解决您的问题,我们可以尝试将您的代码移至主线程。
【讨论】:
不,它不起作用,我的主要要求是当我单击授权应用按钮时,我想显示用户详细信息,然后移动另一个视图控制器以上是关于当我们在 iOS 中使用 Twitter 登录时,如何将一个视图控制器移动到另一个视图控制器的主要内容,如果未能解决你的问题,请参考以下文章
当Apple从设置中删除时,如何在Xcode 9 Simulator中访问Twitter?
使用 Parse iOS 登录 Facebook 和 twitter 只能使用一次