一、导航控制力器NavigationControeller
在第一个 ViewController前添加一个NavigationControeller,新建第二个页面 SecondViewController1、、在 ViewController.m导入
#import "SecondViewController.h"
@implementation ViewController
- (IBAction)leftAction:(UIBarButtonItem *)sender {
NSLog(@"左按钮");
}
- (IBAction)clicked:(id)sender {
SecondViewController *vc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
//创建系统样式按钮
UIBarButtonItem *bbi1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightAction)];
//创建文字按钮
UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithTitle:@"右按钮" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction)];
// self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bbi1,bbi2, nil];
self.navigationItem.rightBarButtonItems = @[bbi1,bbi2];
}
-(void)rightAction{
NSLog(@"右按钮");
- (IBAction)leftAction:(UIBarButtonItem *)sender {
NSLog(@"左按钮");
}
- (IBAction)clicked:(id)sender {
SecondViewController *vc = [[SecondViewController alloc]init];
[self.navigationController pushViewController:vc animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
//创建系统样式按钮
UIBarButtonItem *bbi1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(rightAction)];
//创建文字按钮
UIBarButtonItem *bbi2 = [[UIBarButtonItem alloc]initWithTitle:@"右按钮" style:UIBarButtonItemStyleDone target:self action:@selector(rightAction)];
// self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:bbi1,bbi2, nil];
self.navigationItem.rightBarButtonItems = @[bbi1,bbi2];
}
-(void)rightAction{
NSLog(@"右按钮");
}
2、在 SecondViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"第二个页面";
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(backAction) userInfo:nil repeats:NO];
}
-(void)backAction{
//跳回上一个页面
[self.navigationController popViewControllerAnimated:YES];
[super viewDidLoad];
self.title = @"第二个页面";
[NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(backAction) userInfo:nil repeats:NO];
}
-(void)backAction{
//跳回上一个页面
[self.navigationController popViewControllerAnimated:YES];
}
二、 UITableView
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *names;
@end
@implementation ViewController
//控制tableView有几个区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//控制每个区有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.names.count;
}
//控制每行显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//去内存中找 有没有离开页面的cell 有得话 拿过来直接用 没有则为nil
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//如果没有拿到离开页面的cell则需要创建一个
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
NSLog(@"%ld-%ld",indexPath.section,indexPath.row);
}
NSString *name = self.names[indexPath.row];
cell.textLabel.text = name;
return cell;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.names = [NSMutableArray array];
[self.names addObject:@"刘德华"];
[self.names addObject:@"张学友"];
[self.names addObject:@"郭富城"];
UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)];
self.navigationItem.leftBarButtonItem = addItem;
}
-(void)addAction{
NSString *name = @"王";
[self.names addObject:name];
//在ViewDidLoad方法之后 修改数据源数组的话 需要让tableView重新加载
[self.tableView reloadData];
}