- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试让按钮根据按钮的文本显示警报。因此,在 View 确实加载时,我从数组中提取了一些随机值:
let ingredient1 = realBases[Int(arc4random_uniform(UInt32(realBases.count)))]
var ingredient2 = juices[Int(arc4random_uniform(UInt32(juices.count)))]
let indexOf2 = juices.index(of: ingredient2)
juices.remove(at: indexOf2!)
if ingredient2 == ingredient1 {
ingredient2 = ""
}
var ingredient3 = juices[Int(arc4random_uniform(UInt32(juices.count)))]
let indexOf3 = juices.index(of: ingredient3)
juices.remove(at: indexOf3!)
if ingredient3 == ingredient1 {
ingredient3 = ""
}
var ingredient4 = juices[Int(arc4random_uniform(UInt32(juices.count)))]
let indexOf4 = juices.index(of: ingredient4)
juices.remove(at: indexOf4!)
if ingredient4 == ingredient1 {
ingredient4 = ""
}
正如您所看到的,设置值后,该元素将从数组中删除以防止重复使用。
然后我为按钮指定这些名称:
btnO1.setTitle(newArray[0], for: UIControlState.normal)
btnO2.setTitle(newArray[1], for: UIControlState.normal)
btnO3.setTitle(newArray[2], for: UIControlState.normal)
btnO4.setTitle(newArray[3], for: UIControlState.normal)
btnO5.setTitle(newArray[4], for: UIControlState.normal)
现在我希望按钮显示特定的消息,具体取决于它们获取的名称。也就是说,如果一个按钮的名称为“燕麦牛奶”,那么当单击该按钮时,我希望在警报中显示有关燕麦的信息。
所以我有以下代码:
let ingredient1Text = UIAlertController.init(title: "", message: "", preferredStyle: UIAlertControllerStyle.alert)
ingredient1Text.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.default, handler:nil))
self.present(ingredient1Text, animated: true, completion: nil)
switch ingredient1 {
case "Oat Milk":
ingredient1Text.title = "Oat Milk"
ingredient1Text.message = oatMilkText
case "Soy Milk":
ingredient1Text.title = "Soy Milk"
ingredient1Text.message = soyMilkText
case "Almond Milk":
ingredient1Text.title = "Almond Milk"
ingredient1Text.message = almondMilkText
case "Cashew Milk":
ingredient1Text.title = "Cashew Milk"
ingredient1Text.message = cashewMilkText
我不能做的就是将该代码放入按钮操作中。这是因为变量fragment1在viewDidload()中,所以它无法识别该变量。我可以将这些变量放在 viewDidLoad 之外,但我不能,因为我在定义每个随机值后从数组中删除元素,显然这不会在顶层发生。
所以我被困住了。
最佳答案
您可以在操作中访问按钮的标题,如下所示:
@IBAction func myAction(_ sender: Any?) {
guard let button = sender as? UIButton else { return }
let buttonTitle = button.title(for: .normal)
...
}
关于swift - 在 UIviewcontroller 的按钮操作中使用 viewDidLoad 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46028486/
我正在学习 objective-c 教程,并注意到 viewDidLoad 中的代码放在 super viewDidLoad 下,而不是第一次调用 viewDidLoad。 代码放在viewDidLo
在Apple的scrollView示例中,他们没有这样称呼。我一直认为这是必须的。我为什么要这样调用它? 最佳答案 如果您要重写该方法,您仍然应该在 super 中调用该方法。即使父类(super c
这个问题已经有答案了: Why/when do we have to call super.ViewDidLoad? (6 个回答) 已关闭 4 年前。 override func viewDidLo
大家好,我有一个双 View Controller 。 我的 firstviewcontroller 有一个按钮,这个按钮发送 NSNotification,secontviewController
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Do I always have to call [super viewDidLoad] in the -v
我有一个关于子类化的问题。 我从我的第一个 View 开始:在我的 .h 文件中: @interface viewAController : UIViewController 在我的 .m 文件中:
我读到一篇文章说,当创建 UIViewController 类时,属性需要为零,然后 Storyboard 或在调用 viewDidLoad 时将其添加到值中。 它表示在构造对象时也无法初始化属性。
这是我当前的代码。我什至没有通过 viewDidLoad 方法覆盖,因为在它说 super viewdidload 的那一行它抛出错误 @interface for uitableview 为选择器
import UIKit class SecondViewController: UIViewController { @IBOutlet weak var labelA: UILabel!
我需要设置和存储一些可用于 View 的 NSUserDefaults 数据。这不适用于 viewDidLoad。 这可以做到吗? 在 viewDidLoad 之前我可以使用什么方法? 你会推荐什么?
当我向主视图 Controller 中的 viewDidLoad 添加方法时,我的 iPhone 应用程序崩溃了: #import "dbQuestionGetterViewController.h"
当用户切换到另一个程序然后再次返回时,原始程序的 View 将被另一个程序的新 View 替换。那么当用户切换回原来的程序时,viewDidLoad会被第二次调用吗? 我问这个是因为如果是这种情况,那
全部, 我正在从我的 applicationDidFinishLaunching 方法创建一个 windowController ... (void)applicationDidFinishLaunc
这个问题已经有答案了: Looking to understand the iOS UIViewController lifecycle (11 个回答) iPhone SDK: what is th
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 5年前关闭。 Improve this
我知道viewDidLoad在 UIViewController 期间可能会多次调用方法的生命周期。但这怎么可能?如何让它多次调用而不是直接调用它?我试过这样做: UIView *view = [
我在使用 iOS 时遇到了一点问题。我使用协议(protocol)在两个 View Controller 之间来回传递数据并手动切换 View 。我的问题是,当我关闭顶 View 时,不再调用底 Vi
我正在开发一款新应用程序,该应用程序主要用于组织网络内容中的信息。它将是一款用于监控道琼斯指数股息 yield 的金融应用程序。为此,需要执行几个步骤,但我现在正在执行第一步。我想选取道琼斯指数中的
我需要采用 viewDidLoad 方法中填充的变量来显示在连接到自定义单元格的标签上。我想做的是: 查找数据库中存储的用户盒子中的 SKU 使用SKU查找数据库中存储的产品详细信息 将产品详细信息存
我正在 viewDidLoad 中使用异步任务函数,根据返回的响应,我正在更改 View (颜色、文本...),但我有“错误”: 有时,当我更改 View 并返回 View 时,我的 View 背景没
我是一名优秀的程序员,十分优秀!