在做一个比较大的工程的时候,最好是用MVC模型
因为每个页面都有导航,所以建一个BaseViewController页面继承UIViewController
1 #import <UIKit/UIKit.h> 2 #import "MyConnection.h" 3 #define WINSIZE [[UIScreen mainScreen] bounds].size 4 //宏定义 屏幕的大小 5 6 @interface BaseViewController : UIViewController 7 8 @end
每个页面也都包含TableView,所以建一个BaseTableViewController页面继承BaseViewController
1 #import "BaseViewController.h" 2 //协议写在.h中,为了子类继承的时候能够看到协议 3 @interface BaseTableViewController : BaseViewController <UITableViewDataSource,UITableViewDelegate> 4 5 @property (strong,nonatomic)UITableView *tableView; 6 @property (strong,nonatomic)NSMutableArray *dataArray; 7 8 9 @end
1 #import "BaseTableViewController.h" 2 3 @interface BaseTableViewController () 4 5 @end 6 7 @implementation BaseTableViewController 8 9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 10 { 11 self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 12 if (self) { 13 // Custom initialization 14 } 15 return self; 16 } 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 self.dataArray = [[NSMutableArray alloc] init]; 22 self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WINSIZE.width, WINSIZE.height-64-49)]; 23 self.tableView.delegate = self; 24 self.tableView.dataSource = self; 25 self.tableView.rowHeight = 100;//设置行高 26 [self.view addSubview:self.tableView]; 27 } 28 29 30 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 31 { 32 return self.dataArray.count; 33 } 34 35 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 36 { 37 return nil; 38 } 39 40 41 42 43 @end
在AppDelegate中添加导航,设置图标,设置导航的背景,字体属性
1 //导航背景,设置之后的所有tabBar上的导航都会得到应用 2 [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"顶部条背景"] forBarMetrics:UIBarMetricsDefault]; 3 //导航字体属性 4 5 [[UINavigationBar appearance] setTitleTextAttributes: 6 @{NSFontAttributeName:[UIFont systemFontOfSize:20],NSForegroundColorAttributeName:[UIColor whiteColor] 7 }]; 8 9 10 11 12 for(int i = 0;i<array.count;i++){ 13 //遍历页面 14 //添加导航 15 UIViewController *vc = array[i]; 16 //设置标题 17 vc.title = titleArray[i]; 18 UINavigationController *nc = [[UINavigationController alloc] initWithRootViewController:vc]; 19 [array replaceObjectAtIndex:i withObject:nc];//数组变成导航 20 21 //设置图标(正常情况下和点击的情况下) 22 vc.tabBarItem.image = [UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_正常",titleArray[i]]]; 23 vc.tabBarItem.selectedImage = [[UIImage imageNamed:[NSString stringWithFormat:@"btn_%@_点击",titleArray[i]]] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];//选中图标样式,修改渲染模式 24 25 } 26 UITabBarController *tc = [[UITabBarController alloc] init]; 27 //90 187 37 28 tc.tabBar.tintColor = [UIColor colorWithRed:90/255.0 green:187/255.0 blue:37/255.0 alpha:1];//改变字体颜色 29 tc.viewControllers = array; 30 self.window.rootViewController = tc;
下面是将系统的请求封装起来,方便调用
1 #import <Foundation/Foundation.h> 2 //block 3 typedef void (^FinishBlock) (NSData *data); 4 //typedef void(^) (NSData *data) FinishBlock;//与上面那句是同一个意思 5 typedef void (^FailedBlock)(); 6 7 /* 8 FinishBlock finishiBlock; 9 void(^finishiBlock)(NSData *); 10 FinishBlock = ^(NSData *data) 11 { 12 } 13 */ 14 15 16 17 18 @interface MyRequest : NSObject 19 //请求的地址 20 @property (copy,nonatomic)NSString *url; 21 //block必须用copy修饰 22 //完成的回调 23 @property (copy,nonatomic)FinishBlock finishBlock; 24 //失败的回调 25 @property (copy,nonatomic)FailedBlock failedBlock; 26 //开始请求 27 - (void)startRequest; 28 29 30 @end
1 #import "MyRequest.h" 2 3 @interface MyRequest()<NSURLConnectionDataDelegate> 4 { 5 NSMutableData *_mData; 6 } 7 8 @end 9 10 @implementation MyRequest 11 - (void)startRequest 12 { 13 _mData = [[NSMutableData alloc] init]; 14 //系统请求 15 NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.url]]; 16 [NSURLConnection connectionWithRequest:request delegate:self]; 17 } 18 19 //接收响应 20 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 21 { //菊花(网络旁) 22 [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 23 } 24 25 //接收响应体 26 - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 27 { 28 [_mData appendData:data]; 29 } 30 31 32 33 //请求成功 34 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 35 { 36 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 37 if(self.finishBlock){ 38 self.finishBlock(_mData); 39 } 40 } 41 42 43 //请求失败 44 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 45 { 46 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 47 if(self.failedBlock){ 48 self.failedBlock(); 49 } 50 } 51 52 53 @end
1 #import <Foundation/Foundation.h> 2 #import "MyRequest.h" 3 @interface MyConnection : NSObject 4 //类方法,外部可以通过类名调用 5 + (void)connectionWithUrl:(NSString *)url FinishBlock:(FinishBlock) finishBlock FailedBlock:(FailedBlock)failedBlock; 6 7 8 @end 9 10 11 12 13 14 15 #import "MyConnection.h" 16 17 @implementation MyConnection 18 19 + (void)connectionWithUrl:(NSString *)url FinishBlock:(FinishBlock) finishBlock FailedBlock:(FailedBlock)failedBlock 20 { 21 MyRequest *request = [[MyRequest alloc] init]; 22 request.url = url; 23 request.finishBlock = finishBlock;//外界传来的方法体 24 request.failedBlock = failedBlock;//外界传来的方法体 25 [request startRequest]; 26 } 27 28 @end