UILabel文本未更新

时间:2014-11-19 20:56:04

标签: ios objective-c uilabel

我的.xib文件中的UILabel连接到带有IBOutlet的.m文件,不会更新我是否尝试在init文件,viewDidLoad或viewWillAppear中设置文本。我在四天前遇到了类似的问题(Can't assign text to UILabel or pass NSString property when modally loading a view controller),唯一有效的方法是删除视图控制器及其xib并重新开始。我宁愿不必那样做,所以我发帖看看还有什么可以解决这个问题。这是代码:

@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

我尝试使用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;

}

我也尝试在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];

}

我在viewWillAppear中做了同样的事情(为了简洁,我将其留下)。 IBOutlet被连接起来,所以我不知道还有什么可能出错。如果我不得不删除这些文件并重新开始,那将是非常烦人的 - 因为 - 我还没有检查到其他什么可能出错?

谢谢。

1 个答案:

答案 0 :(得分:1)

在init方法中永远不会设置Outlets,因为此时不会加载XIB文件/故事板。

它可以在viewDidLoadviewWillAppear:中使用。如果它不是它可能意味着您的出口链接被破坏。这是与插件配合使用的代码最常见的原因,似乎它应该可以工作但不是。

相关问题