Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
244 views
in Technique[技术] by (71.8m points)

ios - Changing label text inside method

I'm using delegation to pass back information from a view controller. This is the method

 func writeValueBack(value: String) {
  self.label.text = value
}

The function gets called and all is great apart from the label doesn't update.

The label has a value and is not returning nil, I checked with this line

println(self.label.text)

It prints the value of 'value'

So that means that the label's text is being set to 'value' but it's not updating.

I even tried using the main thread but no luck

func writeValueBack(value: String) {
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.label.text = value
    })
}

I just don't know what the problem is.

Any help would be great.

Protocol:

protocol writeValueBackDelegate {
func writeValueBack(value: String)
}

EDIT: Code for my view controller:

//
//  ViewController.swift
//  DelegateTesting
//
//  Created by Alex Catchpole on 30/11/2014.
//  Copyright (c) 2014 Alex Catchpole. All rights reserved.
//

import UIKit

class ViewController: UIViewController, writeValueBackDelegate {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!


override func viewDidLoad() {
    super.viewDidLoad()
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "MainSegue" {
        var vc = segue.destinationViewController as SecondViewController
        vc.labelText = textField.text
        vc.delegate = self
    }
}

func writeValueBack(value: String) {
    dispatch_async(dispatch_get_main_queue(), { () -> Void in
        self.label.text = value
    })
}


@IBAction func button(sender: AnyObject) {
    self.label.text = textField.text
}

@IBAction func segue(sender: AnyObject) {
    performSegueWithIdentifier("MainSegue", sender: self)

}

}

Second ViewController source:

//
//  SecondViewController.swift
//  DelegateTesting
//
//  Created by Alex Catchpole on 30/11/2014.
//  Copyright (c) 2014 Alex Catchpole. All rights reserved.
//

import UIKit

class SecondViewController: UIViewController {
@IBOutlet weak var label: UILabel!
@IBOutlet weak var textField: UITextField!

var labelText: String!
var delegate: writeValueBackDelegate? = nil

override func viewDidLoad() {
    super.viewDidLoad()
    label.text = labelText

    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
@IBAction func button(sender: AnyObject) {
    label.text = textField.text
}


@IBAction func segueBack(sender: AnyObject) {
    var editedText = label.text
    performSegueWithIdentifier("SecondSegue", sender: self)
    if (delegate != nil) {
        delegate?.writeValueBack(editedText)
        println("working")
    }
}
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The issue is in your second view controller, which as rdelmar pointed out creates a new instance of your first view controller instead of navigating back to the original instance.

To fix this, you could use dismissViewControllerAnimated:completion instead of performing your second segue. But an unwind segue would lead to simpler code, and can be achieved by adding this to your first view controller:

@IBAction func unwindFromSecond(unwindSegue: UIStoryboardSegue) {
    if let secondViewController = unwindSegue.sourceViewController as? SecondViewController {
        label.text = secondViewController.label.text
        // Or whatever you need to retrieve data from the second controller
    }
}

Then in your storyboard create an unwind segue from the second view controller. For example if you have a Dismiss button, control-drag from this button to the Exit icon in your second view controller scene and choosing unwindFromSecond. For detailed steps see the answer to this other question: What are Unwind segues for and how do you use them?

You can now remove the writeValueBackDelegate declaration and associated variables, the writeValueBack method and the second view controller's segueBack method


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...