【问题标题】:Swift 3 add remove subview and its content dynamicallySwift 3 动态添加删除子视图及其内容
【发布时间】:2017-03-25 10:57:27
【问题描述】:

我正在编写我的第一个 Swift 测验应用程序。每个问题都有随机呈现在屏幕上的方式,如下图所示。

我正在编写没有故事板的应用程序,即。以编程方式。我想在不使用 Collocationview、Tableview 或导航的情况下为单个视图控制器中的每个问题创建简单的分页流程。

到目前为止我做了什么?我有添加 UIView() 作为子视图的简单视图控制器。我正在动态添加问题组件。现在,一旦用户单击继续,我想删除子视图并添加带有新问题的新子视图。我可以删除子视图,但子视图上的内容似乎仍然存在,因为我可以看到它被覆盖。

要获得更多说明,请查看我的代码。

import UIKit

class QuizController: UIViewController {

let subView = UIView()
var currentQuestion:Int = 1;


let questions = ["This is question 1", "Hello to question 2", "Question 3"]

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.backgroundColor = .white
    setup_layout()
}

func setup_layout(){

    let closeBtn = UIButton(frame: CGRect(x: 5, y: 10, width: 200, height: 50))
    closeBtn.backgroundColor = UIColor.red
    closeBtn.setTitle("Close", for: .normal)
    closeBtn.addTarget(self, action: #selector(close), for: .touchUpInside)
    view.addSubview(closeBtn)
    //dynamic view
    create_subview()
}

func nextQuestion(){
    print("Show next")
    if let viewWithTag = self.view.viewWithTag(currentQuestion) {
        viewWithTag.removeFromSuperview()
        currentQuestion += 1;
        create_subview()
    } else {
        print("No!")
    }


}

func create_subview(){

    let heightOfView = view.frame.size.height
    let widthOfView = view.frame.size.width

    subView.tag = currentQuestion
    subView.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.2)
    subView.frame = CGRect(x: 0, y: 60, width: self.view.frame.width, height: heightOfView - 60)
    self.view.addSubview(subView)

    let txtLabel1 = UILabel(frame: CGRect(x: 35, y: 120, width: widthOfView , height: 20))
    txtLabel1.text = questions[currentQuestion-1]
    txtLabel1.font = txtLabel1.font.withSize(12)
    subView.addSubview(txtLabel1)

    let nextBtn = UIButton(frame: CGRect(x: 5, y: 300, width: 200, height: 50))
    nextBtn.backgroundColor = UIColor.red
    nextBtn.setTitle("Continue", for: .normal)
    nextBtn.addTarget(self, action: #selector(nextQuestion), for: .touchUpInside)
    subView.addSubview(nextBtn)

}

func close(){
    self.dismiss(animated: true, completion: nil)
}

}

这就是我看到的,我点击继续。

注意:最初认为使用搭配或表格视图会更合适,因为我可以设置为每个问题水平滚动并使用 REST API 获取问题并放置到每个单元格。但我只想向用户显示下一个屏幕,然后单击继续。我猜想使用 collectionview 用户可以在滑动时移动到下一个屏幕。

【问题讨论】:

  • 谢谢@Paulw11 - 这将如何帮助从子视图中删除标签?让我更新我的帖子以显示当我单击继续时看到的内容。
  • for view in self.view.subviews { view.removeFromSuperview() } 在添加任何内容之前在 create_subview() 中使用此代码。
  • 发明轮子总是一种不好的做法。这就是为什么您应该在故事板(或将是 VC 类型并使用其视图的 nib 文件)中为每个具有独特 UI 的问题创建 VC。以编程方式开发 UI 是一种不好的做法,在开发 iOS 时应尽可能避免使用它。充分利用自动布局。使用故事板/笔尖的可维护性将非常灵活。

标签: ios iphone swift uitableview uiview


【解决方案1】:

刚刚找到答案。我假设删除子视图也会自行删除子视图上的所有组件,即。 UILable 和 UIButton 在我的例子中。

我必须单独删除它们。

for subview in self.subView.subviews {
       subview.removeFromSuperview()
}

现在我可以向组件添加标签并像这样删除:

    for subview in self.subView.subviews {
        if (subview.tag == 1) {
            subview.removeFromSuperview()
        }
    }

【讨论】:

  • 您为什么要删除超级视图并一次又一次地添加?只需更改 UILabel.text :)
【解决方案2】:

您为什么要删除超级视图并一次又一次地添加? 只需更改 UILabel.text :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 2012-11-19
    • 2023-03-21
    • 1970-01-01
    • 2014-01-07
    相关资源
    最近更新 更多