首页 文章

使用Storyboard的Twitter iOS登录按钮?

提问于
浏览
0

我已经使用Fabric将 TwitterKit 导入到我的Swift XCode项目中 . 我想使用UIView创建Twitter登录按钮,这样我就可以在故事板中进行布局 .

这就是我所做的:

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

2)在我的视图控制器中,我创建了一个IBOutlet .

@IBOutlet var twitterLoginView: TWTRLogInButton!

3)我've modified the sample code from Fabric to suit my setup. Here'是来自我的 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)

这是我编辑的代码,用于引用我的视图:

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创建时没有设置completionBlock

我不确定为什么会这样,有人有任何想法吗?

1 回答

  • 1

    buttonWithLogInCompletion 是类方法 . 您正在故事板中创建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)
    

    您也可以在代码中使用autolayout设置按钮位置 .

相关问题