Ios Swift 3动态添加/删除子视图及其内容
我正在为测验编写我的第一个Swift应用程序。每个问题都有随机的方式呈现在屏幕上,如下截图所示 我是编程应用程序没有故事板即编程。我想在单个viewcontroller中为每个问题创建简单的分页流,而不使用配置视图、表视图或导航 到目前为止我做了什么?我有一个简单的viewcontroller,其中UIView()被添加为子视图。我正在动态添加问题组件。现在,一旦用户单击“继续”,我希望删除子视图并添加新的子视图和新问题。我可以删除子视图,但子视图上的内容似乎仍然存在,因为我可以看到它被覆盖 要获得更多澄清,请查看我的代码Ios Swift 3动态添加/删除子视图及其内容,ios,iphone,swift,uitableview,uiview,Ios,Iphone,Swift,Uitableview,Uiview,我正在为测验编写我的第一个Swift应用程序。每个问题都有随机的方式呈现在屏幕上,如下截图所示 我是编程应用程序没有故事板即编程。我想在单个viewcontroller中为每个问题创建简单的分页流,而不使用配置视图、表视图或导航 到目前为止我做了什么?我有一个简单的viewcontroller,其中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)
}
}
这就是我看到的,我点击继续
注意:最初认为使用配置或表格视图更合适,因为我可以设置为水平滚动每个问题,并使用RESTAPI获取问题,并将其放置到每个单元格。但我只想向用户显示下一个屏幕一次,然后单击“继续”。我想使用collectionview,用户可以在滑动时移动到下一个屏幕 刚刚找到答案。我假设移除子视图也会移除子视图上的所有组件,在我的例子中是UILable和UIButton 我必须把它们分开
for subview in self.subView.subviews {
subview.removeFromSuperview()
}
现在,我可以向组件添加标记并按如下方式删除:
for subview in self.subView.subviews {
if (subview.tag == 1) {
subview.removeFromSuperview()
}
}
为什么要删除SuperView并一次又一次地添加??
只需更改UILabel.text:)谢谢@Paulw11-这将如何帮助从子视图中删除标签?让我更新我的帖子,以显示我在单击“继续”时看到的内容。对于self.view.subview{view.removeFromSuperview()}中的视图,请在添加任何内容之前在create_subview()中使用此代码。发明轮子总是一个糟糕的做法。这就是为什么你应该在故事板(或者是一个nib文件,它将是一个VC类型并使用它的视图)中为每个问题创建VCs,并使用唯一的UI。以编程方式开发UI是一个糟糕的做法,在开发iOS时,只要有可能,就应该避免这种做法。按应有的方式利用auotlayout。使用故事板/nib的可维护性将是超级敏捷的。