iOS开发UI篇—xib的简单使用实例

2020-01-18 17:15:30刘景俊

运行效果:
ios中xib的使用,ios,xib使用教程,ios开发xib的使用

三、对xib进行连线示例

1.连线示例

新建一个xib对应的视图类,继承自Uiviewios中xib的使用,ios,xib使用教程,ios开发xib的使用
在xib界面右上角与新建的视图类进行关联ios中xib的使用,ios,xib使用教程,ios开发xib的使用
把xib和视图类进行连线ios中xib的使用,ios,xib使用教程,ios开发xib的使用
注意:在使用中把weak改成为强引用。否则...

2.连线后的代码示例

YYViewController.m文件代码如下:


//
// YYViewController.m
// 10-xib文件的使用
//
// Created by apple on 14-5-24.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import "YYapp.h"
#import "YYappview.h"

@interface YYViewController ()
@property(nonatomic,strong)NSArray *app;
@end

@implementation YYViewController

//1.加载数据信息
-(NSArray *)app
{
  if (!_app) {
    NSString *path=[[NSBundle mainBundle]pathForResource:@"app.plist" ofType:nil];
    NSArray *temparray=[NSArray arrayWithContentsOfFile:path];
    
    //字典转模型
    NSMutableArray *arrayM=[NSMutableArray array ];
    for (NSDictionary *dict in temparray) {
      [arrayM addObject:[YYapp appWithDict:dict]];
    }
    _app=arrayM;
  }
  return _app;
}

//创建界面原型
- (void)viewDidLoad
{
  [super viewDidLoad];
  NSLog(@"%d",self.app.count);
  
  //九宫格布局
  int totalloc=3;
  CGFloat appviewW=80;
  CGFloat appviewH=90;
  CGFloat margin=(self.view.frame.size.width-totalloc*appviewW)/(totalloc+1);
  
  int count=self.app.count;
  for (int i=0; i<count; i++) {
    
    int row=i/totalloc;
    int loc=i%totalloc;
    CGFloat appviewX=margin + (margin +appviewW)*loc;
    CGFloat appviewY=margin + (margin +appviewH)*row;
    YYapp *app=self.app[i];
    
    //拿出xib视图
    NSArray *apparray= [[NSBundle mainBundle]loadNibNamed:@"appxib" owner:nil options:nil];
    
    //注意这里的类型名!
    //UIView *appview=[apparray firstObject];
    YYappview *appview=[apparray firstObject];
    
    //加载视图
    appview.frame=CGRectMake(appviewX, appviewY, appviewW, appviewH);
     [self.view addSubview:appview];
    
    appview.appimg.image=app.image;
    appview.applab.text=app.name;
    appview.appbtn.tag=i;
    
    [ appview.appbtn addTarget:self action:@selector(appviewbtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
  }
}

/**按钮的点击事件*/
-(void)appviewbtnClick:(UIButton *)btn
{
  YYapp *apps=self.app[btn.tag];
  UILabel *showlab=[[UILabel alloc]initWithFrame:CGRectMake(60, 450, 200, 20)];
  [showlab setText:[NSString stringWithFormat: @"%@下载成功",apps.name]];
  [showlab setBackgroundColor:[UIColor lightGrayColor]];
  [self.view addSubview:showlab];
  showlab.alpha=1.0;
  
  //简单的动画效果
  [UIView animateWithDuration:2.0 animations:^{
    showlab.alpha=0;
  } completion:^(BOOL finished) {
    [showlab removeFromSuperview];
  }];
}

@end