self.view.backgroundColor = [UIColor redColor];
// 導航欄設置: controller(欄)/item(欄上的元素)
// 導航欄顯示/隱藏
self.navigationController.navigationBarHidden = NO;
// ? ?self.navigationController.navigationBar.hidden = YES;
// 欄樣式
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// 半透明效果
// 開始效果時 屏幕左上角為坐標原點
// 關閉時 導航欄的左下角為坐標原點
self.navigationController.navigationBar.translucent = YES;
// 創建view(0,0,100,100)
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor greenColor];
[self.view addSubview:view];
[view release];
// 欄背景顏色
self.navigationController.navigationBar.backgroundColor = [UIColor yellowColor];
// 欄顏色
self.navigationController.navigationBar.barTintColor = [UIColor grayColor];
// 欄標題
self.title = @"這是一個標題";
// ? ?self.navigationItem.title = @"這是一個猴賽雷的標題";
// 分段
UISegmentedControl *seg = [[[UISegmentedControl alloc] initWithItems:@[@"消息", @"電話"]] autorelease];
seg.frame = CGRectMake(0, 0, 100, 30);
// 欄標題視圖
self.navigationItem.titleView = seg;
// 欄左側按鈕
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(left:)] autorelease];
// 欄右側按鈕
// 系統按鈕樣式
UIBarButtonItem *b1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(right1)] autorelease];
// 自定義按鈕圖片
UIBarButtonItem *b2 = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"dianzan"] style:UIBarButtonItemStylePlain target:self action:@selector(right2)] autorelease];
self.navigationItem.rightBarButtonItems = @[b1, b2];
// 修改導航欄上內容的顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
// 跳轉頁面
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
btn.frame = CGRectMake(200, 200, 100, 100);
btn.backgroundColor = [UIColor yellowColor];
[self.view addSubview:btn];
[btn addTarget:self action:@selector(goTwo) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 跳轉頁面
- (void)goTwo
{
// 1.獲取第二頁對象
TwoViewController *twoVC = [[TwoViewController alloc] init];
// 2.跳轉(由導航控制器 從當前push到第二頁)
[self.navigationController pushViewController:twoVC animated:YES];
// 3.內存管理
[twoVC release];
}
- (void)right1
{
NSLog(@"右1");
}
- (void)right2
{
NSLog(@"右2");
}
#pragma mark - 左按鈕觸發方法
- (void)left:(UIBarButtonItem *)left
{
NSLog(@"左點點");
}
界面間 傳值
#import"RootViewController.h"
#import"TwoViewController.h"
#warning協議4:簽協議
@interfaceRootViewController ()
@property(nonatomic,retain)UITextField*table;
@end
@implementationRootViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor= [UIColorwhiteColor];
self.navigationController.navigationBarHidden=NO;
self.navigationController.navigationBar.translucent=NO;
// self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
self.title=@"首頁";
self.table= [[UITextFieldalloc]initWithFrame:CGRectMake(60,30,260,40)];
self.table.backgroundColor= [UIColorwhiteColor];
[self.viewaddSubview:self.table];
[self.tablerelease];
self.table.layer.borderColor= [UIColorlightGrayColor].CGColor;
self.table.layer.borderWidth=1;
self.table.layer.cornerRadius=5;
UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn.frame=CGRectMake(60,120,260,40);
btn.backgroundColor= [UIColorredColor];
[self.viewaddSubview:btn];
[btn addTarget:selfaction:@selector(goFor) forControlEvents:UIControlEventTouchUpInside];
}
-(void)goFor{
TwoViewController*two = [[TwoViewControlleralloc]init];
#warning屬性2:在push頁面之前傳值(創建對象之后push之前)
two.string=self.table.text;
#warning協議5:設置代理人
//為了保證設置代理人的對象和設置push對象是同一個在創建對象之后push之前設置delegate
two.delegate=self;
[self.navigationControllerpushViewController:twoanimated:YES];
[tworelease];
}
#warning協議6:實現協議方法
-(void)passValue:(NSString*)string{
//把收到的string賦值給輸入框
self.table.text= string;
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
*******************************************************************************
//
// ?TwoViewController.h
#import
#warning協議1:聲明協議(定義一個帶參數的方法)
@protocolPassDelegate
//必須要實現的@required(默認)
//@optional可選的
-(void)passValue:(NSString*)string;//需要傳什么數據就設置什么屬性
@end
@interfaceTwoViewController :UIViewController
#warning屬性1:在第二頁聲明一個屬性用來保存數據
@property(nonatomic,copy)NSString*string;
#warning協議2:定義代理人屬性
@property(nonatomic,assign)iddelegate;
@end
*********************************************
TwoViewController.m
#import"TwoViewController.h"
@interfaceTwoViewController()
@property(nonatomic,retain)UITextField*table2;
@end
@implementationTwoViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor= [UIColorwhiteColor];
self.title=@"第二頁";
self.table2= [[UITextFieldalloc]initWithFrame:CGRectMake(60,30,260,40)];
self.table2.backgroundColor= [UIColorwhiteColor];
[self.viewaddSubview:self.table2];
[self.table2release];
self.table2.layer.borderColor= [UIColorlightGrayColor].CGColor;
self.table2.layer.borderWidth=1;
self.table2.layer.cornerRadius=5;
UIButton*btn = [UIButtonbuttonWithType:UIButtonTypeSystem];
btn.frame=CGRectMake(60,120,260,40);
btn.backgroundColor= [UIColorredColor];
[self.viewaddSubview:btn];
[btnaddTarget:selfaction:@selector(goBack)forControlEvents:UIControlEventTouchUpInside];
#warning屬性3:通過屬性給當前頁面賦值
self.table2.text=self.string;
}
-(void)goBack{
#warning協議3:返回上一頁之前代理人調用協議方法
[self.delegatepassValue:self.table2.text];//self.delegate相當于rootVC頁面執行pass方法
[self.navigationControllerpopToRootViewControllerAnimated:YES];
}