简体   繁体   English

在viewWillAppear之后无法以编程方式更改UILabel文本

[英]Can't programmatically change UILabel text after viewWillAppear

I have a routine in my iOS program that imports and manipulates a file from Dropbox. 我的iOS程序中有一个例程,可以从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. 这可能会花费一些时间(5-10秒),并且在执行操作时将用户返回到正常的UI没有意义,所以我想展示一个视图让用户知道进度。

From one VC, I use Dropbox's drop-in file picker, then load up a presented (modal VC) thus: 从一个VC,我使用Dropbox的插入文件选择器,然后加载一个演示的(模式VC),因此:

        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: VC(沼泽标准UIViewController)具有UILabel属性:

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

In viewWillAppear: I can set the text of this label without any problem. 在viewWillAppear中:我可以毫无问题地设置此标签的文本。 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: 从viewDidAppear调用操作文件的方法:

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

However, within the processImport method, the following has no effect: 但是,在processImport方法中,以下内容无效:

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. NSLog显示该方法被称为正常,但标签文本未更改。 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: 我在某处读到了使用NSNotification有帮助的内容,因此尝试将其添加到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: 并从main方法调用此方法:

[[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. 我可以从控制台消息中看到updateStatus方法正在被调用,但是文本仍然没有改变。 Clearly I'm missing something here. 显然我在这里缺少什么。 Any thoughts? 有什么想法吗?

Check your ZSImportVC instances on Debug perspective. 在“调试”透视图中检查您的ZSImportVC实例。

I think u are sending [self.statusMessage setText:message.object]; 我认为您正在发送[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 . 并检查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 对不起,我的英语:S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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