- (IBAction)oneButton1:(id)发件人

时间:2013-09-23 06:26:57

标签: ios xcode

我是iOS新手,我想更新ViewDidLoad()函数中的文本。

这是我的按钮功能,单击按钮时会发生动画,并将值“1”添加到“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];

}

问题:上面的文本值为1但在ViewDidLoad中为0,

 - (void)viewDidLoad
 {
[super viewDidLoad];

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

}

请有人告诉我如何在ViewDidLoad函数中更新文本值...

3 个答案:

答案 0 :(得分:7)

ViewDidLoad只在对象创建时调用一次。因此,您无法在ViewDidLoad.ViewDidLoad中更新事物,用于初始化参数并在创建对象时设置初始设置。

答案 1 :(得分:1)

这是因为每次你的视图加载它都会创建textField的新对象,这就是你无法获取之前的原因(因为它的新textField不是旧文本)。所以你必须将文本保存在某些地方,例如你可以使用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];

获取文字

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

修改

您可以在按钮点击后制作动画,因此请在按钮点击事件中调用此方法

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

答案 2 :(得分:0)

您可以使用方法来执行此操作

     - (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;

    }