iOS study Day22-storyboard 和xib的混搭

#define LINEHEIGHT 44.0
#define TEXTTAG 10
#import "WViewController.h"

@implementation WViewController

-(IBAction)deleteLast
{
    UIView* lastview = [self.view.subviews lastObject];
    CGRect rect = lastview.frame;
    rect.origin.x = self.view.frame.size.width;
    
    [UIView animateWithDuration:0.5f animations:^{
        [lastview setFrame:rect];
        
    } completion:^(BOOL finished) {
        
        [lastview removeFromSuperview];
        _removeIt.enabled = [self.view.subviews count] > 3;
    }];
}

- (IBAction)newtext
{
    // 新建littlesub.xib文件,设置尺寸为null ,添加组件,Label.tag = 10
    // 调用bundle 的loadnib方法得到xib文件
    UIView* littleSub = [[NSBundle mainBundle] loadNibNamed:@"Littlesub" owner:self options:nil][0];
    
    // 此处计算出现 xib文件的位置
    UIView* lastview = [self.view.subviews lastObject];
    CGFloat newy = ([self.view.subviews count] == 3)?( 40 ):(lastview.frame.origin.y + LINEHEIGHT +1  );
    CGRect frame2 = CGRectMake(0, newy, self.view.frame.size.width, LINEHEIGHT);
    
    // viewwithtag方法得到xib文件中的按钮属性
    UIButton* button = (UIButton *)[littleSub viewWithTag:5];
    
    // 代码对其做关联,设置方法littleClick ,并传入自身对象
    [button addTarget:self action:@selector(littleClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:littleSub];
    UILabel* label = (UILabel*)[littleSub viewWithTag:10];
    
    label.text = [NSString stringWithFormat:@"%d is me", [self.view.subviews count]];
    CGRect frame1 = frame2;
    frame1.origin.x = self.view.frame.size.width;
    [littleSub setFrame:frame1];
    
    // 动画效果
    [UIView beginAnimations:nil context:nil];
    
    [littleSub setFrame:frame2];
    
    [UIView commitAnimations];
}

// 按钮得到子对象tag= 10的值
- (IBAction)littleClick:(UIButton*)sender
{
   UILabel* label = (UILabel*)[sender.superview viewWithTag:10];
    NSLog(@" %@ at view", label.text);
    
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值