2015-06-21 57 views
0

我的项目设计了一个包含大量标签,图像,textField的视图,而无需使用故事板或笔尖。我是否需要手动分配并将所有内容添加到视图中?我认为这太过于夸张,但我不知道如何以其他方式做到这一点。例如:以编程方式在目标中设计视图 - C

UILabel *firstLabel =[UILabel alloc] init]; 
firstLabel.frame = (0,x,20,10) ; 
firstLabel.text = ...; 
firstLabel.font = ...; 
firstLabel.textColor = ...; 
................. 
[self.view addSubView:firstLabel]; 

UILabel *secondLabel =[UIlabel alloc] init]; 
secondLabel.frame = (0,y,20,10);  
secondLabel.text = ...; 
secondLabel.font = ...; 
secondLabel.textColor = ...; 
................. 
[self.view addSubView:secondLabel]; 

UILabel *thirdLabel =[UIlabel alloc] init]; 
    thirdLabel.frame = (0,z,20,10);  
    thirdLabel.text = ...; 
    thirdLabel.font = ...; 
    thirdLabel.textColor = ...; 
    ................. 
    [self.view addSubView:thirdLabel]; 

我应该把所有的人都在viewDidLoadloadViewinit方法?

或者我只需要制作一个方法CreatLabel并一次又一次地使用它?怎么做?

回答

1

如果您是以编程方式从头开始创建视图,那么您可以在loadView中执行此操作。 (请参阅Creating a View Programmatically。)但是,如果您有顶级视图的NIB /故事板,并且您只是添加子视图,则可以在viewDidLoad中执行此操作。

关于创建一堆标签,是的,你可以使用子例程。或者,假设存在定义这些定位位置的常规模式,则甚至可以使用for循环,在该循环中可以增加坐标或建立约束。 (您可以将对这些视图的引用保存在一个数组中,或使用tag值来跟踪它们。)但是,无论如何,您都希望尽量减少重复编写的代码量(从维护角度简化生活,如果没有别的)。

+0

注意,由于WWDC2015苹果(终于!!!)discou鼓励使用'tag'来跟踪观点。请参阅[Cocoa Touch最佳实践](https://developer.apple.com/videos/wwdc/2015/?id=231) – vikingosegundo

0

可以放入ViewDidLoad(),但如果存在大量标签,则使用自定义方法。

2

如果我正确理解你,请问如何将DRY(不要重复自己)应用于此代码。

«更多二 - 使用for»艾兹赫尔·戴克斯特拉

«更多二 - 使用枚举» vikingosegundo

- (void)viewDidLoad { // or loadView, see Rob's answer 
    [super viewDidLoad]; 

    NSArray *properties= @[@{@"color": [UIColor orangeColor], @"text": @"Ceterum censeo"}, 
          @{@"color": [UIColor cyanColor], @"text": @"Carthaginem esse"}, 
          @{@"color": [UIColor purpleColor], @"text": @"delendam"}]; 

    [properties enumerateObjectsUsingBlock:^(NSDictionary *properties, NSUInteger idx, BOOL *stop) { 
      UILabel *l = [[UILabel alloc] initWithFrame:CGRectMake(0, (idx + 1) * 20, 200, 20)]; 
      l.backgroundColor = properties[@"color"]; 
      l.text = properties[@"text"]; 
      l.font= [UIFont italicSystemFontOfSize:12]; 
      [self.view addSubview:l]; 
     }]; 
} 

好的,为什么我更喜欢这个基于块的枚举?
因为它有一个突变保护并为我提供了正确的免费索引,我需要这个框架。

A C for -loop for (int idx = 0; idx < [properties count]; ++idx)给了我正确的索引,但我必须包括一个额外的语句来获取对象NSDictionary *property = properties[idx];,当它不具有突变后卫:它可以反复可能导致不好的东西在改变。

快速枚举for(NSDictionary *property in properties)具有这样的变异保护,并且枚举比块枚举更快一些。但它有一个很大的缺点,即如果我需要索引,我必须调用NSUIndex idx = [properties indexForObject:property];,而不是线性的,导致二次运行时性能:再见,速度优势。更糟的是:如果一个数组包含相同的对象,它将只能重复地找到第一个 - 这是创建错误数据的好机会。

根据代码数量的不同,将其移入辅助方法可能会很有用 - 但这更多的是关于味道。


正如你到底问题是关于可读性,我想分享的口味另一回事:我喜欢对象创建封装到一个不同的范围:通过使用一个隐含的块

无论是

UILabel *label = ^{ 
    UILabel *l =[[UILabel alloc] initWithFrame:CGRectMake(0, (idx + 1) * 20, 200, 20)]; 
    l.backgroundColor = properties[@"color"]; 
    l.text = properties[@"text"]; 
    l.font= [UIFont italicSystemFontOfSize:12]; 
    return l; 
}(); 
[self.view addSubview:label]; 

或语句表达

UILabel *label = ({ 
    UILabel *l =[[UILabel alloc] initWithFrame:CGRectMake(0, (idx + 1) * 20, 200, 20)]; 
    l.backgroundColor = properties[@"color"]; 
    l.text = properties[@"text"]; 
    l.font= [UIFont italicSystemFontOfSize:12]; 
    l; 
}); 
[self.view addSubview:label]; 
相关问题