我想在按下特定按钮后将事件指示器(即事件指示器 View )添加到 View 中。不过,按钮的代码不在该 View Controller 的 swift 文件中,而是在一个自定义类 swift 文件中,该文件与 View Controller swift 文件“交互”(不确定该术语是否正确?)。我的代码没有提示任何错误,但它只是无法正常工作。 View 上不显示任何事件指示器,并且屏幕应该停止接受用户输入,但事实并非如此。我已在下面发布了相关代码:
class CustomCell: UITableViewCell, UITextFieldDelegate {
var activityIndicator: UIActivityIndicatorView = UIActivityIndicatorView()
@IBAction func saveButtonTapped(sender: UIButton) {
func topMostController() -> UIViewController {
var topController = UIApplication.sharedApplication().keyWindow?.rootViewController
while (topController?.presentedViewController != nil) {
topController = topController?.presentedViewController
}
activityIndicator = UIActivityIndicatorView(frame: CGRectMake(0,0,50,50))
activityIndicator.center = topController!.view.center //centers the indicator
activityIndicator.hidesWhenStopped = true
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
topController!.view.addSubview(activityIndicator)
activityIndicator.startAnimating()
UIApplication.sharedApplication().beginIgnoringInteractionEvents()
return topController!
}
}
}
如果闭包量不正确,那不是问题......这只是我搞砸了相关代码位的复制和粘贴。有人有什么想法吗?
最佳答案
您必须调用 bringSubviewToFront
将指示器置于前面。
topController!.view.bringSubviewToFront(activityIndicator)
关于ios - View 未添加到 Xcode 中的顶 View Controller ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38966684/