简体   繁体   中英

Can't programmatically change UILabel text after viewWillAppear

I have a routine in my iOS program that imports and manipulates a file from Dropbox. This can take some time (5-10 seconds) and it doesn't make sense to return the user to the normal UI while it's doing it, so I want to present a view letting the user know what the progress is.

From one VC, I use Dropbox's drop-in file picker, then load up a presented (modal VC) thus:

        ZSImportVC *importVC = [[ZSImportVC alloc] init];
        importVC.results = results;
        [importVC setModalPresentationStyle:UIModalPresentationFormSheet];
        [self presentViewController:importVC animated:YES completion:^{
            [self performFetch];
        }];

The VC (a bog-standard UIViewController), has a UILabel property:

@property (weak, nonatomic) IBOutlet UILabel *statusMessage;

In viewWillAppear: I can set the text of this label without any problem. The thing is, I want to keep changing this text as the process of manipulating the file continues.

The method that manipulates the file is called from viewDidAppear:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self processImport];
}

However, within the processImport method, the following has no effect:

self.statusMessage.text = @"Some text to update the user.";

So I created a method:

- (IBAction)updateStatus:(NSString *)message
{
    [self.statusMessage setText:message];
    NSLog(@"%@", message);
}

just to check what's going on. The NSLog shows that the method is being called okay, but the label text doesn't change. I tried adding:

[self.statusMessage setNeedsDisplay];

to the method, but that didn't help. I'm not using any private queues or background threads. I read somewhere that using NSNotification helps, so I tried adding this to viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateStatus:) name:@"updateStatus" object:nil];

Then changed the called method to:

- (void)updateStatus:(NSNotification *)message
{
    [self.statusMessage setText:message.object];
    NSLog(@"%@", message.object);
}

and called this from the main method with:

[[NSNotificationCenter defaultCenter] postNotificationName:@"updateStatus" object:@"Retrieving file from Dropbox" userInfo:nil];

I could see from the console messages that the updateStatus method is getting called, but still the text doesn't change. Clearly I'm missing something here. Any thoughts?

Check your ZSImportVC instances on Debug perspective.

I think u are sending [self.statusMessage setText:message.object]; to another instance, that it is not shown on the screen.

I mean,

put a debug point here:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self processImport];     /* Debug point here*/
}

and check what´s the hexadecimal direction for self .

Now , do the same here:

- (void)updateStatus:(NSNotification *)message
{
    [self.statusMessage setText:message.object];  /* Debug point here*/
    NSLog(@"%@", message.object);
}

Btw, I had some problems with

 [[NSNotificationCenter defaultCenter] postNotificationName:@"updateStatus" object:@"Retrieving file from Dropbox" userInfo:nil];

because the instance that receive this notification was not the correct.

I fixed that calling a normal method, but u maybe could not do that.

Sorry for my english :S

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