Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting label text after completing block Swift

I want to set user name to a label because my block ("findObjectsInBackgroundWithBlock") is taking time to complete and label.text sets nil .

how to set label text after completing the block??

class test {         

var userName: String? // variable to access user name returned by block        
private func loadData() {      

getCurrentUser()         
lblUserName.text = userName       

}    

}

This is the block where i am getting user data from Parse.

private func getCurrentUser() {         

        if PFUser.currentUser() != nil { 

            currentUser = PFUser.currentUser()?.username    
            let query = PFQuery(className: "_User")    
            query.whereKey("username", equalTo: currentUser!)    
            query.findObjectsInBackgroundWithBlock { (currentUsers, error) -> Void in 
                if error == nil {     
                    for user in currentUsers! {     
                       userName = user["name"] as? String      
                    }   
                }
            }
        }
    }
like image 951
S. Bharti Avatar asked Feb 10 '16 07:02

S. Bharti


People also ask

How to create a label in Swift source code programmatically?

UIKit.UILabel class is used to create a label in iOS swift source code programmatically. You can change label text, label text color, label background color, label border width, label border color by setting it’s properties.

How do I change the label text wrap in Swift?

You can also change the label text wrap method by setting the label object’s lineBreakMode property. This example will tell you how to create a swift label object programmatically in source code, how to align the label text, and how to change the label text line numbers and wrap the label text words.

How to set numberoflines to 0 in Swift?

// Set numberOfLines to 0 means the label text can has any number lines. // Initialize the tips message label. // Set message label's initial text alignment to center. // Add the two labels to the screen. /* This function will be invoked when click button 'Change Label Text Alignment'. */ labelOne.text = "Hello swift label."

How to center text in Swift tips message label?

// Initialize the tips message label. // Set message label's initial text alignment to center. // Add the two labels to the screen. /* This function will be invoked when click button 'Change Label Text Alignment'. */ labelOne.text = "Hello swift label."


2 Answers

You can use a property observer here like this:

var userName: String? {
    didset {
       if let name = username {
           lblUserName.text = name
       } 
    }
}
like image 133
Guy Daher Avatar answered Sep 22 '22 04:09

Guy Daher


You can add a completion handler function argument to your getCurrentUser function and call that handler when you retrived the data:

private func getCurrentUser(completion: (result: String) -> Void) {         
    if PFUser.currentUser() != nil { 
        currentUser = PFUser.currentUser()?.username    
        let query = PFQuery(className: "_User")    
        query.whereKey("username", equalTo: currentUser!)    
        query.findObjectsInBackgroundWithBlock { (currentUsers, error) -> Void in 
            if error == nil {     
                for user in currentUsers! {     
                   completion(user["name"] as? String)     
                }   
            }
        }
    }
}

Then pass the complition function like this:

getCurrentUser() { (result: String) in
    self.lblUserName.text = result
}  

I cannot prove it is fully working code as I don't have XCode now to test it. But you should get the idea.

like image 34
Dmitry Klochkov Avatar answered Sep 21 '22 04:09

Dmitry Klochkov