您的位置:首页 > 理论基础 > 计算机网络

xib,AFN的网络请求,判断网络状态

2016-01-13 16:38 399 查看
使用方法和storyboard基本相同,但是只能创建view,tableViewcell可以直接铺,省去计算坐标的过程,比较省事

创建xib文件的方式

1.在创建文件的时候,选择also create xib file,就会自动创建一个xib文件,文件和xib和关联好了,直接就可以用

2.需要在empty里创建一个xib文件,然后指定files owner,像文件里拖拽一个view,指定owner的self.view是哪一个view

注: 下面代码都是以前见过的可以不看,只有RootViewController.m里加了网络请求和判断网络状态是新的知识点

AppDelegate.m

创建tabbar

//boudel如果写成nil,就是默认路径,默认路径是[NSBundle mainBundle]
RootViewController *rootVC=[[RootViewController alloc] initWithNibName:NSStringFromClass([RootViewController class]) bundle:nil];
UINavigationController *naVC=[[UINavigationController alloc] initWithRootViewController:rootVC];
naVC.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:1000];
MovieViewController *movieVC=[[MovieViewController alloc] initWithNibName:NSStringFromClass([MovieViewController class]) bundle:nil];
UINavigationController *movieNAVC=[[UINavigationController alloc] initWithRootViewController:movieVC];
movieVC.tabBarItem=[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMore tag:1001];

UITabBarController *tab=[[UITabBarController alloc] init];
tab.viewControllers=@[naVC,movieNAVC];
self.window.rootViewController=tab;


RootViewController.m

#import "RootViewController.h"
#import "SecViewController.h"
#import "AFNetworking.h"

@interface RootViewController ()<SecViewControllerDeleage>
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
- (IBAction)buttonAction:(id)sender;

@end

@implementation RootViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title=@"第一页";
NSArray *arr=[[NSBundle mainBundle] loadNibNamed:@"MyView" owner:nil options:nil];
NSLog(@"%@",arr);
UILabel *label=arr[0];
label.center=CGPointMake(200, 400);
[self.view addSubview:label];

//****************请求**************
//    AFHTTPSessionManager *manager=[AFHTTPSessionManager manager];
//    [manager GET:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@"%@",responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@"error=%@",error);
//    }];

//************判断网络状况**************
//    [[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
//        NSLog(@"%ld",status);
//
//    }];
//    [[AFNetworkReachabilityManager sharedManager] startMonitoring];

- (IBAction)buttonAction:(id)sender {

self.view.backgroundColor=[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1];
SecViewController *secVC=[[SecViewController alloc] initWithNibName:NSStringFromClass([SecViewController class]) bundle:nil];
[self.navigationController pushViewController:secVC animated:YES];
secVC.str=self.nameTextField.text;
secVC.deleage=self;

}

-(void)bringStr:(NSString *)str{
NSLog(@"%@",str);
}
@end


SecViewController.h

#import <UIKit/UIKit.h>

@protocol SecViewControllerDeleage <NSObject>

-(void)bringStr:(NSString *)str;

@end

@interface SecViewController : UIViewController

@property(nonatomic,copy)NSString *str;

@property(nonatomic,assign)id<SecViewControllerDeleage>deleage;

@end


SecViewController.m

#import "SecViewController.h"

@interface SecViewController ()
- (IBAction)backAction:(id)sender;

@end

@implementation SecViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.

NSLog(@"%@",self.str);

}

- (IBAction)backAction:(id)sender {

[self.navigationController popToRootViewControllerAnimated:YES];
[self.deleage bringStr:@"1111111"];

}
@end


MovieViewController.m

#import "MovieViewController.h"
#import "MovieCell.h"
#import "UIImageView+WebCache.h"

@interface MovieViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *arr;

@end

@implementation MovieViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
//    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"reuse"];

UINib *nib=[UINib nibWithNibName:NSStringFromClass([MovieCell class]) bundle:nil];
//在文件里找到当前需要注册的cell
[self.myTableView registerNib:nib forCellReuseIdentifier:@"MovieCell"];

[self createData];
}

-(void)createData{
NSString *strUrl=[NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php"];
NSURL *url=[NSURL URLWithString:strUrl];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
NSURLSession *session=[NSURLSession sharedSession];
NSURLSessionTask *task=[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
self.arr=dic[@"result"];
[self.myTableView reloadData];
});

}];
[task resume];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.arr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
MovieCell *cell=[tableView dequeueReusableCellWithIdentifier:@"MovieCell"forIndexPath:indexPath];
cell.myLabel.text=self.arr[indexPath.row][@"movieName"];
[ cell.myImageView sd_setImageWithURL:self.arr[indexPath.row][@"pic_url"] ];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"1121");
}


MovieCell.h

@property (weak, nonatomic) IBOutlet UILabel *myLabel;
@property (weak, nonatomic) IBOutlet UIImageView *myImageView;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: