標籤:
一、導航控制力器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(@"右按鈕");}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];}二、 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];
}
#在藍懿學習iOS的日子#Day13