Update a label through button from different view
There are a few ways to maintain communication between views (view controllers, actually) in iOS. Easiest of which for me is sending notifications. You add an observer for a notification in the view you want to make the change, and from the view that will trigger the change, you post the notification. This way you tell from ViewController B to ViewController A that "something is ready, make the change"
This, of course, requires your receiver view to be created and already be listening for the notification.
In ViewController B (sender)
- (void)yourButtonAction:(id)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil];
}
In ViewController A (receiver)
Add the observer to listen for the notification:
- (void)viewDidLoad
{
//.........
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeTheChange) name:@"theChange" object:nil];
}
Do NOT forget to remove it (in this case, on dealloc
)
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"theChange" object:nil];
[super dealloc];
}
And finally, the method that will update your label
- (void)makeTheChange
{
yourLabel.text = @"your new text";
}
Update Label On Another View
use custom delegate method create a delegate in second view and access that view delegate function in first view. or use NSNotification or use NSUserdefaults
How to update a label text with button inside a collection view in swift 4
If I understood correctly, each time you push + or - you want to update slot label. In my opinion the easiest and fastest way to achieve this it's using NotificationCenter.default.post
In your collection view cell on button action write:
NotificationCenter.default.post(name: Notification.Name("postAction"), object: numberToIncreaseOrDecrease)
In your MainViewController where you have the slot label, add this code in view did load:
NotificationCenter.default.addObserver(self, selector: #selector(updateSlotValue(_:)), name: NSNotification.Name("postAction"), object: nil)
And out from the view did load add this function:
@objc func updateSlotValue(_ notification: Notification) {
let value = notification.object as! Int
cartSlot_lbl.text.text = value
}
how to update label of other viewController in swift
You can use delegation. These are the required steps:
- Create a delegate protocol that defines the messages sent to the delegate.
- Create a delegate property in the delegating class to keep track of the delegate.
- Adopt and implement the delegate protocol in the delegate class.
- Call the delegate from the delegating object.
SecondVC
import UIKit
//1. Create a delegate protocol
protocol CitySelectionDelegate {
func pickCity(with selectedCity:String)
}
class SelectCityPage: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
var selectedCity: String!
//2. Create a delegate property in the delegating class
var delegate:CitySelectionDelegate?
//other stuff
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath){
if indexPath.row == 0{
selectedCity = "Almaty"
}
if indexPath.row == 1{
selectedCity = "Усть-Каменогорск"
}
4. Call the delegate from the delegating object.
delegate?.pickCity(with: selectedCity) //call your delegate method
//dismiss(animated: true, completion: nil) when you dismiss is up to you
}
}
HomeViewController
UPDATE: Since you have another VC embedded inside a UINavigationController, both the Home and Select Region Page MUST conform to the delegate and you have to set the delegate inside prepareForSegue method.
// 3. Adopt and implement the delegate protocol
class HomeViewController: UIViewController, CitySelectionDelegate{
@IBOutlet weak var selectRegion: UIButton!
func pickCity(with selectedCity: String) {
self.selectRegion.text = selectedCity
}
/*please pay attention. In this case you must reference the
navigation controller first and the your can get the right
instance of the delegate variable inside your firstVC (I called
firstVC but in your case is Select Region Page*/
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// you MUST set the right identifier
if segue.identifier == "showFirst" {
if let navController = segue.destination as? UINavigationController {
if let firstVC = navController.topViewController as? FirstViewController{
firstVC.delegate = self
}
}
}
}
}
since you have another VC (embedded in a navigation controller), also this one must conform to the delegate like so:
class FirstViewController: UIViewController, CitySelectionDelegate {
var delegate: CitySelectionDelegate?
override func viewDidLoad() {
super.viewDidLoad()
}
func pickCity(with selectedCity: String){
// here you simply call the delegate method again and you dismiss the navigation controller
self.delegate?.pickCity(with: selectedCity)
self.navigationController?.dismiss(animated: true, completion: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "showSecond" {
if let controller = segue.destination as? SelectCityPage {
controller.delegate = self
}
}
}
Xcode - Update label text when button is pressed in other view
You can use NSNotificationCenter. Put this in you IBAction.
[[NSNotificationCenter defaultCenter] postNotificationName:@"buttonPressed" object:nil];
And this to your viewDidLoad.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectorhere) name:@"buttonPressed" object:nil];
somewhere in your .m
(void)selectorhere {
labelTimer.text = @"foo";
}
Updating Label Text several times while a Button is pressed in Swift
You can run all of your long-running tasks in a background queue, and make the label updates in the main queue. The key is to call the next function only when the first is finished, or they will run in parallel, and could all update at the same time. Here's an example
func doSomething1() {
// as good a way as any to simulate a long-running process
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text1"
}
DispatchQueue.global(qos: .background).async {
self.doSomething2()
}
}
func doSomething2() {
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text2"
}
DispatchQueue.global(qos: .background).async {
self.doSomething3()
}
}
func doSomething3() {
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text3"
}
DispatchQueue.global(qos: .background).async {
self.doSomething4()
}
}
func doSomething4() {
sleep(1)
DispatchQueue.main.async {
self.myLabel.text = "Some text4"
}
}
@IBAction func cmdDoStuff(_ sender: UIButton) {
DispatchQueue.global(qos: .background).async {
self.doSomething1()
}
}
Kivy: Function Call to Update Label Text in Another Class Not Working
The label doesn't change because you call wrong object's resetStatSummary
method.
Within reset's on_release
button method you have:
Factory.StatSummary().resetStatSummary()
which means: create fresh new StatSummary
object (StatSummary()
will create and return new object - class StatSummary
instance), then call it's method resetStatSummary()
. So you call this method on brand new object, not this created by kv file. To access expected StatSummary class instance just replace line:
Factory.StatSummary().resetStatSummary()
with
app.root.get_screen('statSummary').resetStatSummary()
Related Topics
What Privacy-Violating or Device-Changing Things How to Do on an Iphone
Change Button Image When Play Completes Swift
Launch Screen Storyboard Not Displaying Image
How to Set a Custom View's Intrinsic Content Size in Swift
Accessing Core Data SQL Database in iOS 8 Extension (Sharing Data Between App and Widget Extension)
Draw iOS 7-Style Squircle Programmatically
How to Set the Position of the Cursor in Uitextview
How to Transfer Files from One Application to Another in the Same iOS Device
Cellforitematindexpath Not Called But Numberofitemsinsection Does
iOS Simulator Display Scaled Incorrectly
How to Parse This JSON in Swift
Swift: Declare an Empty Dictionary
Playing a Video File from a Server in Swift
How to Detect Taps on Mkpolylines/Overlays Like Maps.App
Uisearchdisplaycontroller's Searchresultstableview's Contentsize Is Incorrect. Bug in iOS 7
Profile Doesn't Match the Entitlements File's Value for the Application-Identifier Entitlement