【发布时间】:2023-03-31 04:10:01
【问题描述】:
我正在尝试将UITapGestureRecognizer 添加到imageview,如下所示
let gest=UITapGestureRecognizer(target: self, action: Selector(imgPressed()));
gest.delegate=self
thalaImg.addGestureRecognizer(gest)
这里是imgPressed 函数:
func imgPressed()
{
let alert=UIAlertController(title: "Img", message: "img pressed", preferredStyle: .Alert)
let okButton=UIAlertAction(title: "Ok", style: .Default, handler: nil)
alert.addAction(okButton)
presentViewController(alert, animated: true, completion: nil)
}
我在viewdidload 中添加了 3 行代码,添加了断点并运行了应用程序。我观察到,一旦编译器到达第一行,即 let gest...,它会立即调用 action 方法甚至在应用程序开始运行之前。由于显然 window 尚未加载,它会引发以下警告
警告:尝试呈现不在窗口层次结构中的视图!
我不明白为什么会这样。有人可以帮我解决这个问题吗?
谢谢
【问题讨论】:
-
'func imgPressed() { let alert=UIAlertController(title: "Img", message: "img pressed", preferredStyle: .Alert) let okButton=UIAlertAction(title: "Ok", style: .Default, handler: nil) alert.addAction(okButton) presentViewController(alert, animated: true, completion: nil) }'
-
好的,在 imgPressed 方法之后删除 ()。并在双引号中加上“imgPressed”。
-
您正在研究哪个 swift 版本意味着 swift 2.0 或 swift 3.0?
-
你已经把方法绑定到手势识别器了对吧?
标签: ios xcode swift uigesturerecognizer uitapgesturerecognizer