【发布时间】:2014-07-06 09:00:51
【问题描述】:
我里面有一个 UIViewController 和一个 UIView。当我尝试在 UIView 中添加警报时,我必须使用控制器来呈现 UIAlertController。如何将 UIViewController 的引用传递给 UIView 类?或者如何创建控制器的委托?
class GameViewController: UIViewController {
@IBOutlet var gameBoardUIView: GameBoardUIView
...
}
class GameBoardUIView: UIView {
...
func move() {
if !gameBoard.checkNextMoveExist() {
var alert = UIAlertController(title: "Game Over", message: nil, preferredStyle: UIAlertControllerStyle.Alert)
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Cancel, handler: {(action: UIAlertAction!) in
println("Game Over")
}))
}))
// Following would fail because self is not a UIViewController here
// self.presentViewController(alert, animated: true, completion: nil)
}
}
}
【问题讨论】:
-
你看到这个答案了吗:stackoverflow.com/a/3732812/2468186?
-
这看起来与您之前的问题How to create an alert in a subview class in Swift? 相同(您接受了答案)。
-
@MartinR 我将这个问题发布到一个更通用的问题,即如何从子 UIView 引用父控制器。这可能对正在寻找此类主题的其他人有所帮助
标签: ios uiview uiviewcontroller swift