繁体   English   中英

UILabel为零

[英]The UILabel is nil

我有一个ContentPageViewController类,它具有IBOutlet东西。 我像下面的代码一样在ViewController中编写ContentPageViewController的getter。

ContentPageViewController.h

@interface ContentPageViewController : UIViewController

@property (weak, nonatomic) IBOutlet UILabel *busName;
@property (weak, nonatomic) IBOutlet UILabel *busTime;
@property (weak, nonatomic) IBOutlet UILabel *busType;

@end

ViewController.m

- (void)viewDidLoad 
{
    [super viewDidLoad];

    // instantiation from a storyboard
    ContentPageViewController *page = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentPageViewController"];
    self.page = page;

    // send url request
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.apb-shuttle.info/now" ]];

    [self sendURLRequest:request];
    // add the view of ContendPageViewController into ViewController
    [self.view addSubview:self.page.view];
}

// It works if i remove the following code
- (ContentPageVC *)page
{
    if (_page) _page = [[ContentPageViewController alloc] init];

    return _page;
}

更新时没有任何反应。 它给了我零。

- (void)updateUI 
{
     // I got null here
     NSLog("%@", self.page.busName)
     // The spacing style font
     NSDictionary *titleAttributes = @{
                                  NSKernAttributeName: @10.0f
                                 };
     NSDictionary *attributes = @{
                             NSKernAttributeName: @5.0f
                            };

     self.page.busName.attributedText = [[NSMutableAttributedString alloc] initWithString:bus.name
                                                             attributes:titleAttributes];
     self.page.busTime.attributedText = [[NSMutableAttributedString alloc] initWithString:bus.depart
                                                             attributes:titleAttributes];
     self.page.busType.attributedText = [[NSMutableAttributedString alloc] initWithString:bus.note
                                                             attributes:attributes];

}

以下代码是我调用updateUI时的代码:

- (void)sendURLRequest:(NSURLRequest *)requestObj
{
    isLoading = YES;
    [RequestHandler PerformRequestHandler:requestObj withCompletionHandler:^(NSDictionary *data, NSError *error) { 
        if (!error) {
            bus = [JSONParser JSON2Bus:data];
            // Add the bus object into the array.
            [self.busArray addObject: bus];
            [[NSOperationQueue mainQueue] addOperationWithBlock: ^{
                [self updateUI];
                isLoading = NO;
            }];
        } else {
            NSLog(@"%@", [error localizedDescription]);
        }
    }];
}

但是,如果我删除上面的吸气剂,它会起作用。 我不知道它是如何工作的,请给我一些提示。 谢谢。

  1. 检查您的IBOutlet是否已连接。
  2. 从情节提要/笔尖创建视图之前,请检查未调用您正在调用的方法

编辑

您添加的代码行覆盖了您的getter。 每次调用self.page时,您都会创建一个新实例!

// It works if i remove the following code
- (ContentPageVC *)page
{
    if (_page) _page = [[ContentPageViewController alloc] init];

    return _page;
}

应该是这样的:

// It works if i remove the following code
- (ContentPageVC *)page
{
    if (!_page) _page = [[ContentPageViewController alloc] init]; // Added the ! mark, only if nil you would create a new instance.

    return _page;
}

另外,您正在对其调用alloc init,因此它与情节提要中的实例不同!

因此,您应该这样做:

- (ContentPageVC *)page
{
    if (!_page) _page = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentPageViewController"];

    return _page;
}

并删除以下代码行:

   // instantiation from a storyboard
    ContentPageViewController *page = [self.storyboard instantiateViewControllerWithIdentifier:@"ContentPageViewController"];
    self.page = page;

每次您调用“ self.page”时,覆盖getter函数都会调用。 并返回相同的实例。

暂无
暂无

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

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