Swift无法将自定义按钮关联到GIDSignInButton属性求助
Hey there! I’ve run into this exact issue a few times when setting up Google Sign-In with Firebase in Swift—let’s break down the most common fixes to get your button linked properly.
First, confirm you’ve imported the GoogleSignIn framework correctly
At the top of your view controller file, make sure you have:import GoogleSignInAlso, double-check your dependency setup:
- If using CocoaPods: Ensure you ran
pod install, closed Xcode, and reopened the .xcworkspace file (not the .xcodeproj). Sometimes Xcode doesn’t pick up framework changes until you reload the workspace. - If using Swift Package Manager: Verify the GoogleSignIn package is added to your project’s dependencies and that it resolved successfully (check the Package Dependencies tab in your project navigator).
- If using CocoaPods: Ensure you ran
Make sure your IBOutlet is declared in the right place
Your@IBOutletneeds to live inside your view controller class, not outside of it. Here’s the correct structure:import UIKit import GoogleSignIn class YourViewController: UIViewController { // Correct placement: inside the class @IBOutlet weak var signInButton: GIDSignInButton! override func viewDidLoad() { super.viewDidLoad() // Your Firebase/Google Sign-In setup code here } }If the outlet is declared outside the class, Xcode won’t associate it with your view controller, which is why it prompts you to create a new one.
Set the Storyboard button’s custom class to
GIDSignInButton
This is the most common culprit! If your button is still set to the defaultUIButtonin Storyboard, Xcode won’t recognize it as aGIDSignInButtonto link to your outlet:- Select your button in Storyboard.
- Open the Identity Inspector (third tab in the right sidebar, icon is a circle with an "i").
- In the Custom Class section, type
GIDSignInButtoninto the Class field. The Module field should auto-fill withGoogleSignIn—if not, select it from the dropdown. - Save your Storyboard, then try control-dragging again.
Clear Xcode’s cache and restart
Xcode’s cache can cause weird interface glitches. Try these steps:- Press
Cmd + Shift + Kto clean your project. - Press
Cmd + Option + Shift + Kto clean the build folder. - Quit Xcode entirely, then reopen your project/workspace.
- Press
Check for typos in your outlet name
Xcode is case-sensitive! Make sure the namesignInButtonmatches exactly between your code and what you’re trying to link. A typo likeSignInButton(capital S) orsigninButton(lowercase "i") will prevent Xcode from finding the existing outlet.
If none of these steps work, try removing and re-adding the GoogleSignIn framework to your project, or double-check that you’ve followed all Firebase setup steps (like adding the GoogleService-Info.plist file correctly).
内容的提问来源于stack exchange,提问作者Yoann Picquenot

