使用iOS中的按钮更改标签文本

时间:2017-09-07 01:09:54

标签: ios swift

首先,我想说我是Swift和Xcode的新手。我有一个标签显示一些文字,我希望按下按钮时该文字会改变。

以下是UIViewController中的代码:

var txt = "Hey"

@IBOutlet weak var Text: UILabel!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    showText()
}

func showText(){
    Text.text = txt
}

@IBAction func Button(_ sender: Any) {
    txt = "Hello"
}

按钮和标签正确链接到他们的main.storyboard对应物。当我运行它并按下按钮时,文本不会改变。

5 个答案:

答案 0 :(得分:2)

viewDidLoad()仅在创建ViewController时调用一次。如果要显示showText(),则需要在设置txt后调用txt。自动执行此操作的一种方法是向showText()添加属性观察者,以便在txt更新时调用var txt = "Hey" { didSet { showText() } }

bool isDead(Particle &p) {
    if (p.position.x > ofGetWindowWidth() || p.position.x == -1 || p.position.y > ofGetWindowHeight() || p.position.y == -1) {
        return true;
    }
    else {
        return false;
    }
}
//--------------------------------------------------------------
void ofApp::setup() {


    ofSetFrameRate(60);
    ofEnableAlphaBlending();
    ofBackground(ofColor::black);

    for (int i = 0; i < 50; i++) {
        Particle p;
        p.setup(ofVec2f(ofRandom(ofGetWindowWidth() * 0.45, ofGetWindowWidth() * 0.55), ofRandom(ofGetWindowHeight() * 0.45, ofGetWindowHeight() * 0.55)));
        particles.push_back(p);
    }

    beat1.load("beat1.wav");
    beat2.load("beat2.wav");
    beat3.load("beat3.wav");

    fftSmooth = new float[8192];

    for (int i = 0; i < 8192; i++) {
        fftSmooth[i] = 0;
    }

    bands = 128;
    beat1.setVolume(0.2);
    beat2.setVolume(0.2);
    beat3.setVolume(0.2);
}
//--------------------------------------------------------------
void ofApp::update() {

    ofSoundUpdate();

    float * value = ofSoundGetSpectrum(bands);
    for (int i = 0; i < bands; i++) {
        fftSmooth[i] *= 0.2f;
        if (fftSmooth[i] < value[i]) {
            fftSmooth[i] = value[i];
        }
    }

    ofRemove(particles, isDead);

    for (Particle& p : particles) {
        if (!p.isActive) {
            p.position = ofVec2f(ofRandom(ofGetWindowWidth() * 0.45, ofGetWindowWidth() * 0.55), ofRandom(ofGetWindowHeight() * 0.45, ofGetWindowHeight() * 0.55));
            p.isActive = true;
            return;
        }
        p.update();
    }



    if (isDead) {
        Particle p;
        p.setup(ofVec2f(0, 0));
        particles.push_back(p);
    }

}

//--------------------------------------------------------------
void ofApp::draw() {
    for (Particle& p1 : particles) {
        if (!p1.isActive) continue;


        bool foundConnection = false;
        //search for connections.
        for (Particle& p2 : particles) {
            if (!p2.isActive || p2.drawPosition == p1.drawPosition) continue;

            float distance = p1.drawPosition.distance(p2.drawPosition);

            for (int i = 0; i < bands; i++) {
                if (distance > 10 && distance < 50 * fftSmooth[i]) {
                    ofDrawLine(p1.drawPosition, p2.drawPosition);
                    foundConnection = true;
                }
            }
        }

        for (int i = 0; i < 50; i++) {
            p1.draw(-(fftSmooth[i] * 10));

        }
    }
}

答案 1 :(得分:2)

我将补充说,让showText()方法将实际文本显示为参数,而不是依赖于某些内部状态,它会更灵活,也许更优雅(属性txt: String的值,在调用网站上不会立即显现)以完成其任务:

@IBOutlet weak var textLabel: UILabel!

func showText(_ text: String){
    self.textLabelabel?.text = text

    // (optional chaining to avoid crash if accidentally 
    // called before viewDidLoad())
}  

@IBAction func buttonAction(_ sender: Any) {
    self.showText("Hello")  // From here it is clear what text will be shown
}

答案 2 :(得分:1)

按以下方式更改您的代码

<script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

    <a href="#" class="clickme">Click me!</a>
<img class="movingimg" src="https://s26.postimg.org/ma8qknfw5/image.png" alt="">

答案 3 :(得分:0)

用按钮更改标签文本只需要传递标签中的文字;

//Outlets  
@IBOutlet weak var firstLabel: UILabel!        
@IBOutlet weak var secondLabel: UILabel!


 //Button Action
 @IBAction func textChabge(_ sender: Any) {

            firstLabel.text = "newText"

            secondLabel.text = "newText"

  }

答案 4 :(得分:0)

只需像这样更改 IBAction

svg{
  border: 1px solid gray;
}