gpt4 book ai didi

ios - 将变量放入 UIAlertView - Swift

转载 作者:行者123 更新时间:2023-11-28 10:02:08 25 4
gpt4 key购买 nike

我创建了一个 UIAlertView 实例并将其命名为 alert,我制作了标题和消息,现在我在 addButtonWithTitle 上。我试过这样做“唤醒时间(strDate)”,我也尝试过“唤醒时间”+ strDate。这是 UIAlertView 实例的代码:

let alert = UIAlertView()
alert.title = "WAKE UP"
alert.message = "Time to wake up it's"
alert.addButtonWithTitle("I'm Up!")
alert.show()

在第三行它应该说 alert.message = "Time to wake it's strDate"

strDate 应该是一个变量

这是我的strDate

var strDate = timeFormatter.stringFromDate(theDatePicker.date)

最佳答案

其他答案是正确的,但您应该使用此代码:

let alert = UIAlertController(title: "WAKE UP", message:nil, preferredStyle: UIAlertControllerStyle.Alert);
var strDate = "Ok";
alert.message = "Time to wake up " + strDate;


let dismissHandler = {
(action: UIAlertAction!) in
self.dismissViewControllerAnimated(true, completion: { () -> Void in
})
}

alert.addAction(UIAlertAction(title: "I'm Up!", style: .Default, handler: dismissHandler))
presentViewController(alert, animated: true) { () -> Void in

}

根据 Swift 文档,您应该使用 + 运算符在字符串中添加内容。

The addition operator is also supported for String concatenation:

"hello, " + "world" // equals "hello, world"

来自 Swift Programming Language - Basic Operators

关于ios - 将变量放入 UIAlertView - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26772443/

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