I have a file called 'LoginViewController' that needs to do an action from the 'RootViewController.m' file. I did a NSLog and it functions, but the actual script doesn't.
I have also made sure to #import "RootViewController.h"
in my LoginViewController.
LoginViewController
RootViewController *test = [[RootViewController alloc] init];
[NSThread detachNewThreadSelector:@selector(updateAlbumsAfterLogin) toTarget:test withObject:nil];
RootViewController
- (void)updateAlbumsAfterLogin {
NSLog(@"Updating albums after login!");
// Set up loading box and show it
UIImage *imageLoop = [UIImage imageNamed:@"loading_box.png"];
imagefour = [[UIImageView alloc] initWithImage:imageLoop];
imagefour.frame = CGRectMake(100, 80, 116, 116);
[self.view addSubview:imagefour];
// Set up loading text and show it
myLabel = [[UILabel alloc] initWithFrame:CGRectMake(97, 60, 130, 100)];
myLabel.text = 开发者_Python百科@"Loading...";
myLabel.textColor = [UIColor whiteColor];
myLabel.textAlignment = UITextAlignmentCenter;
myLabel.backgroundColor = [UIColor clearColor];
myLabel.font = [UIFont fontWithName:@"Helvetica" size: 16.0];
myLabel.numberOfLines = 0;
//[myLabel sizeToFit];
[self.view addSubview:myLabel];
// Set up spinner and show it
myIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
myIndicator.center = CGPointMake(158, 155);
myIndicator.hidesWhenStopped = YES;
[self.view addSubview:myIndicator];
[myIndicator startAnimating];
[NSThread detachNewThreadSelector:@selector(updateAlbums) toTarget:self withObject:nil];
//myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector: @selector(updateAlbums) userInfo: nil repeats: YES];
}
Thanks in advance, Coulton.
If you need any other code, feel free to ask for it!
You are detaching a new thread to do this work and update your view, but only the main thread is ever allowed to update the view. That's why you can see your NSLog() messages happening but nothing in your UI updates.
The standard method to handle this is for your main thread to detach a new thread (or perform selector on a background thread), then once the fetching and data handling is complete you perform selector on the main thread which uses the data you fetched to update the UI.
精彩评论