Search code examples
iphoneuilabelviewdidload

how to show the values on label same untill the app quits


I have label in which i am loading the text value in viewDidLoad and i am changing that value on button click i want the value changed on button clicked to be remain same in the label untill we again click the label but when i move to next screen and comeback to original then again it shows the value is which is set in viewDidLoad not the changed values.

   -(void)viewDidLoad{

    testLabel.text=@"This is fine";


   }

  -(void)changeData{

    testLabel.text=@"changed data";

   }     



    -(IBAction)riskButtonAction{

   RiskViewController*targetContrlloer=[[RiskViewController alloc] init];
   [self.navigationController pushViewController:targetContrlloer animated:YES];

   }

Solution

  • try below code

     -(void)viewWillAppear:(BOOL)animated
        {
           NSString *checkFb=[[NSUserDefaults standardUserDefaults] valueForKey:@"someKey"];
           if(checkFb.lenght > 0)
           { 
    
            testLabel.text=checkFb;
            }
    
        }
    
    
       -(void)changeData{
    
          testLabel.text=@"changed data";
        [[NSUserDefaults standardUserDefaults] setObject: testLabel.text forKey:@"someKey"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    
       }    
    

    let me know it is working or not!!!