首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用swift创建自定义弹出视图?

如何使用swift创建自定义弹出视图?
EN

Stack Overflow用户
提问于 2015-11-30 04:06:39
回答 3查看 68.8K关注 0票数 18

因此,我正在开发一个简单的应用程序,其中我使用了表视图。此表视图显示了"players“的名称。但为了让我在表视图中添加球员,我希望显示一个弹出窗口,其中包含一个文本字段,您可以在其中提供名称。

现在,我一直在阅读有关创建xib或nib文件的内容,但我不确定如何“加载”弹出窗口。

解决这个问题的最好方法是什么?

看起来像这样:

EN

Stack Overflow用户

回答已采纳

发布于 2015-11-30 04:11:35

你需要用所有需要的对象创建一个定制的UIView,然后在你的控制器的viewDidLoad()中隐藏它。

代码语言:javascript
运行
复制
customView.hidden = true

每当你的用户想要执行某项操作或任务时,你可以将其取消隐藏,一旦用户完成,然后再次隐藏它,或者从superView中删除它。

代码语言:javascript
运行
复制
customView.hidden = false

下面有一些代码可以帮助您入门

代码语言:javascript
运行
复制
    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

票数 17
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33987442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档