2016-07-30 38 views
0

我有一个ContentPageViewController类,它有IBOutlet的东西。我在ViewController中编写ContentPageViewController的getter,如下面的代码。UILabel是无

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]); 
     } 
    }]; 
} 

但它的工作,如果我去掉上面的吸气剂。 我不知道它是如何工作的,请给我一些提示。谢谢。

+0

与例如initWithNibName或从故事板实例化相比,Alloc'init'将执行非常少的初始化操作。默认实现肯定不会初始化任何子视图 – danh

+0

您是否已经与视图控制器一起设置了xib/storyboard文件? –

+0

嗨@danh,我已经更新了关于实例化的文章,请看看它。你能推荐任何有关你的材料吗?非常感谢。 –

回答

4
  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; 
} 

加你就可以调用初始化页头,所以它不是从情节串连图板相同的实例!

所以,你应该这样做:

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

+0

(1)。我确定它与故事板中的ContentPageViewController相关联。 (2)。我在ContentPageViewController的viewDidLoad中添加了一些打印代码。所以我确定我在加载后更新了UI。 –

+0

从什么函数调用“updateUI”? viewDidLoad中()?你在使用故事板或笔尖文件吗? – MCMatan

+0

对不起,我让你感到困惑,我更新了我的代码,请看看。谢谢。 –