0

Hey everbody, im getting some trouble here. Well, im trying to pass a value from an string to an label. Im setting to setText:myString inside of function, but doesnt works. But when i try to set some random text inside of viewDidLoad, it works.

My function is called inside of viewDidLoad.

    - (void)viewDidLoad {
        //this works
        [precoProd setText:@"hahaha"];
        //here not
        [MyViewClassName print];
        [super viewDidLoad];
    }
   -(void) print{

        float x = 500;
        float c = x/3;

        NSString *valorTotalParcelas;

        if(c > 0) {
            NSString *pVal = [NSString stringWithFormat:@"%0.2f", c];
            NSString *cents = [pVal substringFromIndex:2];
            NSString *reais = [pVal substringWithRange:NSMakeRange(0, 1)];

            valorTotalParcelas = [[NSString alloc] initWithFormat:@"%@,%@",reais, cents];


        }
        // ---------------------
        if(c > 9.99) {
            NSString *pVal = [NSString stringWithFormat:@"%0.2f", c];
            NSString *cents = [pVal substringFromIndex:3];
            NSString *reais = [pVal substringWithRange:NSMakeRange(0, 2)];

            valorTotalParcelas = [[NSString alloc] initWithFormat:@"%@,%@",reais, cents];

        }
        // ---------------------
        if(c > 99.99) {
            NSString *pVal = [NSString stringWithFormat:@"%0.2f", c];
            NSString *cents = [pVal substringFromIndex:4];
            NSString *reais = [pVal substringWithRange:NSMakeRange(0, 3)];

            valorTotalParcelas= [[NSString alloc] initWithFormat:@"%@,%@",reais, cents];

        }
        // ---------------------
        if(c > 999.99) {
            NSString *pVal = [NSString stringWithFormat:@"%0.2f", c];
            NSString *cents = [pVal substringFromIndex:5];
            NSString *reais = [pVal substringWithRange:NSMakeRange(0, 4)];

            valorTotalParcelas = [[NSString alloc] initWithFormat:@"%@,%@",reais, cents];

        }

        NSLog(@"FINAL VALUE ---> %@", valorTotalParcelas);
        [precoProd setText:valorTotalParcelas];

    }

What im doing wrong? Thanks!

1
  • One thing is for sure, there's a memory leak - valorTotalParcelas never got sent the release message. Commented Jan 11, 2011 at 17:41

1 Answer 1

2
[MyViewClassName print];

should be

[self print];

print is an instance method, it should be called on an instance. by doing [MyViewClassName print], you're trying to call it on the class itself, and not on an instance of the class.

Methods that are called on the class object itself (like [NSObject alloc] for example) are class methods and their declaration start with + instead of the - that instance methods start with.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.