iOS:Google+按钮无法在XIB文件中使用?

时间:2014-07-01 13:08:53

标签: ios iphone objective-c

昨天我问过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

我想知道我在做什么错,有人可以发现吗?

由于

2 个答案:

答案 0 :(得分:1)

这是一种不好的方法。如果仔细查看输出,蓝色按钮仍保留在后台。这是因为,您已在UIButton / storyboard中添加了xib,并且已将其与ViewController.h文件相关联。

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

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

是的,我今天遇到了同样的问题,这就是我解决它的方法。

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

viewDidLoad

ViewController.m中添加此内容
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];

答案 1 :(得分:0)

成为iOS的新用户,我不确定这是否是正确的做法,但是我为我工作的以下更改

- (void)viewDidLoad {
    [super viewDidLoad];

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

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

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

}

它显示为
enter image description here