Uilabel Text Not Being Updated

UILabel text not being updated

You may be facing an issue with threading as mentioned in the comments above. If you have a method that runs on the main thread and does some activity (such as search a database), updates that you make to the UI will not be committed until the run loop gets control. So, if you have a long, time consuming task going on on the main thread, run this code after setting the text of the label:

- (void)doSomethingTimeConsuming
... consume some time ...
... set text of label ...
[[NSRunLoop mainRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
... continue long operation ...
}

This should flush out any UI changes that you have made. Although this may be a sensible and functional hack, it doesn't beat the alternative. I highly suggest that you perform your app's time consuming tasks on a background thread, and update the UI through the main thread using performSelectorOnMainThread:withObject:waitUntilDone:. See Apple's page on iOS Thread Management.

UILabel text not updating inside For loop execution Swift

Your issue not occured in my machine. Please restart your machine, maybe problem will resolve.

UILabel Not Updating to match UILabel.text

You need to make sure that the constraint for the UILabel's width is wide enough to accommodate the text. Otherwise, it is being cutoff and the ... you are seeing is because the label's line break is set to truncating tail, which means that the end of the string will be replaced with ... when the label is too narrow.

So this is an interface builder issue and not a Swift-specific issue. The label's text is being correctly updated, the UI is just not able to properly display it.

If you want the width constraint of the label to change dependent on the text, there are ways to calculate the width of the text and update the constraint's constant to accommodate that text's width.

Why is the label's text not updating?

Your outlets aren't wired up until the view loads.

Try creating a property in your KeysController, say:

var keyText: String?

Set the property after instantiating the view controller:

tipsVC.keyText = firstTips[initialRow]

Then in your KeysController.viewDidLoad method:

key1.text = keyText


Related Topics



Leave a reply



Submit