Reputation: 775
A UILabel in my .xib file that is hooked up to my .m file with an IBOutlet is not updating whether I try to set the text in the init file, viewDidLoad, or viewWillAppear. I ran into a similar problem four days ago (Can't assign text to UILabel or pass NSString property when modally loading a view controller), and the only thing that worked was deleting the view controller and its xib and starting over. I'd rather not have to do that, so I'm posting to see what else might solve this problem. Here's the code:
@interface LoginViewController () <UITextFieldDelegate, UIGestureRecognizerDelegate>
@property (nonatomic, strong) NSURLSession *session;
@property (weak, nonatomic) IBOutlet UILabel *notificationLabel;
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property (nonatomic) int loggedin;
@end
I try to change the label with my initWithNibName:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if(self){
self.title=@"Followers & Following";
self.navigationController.navigationItem.title = @"Follow";
NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:nil delegateQueue:nil];
self.restorationIdentifier = NSStringFromClass([self class]);
self.restorationClass = [self class];
[self view];
self.notificationLabel.text = @"Hello world"; ******************
[self.view setNeedsDisplay];
}
return self;
}
I also try to change it in viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.navigationItem.title = @"Login";
UITapGestureRecognizer* tapBackground = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard:)];
[tapBackground setNumberOfTapsRequired:1];
[self.view addGestureRecognizer:tapBackground];
self.notificationLabel.text = @"Hello world"; ******************
[self.view setNeedsDisplay];
}
and I do the same in viewWillAppear (I'll leave that out for brevity). The IBOutlet is hooked up, so I don't see what else could be going wrong. It will be quite annoying if I have to delete these files and start over just because - what else could be going wrong that I'm not checking?
Thank you.
Upvotes: 1
Views: 1763
Reputation: 131501
Outlets will never be set in your init method, because the XIB file/storyboard isn't loaded at that point.
It should work in viewDidLoad
or viewWillAppear:
. If it doesn't it probably means your outlet link is broken. That's the most common reason for code that works with outlets that seems like it should work but doesn't.
Upvotes: 1