简体   繁体   English

Swift 3动态添加删除子视图及其内容

[英]Swift 3 add remove subview and its content dynamically

I am writing my first Swift app for quiz. 我正在编写我的第一个Swift应用程序进行测验。 Each question has random way to render on screen as below screenshot. 每个问题都有随机的方式在屏幕上呈现如下截图。

在此输入图像描述

I am programming app without story board ie. 我是编程应用程序没有故事板即。 programmatically. 编程。 I want to create simple pagination flow for each question within single viewcontroller without using Collocationview, Tableview or navigation. 我想在单个viewcontroller中为每个问题创建简单的分页流,而不使用Collocationview,Tableview或navigation。

What I did so far? 到目前为止我做了什么? I have simple viewcontroller with UIView() added as subview. 我有简单的viewcontroller与UIView()添加为子视图。 I am adding question components dynamically. 我正在动态添加问题组件。 Now once user click on continue I wanted remove subview and add new subview with new question. 现在,一旦用户点击继续,我想删除子视图并添加新的子视图和新问题。 I am able to remove subview but contents on subview seems to be still there as I can see its overwriting. 我能够删除子视图但子视图上的内容似乎仍然存在,因为我可以看到它被覆盖。

To get more clarification please view my code. 要获得更多说明,请查看我的代码。

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)
}

}

And this is what I see which I click continue. 这就是我看到的点击继续。

在此输入图像描述

NOTE: Initially thought using Collocation or Table view will be more appropriate as I can set to scroll horizontally for each question and fetch questions using REST API and place to each cell. 注意:最初考虑使用Collocation或Table视图会更合适,因为我可以设置为每个问题水平滚动并使用REST API获取问题并放置到每个单元格。 But I want to present next screen to user only once then click on continue. 但我想只向用户显示一次下一个屏幕然后点击继续。 I guess with collectionview user can move to next screen on swipe. 我想使用collectionview用户可以在滑动时移动到下一个屏幕。

Just found the answer. 刚刚找到答案。 I assumed removing subview will also remove all components on subview by itself ie. 我假设删除子视图也会自动删除子视图中的所有组件,即。 UILable and UIButton in my case. 在我的情况下UILable和UIButton。

I have to remove them separately. 我必须单独删除它们。

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

Now i can add tag to components and remove like this: 现在我可以添加标签到组件并删除如下:

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

Why are you removing superviews and adding again and again?? 你为什么要删除超级视图并一次又一次地添加? Simply change UILabel.text :) 只需更改UILabel.text :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM