iOS study Day22-storyboard 和xib的混搭

来源:互联网 发布:网络监控摄像头ip地址 编辑:程序博客网 时间:2024/05/13 20:54
#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