简体   繁体   English

如何删除Rubymotion中的所有子视图?

[英]How do I remove all subviews in Rubymotion?

Basically, I have a quiz app where I add have questions, then switch to a different screen to show an expanded version of the correct answer after it is chosen. 基本上,我有一个测验应用程序,我添加有问题,然后切换到另一个屏幕,以显示选择后正确答案的扩展版本。 I've tried adding button and labels, but when I do this, I eventually end up with buttons and labels 'ghosting' on the screen, after I went them gone. 我尝试过添加按钮和标签,但是当我这样做时,我最终会在屏幕上显示按钮和标签“重影”,之后我就不再使用了。 How can I remove all subviews, so that each button and label I add from within a method appears on a fresh screen? 如何删除所有子视图,以便我在方法中添加的每个按钮和标签都会出现在新屏幕上?

class MyApplicationController < UIViewController

def loadView  
    self.view = UIImageView.alloc.init  
end

def viewDidLoad  
    super  
    view.userInteractionEnabled=true  
    @question_index=0  
    @answerChoices = []  
    @questionChoices=["What is best in life?",...]  
    makeQuestion  
end  

def makeQuestion 
    left =20  
    top=100  
    label = UILabel.new  
    label.text =@questionChoices[@question_index]  
    label.textAlignment = UITextAlignmentCenter  
    label.textColor = UIColor.redColor  
    label.frame = [[20,20], [view.frame.size.width-40, 40]]  
    label.lineBreakMode = UILineBreakModeWordWrap  
    label.numberOfLines = 0  
    label.sizeToFit  
    view.image = UIImage.imageNamed('background.jpg')  
    view.addSubview(label)  
    4.times do |i|  
        @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)  
    @button.setTitle(@answerChoices[i][@question_index], forState:UIControlStateNormal)  
        @button.frame = [[left, top], [view.frame.size.width - left * 2, 40]]  
    @button.addTarget(self, action:'actionTapped:', forControlEvents:UIControlEventTouchUpInside)  
        @button.tag=i  
        top+=45  
        view.addSubview(@button)  
    end  
end

def makeAnswer  
    @button.removeFromSuperview
    @button.removeFromSuperview
    @button.removeFromSuperview
    @button.removeFromSuperview
    view.image = UIImage.imageNamed('back.jpg')
    label = UILabel.new
    label.text =@rightArray[@question_index]
    label.textAlignment = UITextAlignmentCenter
    label.textColor = UIColor.blackColor
    label.frame = [[20,20], [view.frame.size.width-40, 40]]  
    label.lineBreakMode = UILineBreakModeWordWrap  
    label.numberOfLines = 0  
    label.sizeToFit  
    view.addSubview(label)  


    @button = UIButton.buttonWithType(UIButtonTypeRoundedRect)  
    @button.setTitle("More questions", forState:UIControlStateNormal)  
    @button.tag=1  
    @button.addTarget(self, action:'makeQuestion', forControlEvents:UIControlEventTouchUpInside)     
    @button.frame = [[20, 100], [view.frame.size.width - 20 * 2, 40]]  
    view.addSubview(@button)  
end 

end

You need an array of buttons. 你需要一系列按钮。 In your 4.times loop, you are overwriting the instance variable @button four times. 在你4.times循环,要覆盖实例变量@button四倍。 Instead, make @button an array of buttons, and assign a different button to each index. 相反,将@button设为一个按钮数组,并为每个索引指定一个不同的按钮。

If you have a variable myView and want to remove all of its subviews, I believe this will work: 如果你有一个变量myView并想要删除它的所有子视图,我相信这将有效:

myView.subviews.each {|sv| sv.removeFromSuperview}

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

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