新建一个Single View的iOS工程,工程名:TestNaviTabbar
可以删掉ViewController类,暂时不能删掉Main.storyBoard(原因还没有弄清楚)
工程主要使用Xib文件+UIViewController(UITableViewDelegate,UITableViewDataSource),以代码的方式来组织这些界面。
步骤1
添加4个ios CocoaTouchClass文件,都继承自UIViewController,都勾选Also create XIB file
FirstViewController
SecondViewController
DisplaySettingViewController
PopUpViewController
步骤2
添加Tab页
//AppDelegate.h
#import <UIKit/UIKit.h>
@class FirstViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong,nonatomic) FirstViewController* firstViewController;
@end
//AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
self.firstViewController.title =@"记录";
self.firstViewController.view.backgroundColor = [UIColor yellowColor];
self.window.rootViewController = self.firstViewController;
[self.window makeKeyAndVisible];
return YES;
}
这是后可以尝试运行一下,如果成功,那么继续修改这个函数:
(之所以将一个步骤分成两个,是为了更好的理解这些代码)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstViewController" bundle:nil];
self.firstViewController.title =@"记录";
self.firstViewController.view.backgroundColor = [UIColor yellowColor];
UINavigationController* firstNC = [[UINavigationController alloc] initWithRootViewController:self.firstViewController];
SecondViewController* secondVC = [[SecondViewController alloc]initWithNibName:@"SecondViewController" bundle:nil];
secondVC.title =@"功能";
secondVC.view.backgroundColor = [UIColor yellowColor];
UINavigationController* secondNC = [[UINavigationController alloc] initWithRootViewController:secondVC];
UITabBarController* tbc = [[UITabBarController alloc]init];
tbc.viewControllers = [NSArray arrayWithObjects:firstNC,secondNC,nil];
self.window.rootViewController = tbc;
[self.window makeKeyAndVisible];
return YES;
}
//FirstViewController.h
//FirstViewController.m
//DisplaySettingViewController.m
#import <UIKit/UIKit.h>
@interface FirstViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)IBOutletUITableView *tableRecords;
@end
//FirstViewController.m
#import "FirstViewController.h"
#import "DisplaySettingViewController.h"
#import "PopUpViewController.h"
@interface FirstViewController ()
@end
NSMutableArray* firstCellContent;
@implementation FirstViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
firstCellContent = [NSMutableArrayarrayWithObjects:@" +点击添加新纪录",@"记录1",@"记录2",nil];
UIBarButtonItem* btn = [[UIBarButtonItemalloc]initWithTitle:@"排版"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(btnClick)];
self.navigationItem.rightBarButtonItem = btn;
self.tableRecords.dataSource =self;
self.tableRecords.delegate =self;
}
-(void)btnClick
{
DisplaySettingViewController* dsvc = [[DisplaySettingViewControlleralloc]initWithNibName:@"DisplaySettingViewController"bundle:nil];
//svc.view.backgroundColor = [UIColor redColor];
dsvc.title = @"显示排版";
self.hidesBottomBarWhenPushed =YES;
[self.navigationControllerpushViewController:dsvcanimated:YES];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell =[tableViewdequeueReusableCellWithIdentifier:@"cellId"];
if(cell == nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cellId"];
}
NSUInteger row = indexPath.row;
cell.textLabel.text = [firstCellContentobjectAtIndex:row];
cell.layer.cornerRadius =12;
cell.layer.masksToBounds =YES;
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returnfirstCellContent.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PopUpViewController* pvc = [[PopUpViewControlleralloc]init];
//svc.view.backgroundColor = [UIColor redColor];
pvc.title = @"页面详细";
self.hidesBottomBarWhenPushed =YES;
[self.navigationControllerpushViewController:pvcanimated:YES];
}
-(void) viewDidDisappear:(BOOL)animated
{
self.hidesBottomBarWhenPushed =NO;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
步骤4
上面的类用到了PopUpViewController
//DisplaySettingViewController.h
#import <UIKit/UIKit.h>
@interface DisplaySettingViewController :UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (strong,nonatomic)IBOutletUITableView *tableDisplayStyle;
@end
//DisplaySettingViewController.m
#import "DisplaySettingViewController.h"
@interface DisplaySettingViewController ()
@end
@implementation DisplaySettingViewController
NSMutableArray* displayCellContent;
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
self.tableDisplayStyle.delegate =self;
self.tableDisplayStyle.dataSource =self;
displayCellContent = [NSMutableArrayarrayWithObjects:@"展开所有记录",@"仅展开一个星期的记录",@"仅展开两个星期的记录",@"仅展开一个月的记录",nil];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableViewdequeueReusableCellWithIdentifier:@"cellId"];
if(cell == nil)
{
cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cellId"];
}
NSUInteger row = indexPath.row;
cell.textLabel.text = [displayCellContentobjectAtIndex:row];
cell.layer.cornerRadius = 12;
cell.layer.masksToBounds =YES;
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
returndisplayCellContent.count;
}
-(void)viewDidDisappear:(BOOL)animated
{
self.hidesBottomBarWhenPushed =NO;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
步骤5:
//SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableFunction;
@end
// SecondViewController.m
#import "SecondViewController.h"
#import "PopUpViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
NSMutableArray* secondCellContent;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.tableFunction.dataSource = self;
self.tableFunction.delegate = self;
secondCellContent = [NSMutableArray arrayWithObjects:@"对应表",@"通用功能",@"关于",nil];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"cellId"];
if(cell == nil)
{
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellId"];
}
NSUInteger row = indexPath.row;
cell.textLabel.text = [secondCellContent objectAtIndex:row];
cell.layer.cornerRadius = 12;
cell.layer.masksToBounds = YES;
//cell.detailTextLabel.text = @"detail";
//return nil;
return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return secondCellContent.count;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
PopUpViewController* pvc = [[PopUpViewController alloc] init];
//svc.view.backgroundColor = [UIColor redColor];
pvc.title = @"页面详细";
self.hidesBottomBarWhenPushed = YES;
[self.navigationController pushViewController:pvc animated:YES];
}
-(void)viewDidDisappear:(BOOL)animated
{
self.hidesBottomBarWhenPushed = NO;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
步骤6:
给PupUpViewControler.xib添加一个label,Label改名为“详细信息”
Demo运行的图片: