开发者

Problem with passing string with a function

开发者 https://www.devze.com 2023-02-03 21:23 出处:网络
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

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:NSMak开发者_运维问答eRange(0, 4)];

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

        }

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

    }

What im doing wrong? Thanks!


[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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号