首先, 先創建三個VC.
完成點擊按鈕, 進入下一頁, 並能夠返回.
要先把導航視圖控制器創建出來.
在AppDelegate.m 文件中代碼如下:
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
//先創建一個ViewController
MainViewController *mainVC = [[MainViewController alloc] init];
//創建導航視圖控制器
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController = navC;
//釋放
[mainVC release];
[navC release];
return YES;
}
對於導航視圖控制器的一些設置.
//導航視圖控制器的高度是44,上面的狀態欄高度是20,加在一起默認是64;
// 加上一個標題.
self.title = @"貓眼電影";
// 對外觀進行設置,不是所有的顏色都是BackgroundColor;
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
// 創建一個UIView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
view.backgroundColor = [UIColor orangeColor];
[self.view addSubview:view];
[view release];
// 爲了防止座標系被篡改,我們把bar從半透明改爲全透明.這樣座標系的原點會自動向下推64
self.navigationController.navigationBar.translucent = NO;
內容方面的設置
//第二種標題的設置
self.navigationItem.title = @"骨頭 商店";
// 指定一些視圖,作爲titleView
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"信息",@"通話"]];
self.navigationItem.titleView = seg;
[seg release];
創建左右兩個按鈕
//左按鈕
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemOrganize target:self action:@selector(leftAction:)] autorelease];
//右按鈕
//如此創建,原本拖拽的圖標的顏色是黃色,但這麼創建後,圖標是藍色的.
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"狗.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
// 下面方法圖標將會顯示黃色.
// 創建一個小button,把圖片裝上.
UIButton *Littlebutton = [UIButton buttonWithType:UIButtonTypeCustom];
Littlebutton.frame = CGRectMake(0, 0, 40, 40);
[Littlebutton setImage:[UIImage imageNamed:@"狗.png"] forState:UIControlStateNormal];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:Littlebutton];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(260, 530, 80, 40);
button.backgroundColor = [UIColor yellowColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.layer.cornerRadius = 10;
[button setTitle:@"下一頁" forState:UIControlStateNormal];
相應的, 左右兩個按鈕的點擊事件也要實現
- (void)rightAction:(UIBarButtonItem *)button{
}
- (void)leftAction:(UIBarButtonItem *)barButton{
}
點擊Button跳轉到下一頁.
用模態也可以跳轉 , 但是跳轉的頁面不再有導航視圖.
實現Button的點擊事件
其中我要完成從前向後傳值.
- (void)click:(UIButton *)button{
- // 用導航視圖控制器跳轉.
//1.先創建下一頁對象.
SecondViewController *secondVC = [[SecondViewController alloc] init];
//通過導航控制器找到
[self.navigationController pushViewController:secondVC animated:YES];
[secondVC release];
// 屬性傳值第二步
secondVC.number = 100;
secondVC.string = self.textField.text;
secondVC.arr = @[@"haha",@"啦啦"];
}
第二個頁面的 .h 中代碼:
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
// 屬性傳值第一步,在第二個頁面寫一條屬性.
@property(nonatomic, assign)NSInteger number;
//屬性傳值第二種: 針對TextField中的字符串寫一條屬性.
@property(nonatomic, copy)NSString *string;
//屬性傳值第三種: 傳一個數組
@property(nonatomic, retain)NSArray *arr;
@end
第二個頁面的 .m 中代碼:
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@property(nonatomic, retain)UILabel *label;
@end
@implementation SecondViewController
- (void)dealloc{
[_label release];
[_string release];
[_arr release];
[super dealloc];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
self.navigationController.navigationBar.barTintColor = [UIColor magentaColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(260, 530, 80, 40);
button.backgroundColor = [UIColor blueColor];
[self.view addSubview:button];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
button.layer.cornerRadius = 10;
[button setTitle:@"下一頁" forState:UIControlStateNormal];
//第三步:
//第一種:
NSLog(@"%ld",self.number);
//第二種:
self.label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
[self.view addSubview:self.label];
[_label release];
self.label.layer.borderWidth = 1;
//屬性傳值第三步:裏面的值賦給label
self.label.text = self.string;
// 第三種:
NSLog(@"%@",self.arr[1]);
}
- (void)click:(UIButton *)button{
ThirdViewController *thirdVC = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thirdVC animated:YES];
[thirdVC release];
}
屬性傳值. 傳NSIntger . NSString . NSArray