1.指定根目录,建立MVC,model以及自定义网格xib执行拖拽
xib拖拽对象属性,在其.h中声明一个方法,并且在.m中给值,实现方法,model类继承与nsobject找到相对应属性声明并且在model.m中实现一个方法如下:
2.设置info网络允许请求网络,新建控制器跳转到webview
viewcontroller中代码如下:
//
// ViewController.m
// 男装界面 8.21
#import "ViewController.h"
#define HCWidth self.view.frame.size.width
#define HCHeight self.view.frame.size.height
#import "ZDCollectionViewCell.h"
#import "NanZ.h"
#import "AFHTTPSessionManager.h"
#import "TiaoViewController.h"
@interface ViewController ()<UICollectionViewDelegate , UICollectionViewDataSource>
@property (nonatomic , strong) UICollectionView *collV;
@property (nonatomic , strong) NSMutableArray *datasource;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view addSubview:self.collV];
[self.collV registerNib:[UINib nibWithNibName:@"ZDCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"coll"];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
self.datasource = [[NSMutableArray alloc] init];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager GET:@"http://m.api.zhe800.com/v5/deals?parent_tag=&url_name=wireless1004&order=&deal_type=&min_price=&max_price=&user_type=1&user_role=1&student=0&shop_type=&image_type=si3&image_model=webp&super=2&page=1&per_page=20" parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"%@",responseObject);
for (NSDictionary *dic in responseObject[@"objects"]) {
NanZ *nan = [[NanZ alloc] init];
[nan setValuesForKeysWithDictionary:dic];
[self.datasource addObject:nan];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.collV reloadData];
});
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@",error);
}];
}
-(UICollectionView *)collV{
if (!_collV) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.itemSize = CGSizeMake(HCWidth/2-2, 400);
layout.minimumLineSpacing = 1;
layout.minimumInteritemSpacing = 1;
_collV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, HCWidth, HCHeight) collectionViewLayout:layout];
_collV.delegate = self;
_collV.dataSource = self;
_collV.backgroundColor = [UIColor lightGrayColor];
}
return _collV;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return _datasource.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
ZDCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"coll" forIndexPath:indexPath];
// UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, HCWidth/2, 200)];
// [cell addSubview:imgV];
// UILabel *lb1 = [[UILabel alloc] initWithFrame:CGRectMake(10, 210, 200, 20)];
// [cell addSubview:lb1];
// UILabel *lb2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 210, 200, 20)];
//
// [cell addSubview:lb2];
if (self.datasource.count>0) {
[cell setValueForNanZ:self.datasource[indexPath.row]];
}
cell.backgroundColor = [UIColor whiteColor];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
TiaoViewController *tiao = [[TiaoViewController alloc] init];
tiao.chuan = [_datasource[indexPath.row] ID];
[self.navigationController pushViewController:tiao animated:NO];
} \
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
最后在第二个继承UIviewcontroller的界面中实现webview,这里需要建立传值属性
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"宝贝详细";
// self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"首页" style:UIBarButtonItemStyleDone target:self action:nil];
UIWebView *WebView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
WebView.contentScaleFactor = YES;
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://out.zhe800.com/jump?id=%@&jump_source=2",self.chuan]]]];
[self.view addSubview:WebView];
}
最后是为大家讲解一下父子控制器,因为在生活工作中搭建界面跟多需要用到父子控制器的地方:
1.需要几个按钮滚动就创建几个控制器
2.viewcontroller中代码如下
//
// ViewController.m
// 淘宝男装
//
// Created by Apple on 2018/8/21.
// Copyright © 2018年 chuck. All rights reserved.
//
#import "ViewController.h"
#import "oneViewController.h"
#import "twoViewController.h"
#import "threeViewController.h"
#import "fourViewController.h"
#import "fiveViewController.h"
@interface ViewController ()<UIScrollViewDelegate>{
UIScrollView *scr;
UIScrollView *CenterScr;
UIButton *btn;
}
@property (nonatomic , strong)UIButton *selBtn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setnav];
//添加所以子控制器
[self setUpVc];
//设置顶部滚动试图
[self setUp];
//按钮
[self setBtn];
//设置中间滚动试图
[self setCenter];
}
-(void)setnav{
self.navigationItem.title =@"淘我喜欢";
self.navigationController.navigationBar.barTintColor = [UIColor yellowColor];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"搜索--1"] style:UIBarButtonItemStyleDone target:self action:@selector(abc)];
}
-(void)abc{
}
//设置中间滚动试图
- (void)setCenter{
CenterScr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 108, self.view.frame.size.width, self.view.frame.size.height-108)];
//滚动范围
CenterScr.contentSize = CGSizeMake(self.view.frame.size.width* self.childViewControllers.count, 0);
//代理
CenterScr.delegate = self;
CenterScr.pagingEnabled = YES;
CenterScr.showsHorizontalScrollIndicator = NO;
CenterScr.showsVerticalScrollIndicator = NO;
CenterScr.bounces = NO;
[self.view addSubview:CenterScr];
for (int i= 0 ; i<self.childViewControllers.count; i++) {
//获取对应的子控制器
UIViewController *VCL = self.childViewControllers[i];
//设置子控制器view的位置
VCL.view.frame = CGRectMake(self.view.frame.size.width*i, 0, self.view.frame.size.width, self.view.frame.size.height-108);
[CenterScr addSubview:VCL.view];
}
}
//设置顶部滚动试图
- (void)setUp{
scr = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, 44)];
scr.backgroundColor = [UIColor blueColor];
scr.contentSize = CGSizeMake(5*100, 0);
[self.view addSubview:scr];
}
//按钮
- (void)setBtn{
//获取当前控制器的子控制器
NSInteger count = self.childViewControllers.count;
for (int i =0; i<count; i++) {
//初始化位置
btn = [[UIButton alloc]initWithFrame:CGRectMake(i*100, 0, 100, 44)];
//绑定标识
btn.tag = i;
//获取对应的子控制器的文字
UIViewController *Vc = self.childViewControllers[i];
if (i == 0) {
[self click:btn];
}
//添加按钮文字
[btn setTitle:Vc.title forState:UIControlStateNormal];
//默认颜色
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
//选中后的颜色
[btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
[btn addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
[scr addSubview:btn];
}
}
//按钮方法
- (void)click:(UIButton *)btn{
[self selBtn:btn];
//滚动到对应的界面的偏移量
// CGFloat offSetX = btn.tag * self.view.frame.size.width;
//设置中间滚动试图的偏移量
CenterScr.contentOffset = CGPointMake(btn.tag * self.view.frame.size.width, 0);
//添加对应自控制器的view到对应的位置
UIViewController *Vc = self.childViewControllers[btn.tag];
//设置自控制器view的位置
Vc.view.frame = CGRectMake(btn.tag * self.view.frame.size.width, 0, self.view.frame.size.width, self.view.frame.size.height);
[CenterScr addSubview:Vc.view];
}
//点击按钮切换颜色
- (void)selBtn:(UIButton *)btn{
//点击按钮切换颜色
_selBtn.selected = NO;
btn.selected = YES;
_selBtn = btn;
}
//添加所以子控制器
- (void)setUpVc{
//创建热点控制器
oneViewController *one = [oneViewController new];
one.title = @"男装";
//添加到当前控制器
[self addChildViewController:one];
//创建热点控制器
twoViewController *two = [twoViewController new];
two.title = @"数码家电";
//添加到当前控制器
[self addChildViewController:two];
//创建热点控制器
threeViewController *three = [threeViewController new];
three.title = @"文娱运动";
//添加到当前控制器
[self addChildViewController:three];
//创建热点控制器
fourViewController *four = [fourViewController new];
four.title = @"内衣";
//添加到当前控制器
[self addChildViewController:four];
//创建热点控制器
fiveViewController *five = [fiveViewController new];
five.title = @"居家用品";
//添加到当前控制器
[self addChildViewController:five];
}
@end