ios - 以编程方式更改 UIButton 文本并打开一个 url

标签 ios swift uibutton

我有一个 UIButton 作为 Action 。我想最终为它设置一个动态 url 和一个动态文本(在 View 加载时,而不是在单击时)。

@IBAction func openUrl(sender: UIButton) {
    sender.setTitle("Hello World", forState: .Normal)
    UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!)
}

上面的代码可以正常工作,但是按钮标题只有在单击后才会变为“Hello World”。

enter image description here

最佳答案

覆盖 viewDidLoad 并在那里设置标题。另外,如果您还没有创建按钮的导出:

override func viewDidLoad() {
    super.viewDidLoad()

    yourButtonOutlet.setTitle("Hello World", forState: .Normal)
}

从 IBAction 中删除 setTitle() 行。

这将在为 Controller 加载 View 时设置按钮的标题。

关于ios - 以编程方式更改 UIButton 文本并打开一个 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35354129/

相关文章:

ios - SpriteKit : didBeginContact being called non-stop on iPad

ios - 有没有办法将 MicroQR 条形码添加为 AVMetadataObject.ObjectType?

ios - 通过 Interface Builder 创建的 UIView 在 vi​​ewDidLoad 中没有 subview

ios - didSelectRowAtIndexPath 未调用到 cellForRowAtIndex 的 UIButton

ios - UIButton 按下,另一个不按下

ios - 如何从 MeasurementFormatter 结果中获取本地化的单位符号?

ios - dSYM 未通过 CLI 命令上传

ios - 在没有 Xcode 的情况下,在 Flutter 中将 GoogleService-Info.plist 保存在哪里?

swift - 如何在 MVVM 中使用 RxSwift 正确处理来自 api 请求的错误?

ios - 如何在ios9(Ipad)中隐藏键盘