简体   繁体   中英

UILabel doesn't update through modal presentation view controller

I have two class, PresentViewController and ModalViewController , In PresentViewController I call the ModalViewController in this format:

ModalViewController *targetController = [[ModalViewController alloc] init];

targetController.modalPresentationStyle = UIModalPresentationFormSheet;

targetController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

[self presentViewController:targetController animated:YES completion:nil];

// it is important to do this after presentModalViewController:animated:
targetController.view.superview.bounds = CGRectMake(0, 0, 400, 218);

In ModalViewController , I have a button who send a parameter to the class PresentViewController :

-(IBAction)apply{

    PresentViewController *infoViewController = [[PresentViewController alloc] init];
    [infoViewController setDiscount:[campoDesconto.text intValue]];

        [self dismissViewControllerAnimated:YES completion:nil];

}

In PresentViewController the method setDiscount is as follows:

-(void)setDiscount:(int)value{

    NSLog(@"Method is called!");
    totalPrice.text = value;

}

This method is being called because I get a console message, but unfortunately this UILabel not updating your value, why this is happening and how can I solve?

That's because totalPrice is not probably instantiated before setDiscount: is called. Call setDiscount: after your view is loaded and totalPrice is instantiated to have the label text updated.

When you call apply method, you haven't setup the view of infoViewController yet. So you probably haven't created the totalPrice label yet, if you do NSLog(@"totalPrice: %@", totalPrice); in setDiscount: , it will probably output (null) . You should save the discount value as a property, and set it in viewDidLoad as well as when setDiscount: method is called.

You're also setting an int to a NSString , this isn't correct.

PresentViewController.h

@interface PresentViewController : UIViewController

@property (nonatomic, assign) int discount;

@end

PresentViewController.m

@implementation PresentViewController

-(void)viewDidLoad
{
    [super viewDidLoad];

    // Setup detailLabel

    detailLabel.text = [@(self.discount) stringValue];
}

-(void)setDiscount:(int)discount
{
    _discount = discount;
    detailLabel.text = [@(_discount) stringValue];
}

@end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM