gpt4 book ai didi

ios - 使用 Storyboard 的 Twitter iOS 登录按钮?

转载 作者:行者123 更新时间:2023-11-29 01:05:51 25 4
gpt4 key购买 nike

我使用 Fabric 将 TwitterKit 导入到我的 Swift XCode 项目中。我想使用 UIView 创建 Twitter 登录按钮,以便我可以将其放置在 Storyboard 中。

这是我所做的:

1) 我在 Storyboard 中创建了一个 UIView 并将类设置为 TWTRLogInButtonTWTRLogInButton Class

2)在我的 View Controller 中,我创建了一个 IBOutlet。

@IBOutlet var twitterLoginView: TWTRLogInButton!

3) 我修改了 Fabric 的示例代码以适合我的设置。以下是我的 viewDidLoad() 中来自 Fabric 的原始代码:

        let logInButton = TWTRLogInButton { (session, error) in
if let unwrappedSession = session {
let alert = UIAlertController(title: "Logged In",
message: "User \(unwrappedSession.userName) has logged in",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
NSLog("Login error: %@", error!.localizedDescription);
}
}

// TODO: Change where the log in button is positioned in your view
logInButton.center = self.view.center
self.view.addSubview(logInButton)

这是我编辑的代码来引用我的 View :

twitterLoginView = TWTRLogInButton { (session, error) in
if let unwrappedSession = session {
let alert = UIAlertController(title: "Logged In",
message: "User \(unwrappedSession.userName) has logged in",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
print("Login Error, \(error?.localizedDescription)")
}
}

XCode 接受这一点,但是当我启动应用程序时,当我选择 Twitter 登录按钮时,我在日志中收到以下错误:

TWTRLogInButton was created with no completionBlock set

我不确定为什么会发生这种情况,有人有什么想法吗?

最佳答案

buttonWithLogInCompletion 是类方法。您正在 Storyboard中创建 IBOutlet,而不调用实例方法。覆盖它会发出该警告。顺便说一句,你的代码可以工作。如果您不想看到该警告,您应该按照您在问题中所写的方式在代码中创建按钮:

    let logInButton = TWTRLogInButton { (session, error) in
if let unwrappedSession = session {
let alert = UIAlertController(title: "Logged In",
message: "User \(unwrappedSession.userName) has logged in",
preferredStyle: UIAlertControllerStyle.Alert
)
alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
self.presentViewController(alert, animated: true, completion: nil)
} else {
NSLog("Login error: %@", error!.localizedDescription);
}
}

// TODO: Change where the log in button is positioned in your view
logInButton.center = self.view.center
self.view.addSubview(logInButton)

您也可以在代码中使用自动布局设置按钮位置。

关于ios - 使用 Storyboard 的 Twitter iOS 登录按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36377820/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com