所以我正在开发一个简单的应用程序,我使用表视图。此表视图显示“播放器”的名称。但是为了让我在表格视图中添加玩家,我想要一个弹出窗口显示一个文本字段,您可以在其中提供名称。
现在我一直在阅读有关创建xib或nib文件的内容,但我不确定如何“加载”弹出窗口。
最好的方法是什么?
看起来像这样:
答案 0 :(得分:15)
在故事板中
在viewController中
let popvc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "another_view_sid") as! anotherViewController
self.addChildViewController(popvc)
popvc.view.frame = self.view.frame
self.view.addSubview(popvc.view)
popvc.didMove(toParentViewController: self)
在anotherViewController中
override func viewDidLoad() {
super.viewDidLoad()
showAnimate()
}
}
@IBAction func Close_popupView(_ sender: Any) {
removeAnimate()
}
func showAnimate()
{
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
UIView.animate(withDuration: 0.25, animations: {
self.view.alpha = 1.0
self.view.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
})
}
func removeAnimate()
{
UIView.animate(withDuration: 0.25, animations: {
self.view.transform = CGAffineTransform(scaleX: 1.3, y: 1.3)
self.view.alpha = 0.0
}, completion: {(finished : Bool) in
if(finished)
{
self.willMove(toParentViewController: nil)
self.view.removeFromSuperview()
self.removeFromParentViewController()
}
})
}
答案 1 :(得分:14)
您需要创建一个自定义UIView
,其中包含所需的所有受尊重对象,您可以在Controller的viewDidLoad()中隐藏它。
customView.hidden = true
每当您的用户想要执行某些操作或任务时,您取消隐藏它,一旦用户完成,然后再次隐藏或从superView中删除。
customView.hidden = false
下面有一些代码可以帮助您开始
private var customView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
customView.hidden = true
}
private func loadCustomViewIntoController() {
let customViewFrame = CGRect(x: 0, y: 0, witdh: view.frame.width, height: view.frame.height - 200)
customView = UIView(frame: customViewFrame)
view.addSubview(customView)
customView.hidden = false
// any other objects should be tied to this view as superView
// for example adding this okayButton
let okayButtonFrame = CGRect(x: 40, y: 100, width: 50, height: 50)
let okayButton = UIButton(frame: okayButtonFrame )
// here we are adding the button its superView
customView.addSubview(okayButton)
okayButton.addTarget(self, action: #selector(self.didPressButtonFromCustomView:), forControlEvents:.TouchUpInside)
}
func didPressButtonFromCustomView(sender:UIButton) {
// do whatever you want
// make view disappears again, or remove from its superview
}
@IBAction func rateButton(sender:UIBarButtonItem) {
// this barButton is located at the top of your tableview navigation bar
// when it pressed make sure you remove any other activities that were on the screen, for example dismiss a keyboard
loadCustomViewIntoController()
}
查看这个github project,关闭生产就绪,它为你提供了一个更好的方式来处理(呈现和解雇)UIViews
如果您只想要播放器的名称,请使用包含文本字段的
UIAlertController
答案 2 :(得分:1)
此第三方EzPopup(https://github.com/huynguyencong/EzPopup)可以帮助您展示它。只需在故事板中设计弹出窗口,然后启动它。
// init YourViewController
let contentVC = ...
// Init popup view controller with content is your content view controller
let popupVC = PopupViewController(contentController: contentVC, popupWidth: 100, popupHeight: 200)
// show it by call present(_ , animated:) method from a current UIViewController
present(popupVC, animated: true)