user678992
user678992

Reputation: 31

- (IBAction)oneButton1:(id)sender

I'm new to iOS, i want to update the text in ViewDidLoad() function.

This is my button function, When button is clicked animation take place and also adds the value "1" to "resultText.text"

   - (IBAction)oneButton1:(id)sender {
    oneBtn2.userInteractionEnabled = YES;
    CGRect frame = oneBtn1.frame;
    CGRect frame1 = reffButton.frame;
    frame.origin.x = frame1.origin.x; 
    frame.origin.y = frame1.origin.y; 

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration: 3.0];

    [UIView animateWithDuration:3.0 animations:^{
        [oneBtn1 setTransform:CGAffineTransformMakeScale(.4, .4)];
    } completion:^(BOOL finished) {
        oneBtn1.hidden = YES;
        price = [resultText.text intValue];

        [resultText setText:[NSString stringWithFormat:@"%i", price+1]];

      }];
    oneBtn1.frame = frame;
    [UIView commitAnimations];

}

Problem: The above text value is 1 but in ViewDidLoad is 0 ,

 - (void)viewDidLoad
 {
[super viewDidLoad];

 NSLog(@"%@", resultText.text); // output is 0 instead of 1;

}

Please anyone tell me how to update the text value in ViewDidLoad function ...

Upvotes: 1

Views: 291

Answers (3)

Rajneesh071
Rajneesh071

Reputation: 31081

This is because every time your view will load it create new object of your textField, thats why you are unable to fetch previous (because its new textField not old one).So you have to save your text some where, for example you can use NSUserDefaults

SetText

NSString *result=[NSString stringWithFormat:@"%i", price+1];

[resultText setText:];

//Also set it to NSUserDefaluts
[[NSUserDefaults standardUserDefaults] setValue:result forKey:@"key"];
[[NSUserDefaults standardUserDefaults] synchronize];

Get Text

- (void)viewDidLoad
{
    [resultText setText:[[NSUserDefaults standardUserDefaults] valueForKey:@"key"]];
    NSLog(@"%@", resultText.text); 
}

EDIT

You can make animation afterButton click, so call this method in button click event

-(void)animateImage
{
    if ([resultText.text isEqualToString:@"3"]) {
        //make your animation
    }
}

Upvotes: 1

Vinodh
Vinodh

Reputation: 5268

You can use a method to do that

     - (void)updateLabel {
        oneBtn2.userInteractionEnabled = YES;
        CGRect frame = oneBtn1.frame;
        CGRect frame1 = reffButton.frame;
        frame.origin.x = frame1.origin.x; 
        frame.origin.y = frame1.origin.y; 

        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration: 3.0];

        [UIView animateWithDuration:3.0 animations:^{
            [oneBtn1 setTransform:CGAffineTransformMakeScale(.4, .4)];
        } completion:^(BOOL finished) {
            oneBtn1.hidden = YES;
            price = [resultText.text intValue];

            [resultText setText:[NSString stringWithFormat:@"%i", price+1]];

          }];
        oneBtn1.frame = frame;
        [UIView commitAnimations];

   }


     - (void)viewDidLoad
     {
    [super viewDidLoad];
     [self updateLabel];
     NSLog(@"%@", resultText.text); // output is 0 instead of 1;

    }

Upvotes: 0

Albin Joseph
Albin Joseph

Reputation: 1141

ViewDidLoad calling only once when the object is creating .So you can't update the thing in ViewDidLoad.ViewDidLoad use for initialising the parameters and set the initial settings when an object creating.

Upvotes: 7

Related Questions