Search code examples
iosios7uilabel

UILabel text updation


I have aUILabel which I want to update.

If I will login with a user name then the UILabel will be updated with that user name,which it is doing correctly and storing the value.

Now I want whenever i will logout then the UILabel text should return the previous value which it stored deleting the new value.

Can you please help me with this.

After login I have updated my UiLabel text like this

-(void)updateUserName:(NSNotification*)noti
{
    NSDictionary* userInfo = [noti userInfo];

    self.user.firstName = [userInfo valueForKey:@"FirstName"];
    self.user.lastName = [userInfo valueForKey:@"LastName"];

    if (self.user.firstName != nil && self.user.firstName.length > 0 && self.user.lastName.length > 0)
    {
        self.userName.text = [NSString stringWithFormat:@"%@ %@",self.user.firstName, self.user.lastName];
    }
}

then after logout I need to retrieve the old data of UiLabel.


Solution

  • NSString *userName =[NSString stringWithFormat:@"%@ %@",self.user.firstName, self.user.lastName]; 
    [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    

    save username to NSUserdefaults

    NSString *userName = [[NSUserDefaults standardUserDefaults]
        stringForKey:@"preferenceName"];
    

    Take the username whenever you want and assign it to UILabel name

    whenever the user got Logout clear the NSuserdefaults

    NSString *userName =@""; 
        [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"preferenceName"];
        [[NSUserDefaults standardUserDefaults] synchronize];