UILabel是零

时间:2016-07-30 14:16:13

标签: ios objective-c

我有一个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 个答案:

答案 0 :(得分:4)

  1. 检查您的IBOutlet是否已连接。
  2. 在从storyboard / nib
  3. 创建视图之前,请检查您正在调用的方法

    修改

    您添加的代码行将覆盖您的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,所以它与storyboard不是同一个实例!

    所以你应该这样做:

    - (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函数都会调用。并返回相同的实例。