OGeek|极客世界-中国程序员成长平台

标题: iOS:Google+ 按钮在 XIB 文件中不起作用? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 23:34
标题: iOS:Google+ 按钮在 XIB 文件中不起作用?

我问 iOS: Sign In with Google button昨天还在挣扎。我得到的答案帮助我弄清楚 GooglePlus.bundle 导入不正确。

现在我被困在我通过 XIB 文件创建的按钮没有显示在页面上的部分。

我做了什么?
我根据这个 gist 添加了一个新按钮,并验证是否一切正常。代码看起来像

- (void)viewDidLoad {
    [super viewDidLoad];

    GPPSignInButton *button = [[GPPSignInButton alloc] init];
    [button setStyle:kGPPSignInButtonStyleWide];
    [self.view addSubview:button];

    GPPSignIn *signIn = [GPPSignIn sharedInstance];
    signIn.shouldFetchGooglePlusUser = YES;
    signIn.shouldFetchGoogleUserEmail = YES;


    [self.signInButton setStyle:kGPPSignInButtonStyleWide];


    signIn.clientID = kClientId;
    signIn.scopes = @[@"profile"];
    signIn.delegate = self;
//    [signIn trySilentAuthentication];

}

当我运行它时,我看到了
enter image description here

所以手动添加的按钮有效,但不是我使用 xib 创建的按钮。我的 xib 看起来像
enter image description here

GooglePlusLoginViewController.h 中的代码看起来像

#import <UIKit/UIKit.h>
#import <GooglePlus/GooglePlus.h>

@class GPPSignInButton;

@interface GooglePlusLoginViewController : UIViewController <GPPSignInDelegate>
@property(weak, nonatomic) IBOutlet GPPSignInButton *signInButton;
@end

我想知道我做错了什么,有人能发现吗?

谢谢



Best Answer-推荐答案


这是一个不好的方法。如果您仔细查看输出,蓝色按钮仍保留在背景中。这是因为,您在 storyboard/xib 中添加了 UIButton,并且已将它们连接到 ViewController.h 文件。

然后您将创建 GPPSignInButton 的新实例并将其添加回您之前创建的 UIButton

您不应该在按钮内创建按钮。

顺便说一句,我今天遇到了同样的问题,这是我的解决方法。

我以编程方式创建了 GPPSignInButton,并以编程方式将其定位在 view 中。

ViewController.m

viewDidLoad 中添加这个
GPPSignInButton *button = [[GPPSignInButton alloc] init];
[button setStyle:kGPPSignInButtonStyleWide];

CGRect frame = button.frame;
frame.origin.x = 50;
frame.origin.y = 190;
button.frame = frame;

[self.view addSubview:button];

关于iOS:Google+ 按钮在 XIB 文件中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24511134/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4