iOS自我学习库

Interface Builder

2017-10-03  本文已影响495人  wpf_register

参考文档1
参考文档2

近来系统学习了《利用IB优雅地开发UI》,收获颇多。开始接触可视化编程时,非常喜欢,简单高效,可以少写很多代码,但后来实践中更多的使用的是纯代码。主要还是对XIB及SB好些特性不甚了解,自以为代码处理问题更加灵活,也更有优越感,实则不然。

IB 从Xcode4开始集成到Xcode当中,成为IDE的一部分,之后每个版本都会对其进行改进加强,更多地体现出苹果对可视化编程的重视。

IB 基础

 UINib  *nib = [UINib nibWithNibName:@"VCname" bundle:nil] ;
 VCname *vc = [nib instantiateWithOwner:nil options:nil ][0];
  [sb instantiateInitialViewController];(左侧小箭头的初始VC)
  [sb instantiateViewControllerWithIdentifier:@"VCName"];

相比较xib,sb不仅可以处理VC间跳转,同时对TableView等视图可以直接拖入Cell进行布局,xib更倾向view层面,场景复杂,sb更倾向VC层面,功能强大。

Xib相关

[[NSBundle mainBundle] loadNibNamed:@"" owner:nil options:nil];
[[UINib nibWithNibName:@"" bundle:nil]instantiateWithOwner:nil options:nil];
- (void)prepareForReuse;
     [UIView animateWithDuration:0.2 animations:^{
         self.widthConstraint.constant = 100;
         self.heightConstraint.constant = 100;
         //添加动画
         [self.view layoutIfNeeded];
     }];

XIB 嵌套

关于xib的嵌套使用在父视图-awakeFromNib方法中用代码添加子视图,但不优雅。

设置Files Owner 清空View的Classs.jpg
//设置frame时用self的尺寸,而不要随意设置(很重要)
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        UIView *containerView = [[[UINib nibWithNibName:@"ScrollowView" bundle:nil] instantiateWithOwner:self options:nil] objectAtIndex:0];
        CGRect newFrame = CGRectMake(0, 0, self.frame.size.width, 179);
        containerView.frame = newFrame;
        [self addSubview:containerView];
    }
    return self;
}

Storyboard

//跳转时传值
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if ([segue.identifier isEqualToString:@"showSec"]) {
        if ([segue.destinationViewController isKindOfClass:[SecondViewController class]] && [segue.sourceViewController isKindOfClass:[ViewController class]]) {
            SecondViewController *secVC = segue.destinationViewController;
            //传值建议用KVC
            //不能直接对目标VC的UI进行更改
            [secVC setValue:@"不是测试" forKey:@"secStr"];
            
        }
    }
}
//根据相应条件判断是否要跳转
- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
    
    if ([identifier isEqualToString:@"showSec"]) {
        //根据condition判断
        //返回值决定是否跳转
        return YES;
    }else{
        
        return NO;
    }
    
}

回传时须要在要返回去的VC中实现(IBAction)方法,参数UIStoryboard。将返回事件联接到VC 顶部的Exit按钮上,会自动选择方法。

//回传值的方法
- (IBAction)theBack:(UIStoryboardSegue*)segue{
   
    //注意
    //返回值必须是 IBAction
    //方法名随意
    
    if ([segue.identifier isEqualToString:@"backViewController"]) {
        
        //destinationViewController 是当前VC
        NSLog(@"%@",[segue.destinationViewController class]);
        
        //sourceViewController 是源VC
        SecondViewController *secVC = segue.sourceViewController;
        NSLog(@"%@",secVC.backStr);
    }
}
[self.view addSubView:subView];
[self  addChildViewController:suVC];

在IB 中系统提供一个ContainerView 专门处理这类事件,需要时可尝试使用。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
      UITableViewCell  *cell = sender;
      NSIndexPath *index =  [self.tableView indexPathForCell:cell];
}
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header" forIndexPath:indexPath];
    }else{
        return [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer" forIndexPath:indexPath];
    }
}

Use Trait Variations适配

通过底部的Trait Variations 特征亦是适配横竖屏下的不同约束。
点击相应约束,constant侧面有“+”标志的可以增加以添加不同特征约束。

设备 竖屏 横屏
3.5英寸 iPhone wC hR wC hC
4.0英寸 iPhone wC hR wC hC
4.7英寸 iPhone wC hR wC hC
5.5英寸 iPhone wC hR wR hC
iPad(all) wR hR wR hR

External Object 相关

仅限 xib 使用

  1. 如图示做以下操作,并在 VC,VCView 和 VCManager 中拉相关点击事件。
  2. 在 VC 中代码
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _manager = [VCManager new];
    //这里注意传入这个dic中的_aPersonHandle对象必须是全局变量
    NSDictionary *pramaDic = @{@"testKey" : _manager};
    NSDictionary *optionDic = @{UINibExternalObjects : pramaDic};
    //options这个参数只接收一个key为UINibExternalObjects的字典,
    //这个字典的value也必须是个字典,
    //这个value字典的key就是xib中设置的external object的identifier
    VCView *vcView = [[NSBundle mainBundle] loadNibNamed:@"VCView" owner:self options:optionDic][0];
  
    [self.view addSubview:vcView];
}
VCView Files Owner VCManager设置 Class Identifier

Object [1]

  1. 新建ViewControllerManager 类,继承自NSObject,并实现 -(IBAction)***方法。
  2. IB中添加Object类,并且与源文件关联。
  3. 通过连线可以在ViewControllerManager中实现VC中部分代码逻辑。
  4. 可以将viewControllerManager作为属性拉到VC中去,也可以将VC作为属性拉到ViewControllermanager中去
    须要用weak修饰一下,防止循环引用 )

VC瘦身的话可以考虑将VC中的Delegate中的内容放到Manager中去



Xnip2018-09-26_10-49-04.jpg
  1. 实测仅SB成功

上一篇下一篇

猜你喜欢

热点阅读