以上就是導覽列的效果,導覽列在項目中應用很廣泛,需要熟練掌握。
建立項目,選擇“Empty Application”,項目命名為:NavigationControllerTest
建立一個UIViewController視圖,命名為HomeViewConroller
修改AppDeledate.h和AppDolegate.m原始碼
思路: 將home"push到”navigationController中,再將navigationController.View 添加到window中
// AppDelegate.m
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
@property (strong, nonatomic) UIWindow *window;
@end
// AppDelegate.m
#import "AppDelegate.h"
#import "HomeViewController.h"
@implementation AppDelegate
@synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.window.backgroundColor = [UIColor whiteColor];
navigationController = [[UINavigationController alloc] init];
HomeViewController *home = [[HomeViewController alloc] init];
home.title = @"備忘錄";
[navigationController pushViewController:home animated:NO];
[self.window addSubview:navigationController.view];
[home release];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[navigationController release];
[_window release];
[super dealloc];
}
@end
效果如:
上面的只是一個頁面,下面建立一個UIViewController視圖“SecondViewController”,使項目在兩個頁面間切換。
在 HomeViewController.xib上添加button “進入第二個視圖”
HomeViewController中添加
- (IBAction)displaySecondView:(id)sender方法。
- (void)displaySecondView:(id)sender
{
SecondViewController *secondViewConroller = [[SecondViewController alloc] init];
//向視圖詢問它的導航控制器,因為在AppDelegate.m中我們已經在navigationController中添加了home,
//所以這裡我們詢問home的導航控制器就會返回先前navigationController指標,如果沒有就返回空
[self.navigationController pushViewController:secondViewConroller animated:YES];
secondViewConroller.title = @"第二個視圖";
[secondViewConroller release];
}
效果如下:
當切換到SencondViewController,導覽列自動顯示返回第一個視圖的按鈕。
在導覽列上實現左右兩個按鈕。
這個任務主要由UINavigationController上面的UINavigationItem來實現。
在 UINavigationItem上添加UIBarButtonItem。
在HomeViewController.m中修改- (void)viewDidLoad的代碼:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"觸摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
self.navigationItem.leftBarButtonItem = leftBarBtn;
[leftBarBtn release];}
- (void)leftBarBtnClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左邊的BarButton被點擊!" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert show];
[alert release];
}
效果如:
還有一些按鈕按鈕和行為,系統已經幫我們定義好了,Edit和Done按鈕行為,我們只要實現它就好了 。
下面的和上面的原理一樣,還是看代碼吧,代碼勝於一切華麗的言語。
代碼如下:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"觸摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
self.navigationItem.leftBarButtonItem = leftBarBtn;
[leftBarBtn release];
UIBarButtonItem *addBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBarBtnClicked:)];
self.navigationItem.rightBarButtonItem = addBarBtn;
[addBarBtn release];
self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSArray *segmentButtons = [NSArray arrayWithObjects:@"升序", @"降序", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentButtons];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
//導航到另一個視圖後,修改返回的按鈕的顯示文字,預設是當前視圖的導航標題,如“備忘錄” UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backBarBtn;
[backBarBtn release];
}
- (void)leftBarBtnClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左邊的BarButton被點擊!" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)addBarBtnClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"右邊的addBarButton被點擊!" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert show];
[alert release];
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
if (editing)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"edit" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert show];
[alert release];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"done" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert show];
[alert release];
}
}
- (void)segmentAction:(id)sender
{
switch ([sender selectedSegmentIndex])
{
case 0:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"升序" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert show];
[alert release];
break;
}
case 1:
{
UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"降序" delegate:self cancelButtonTitle:@"確認" otherButtonTitles:nil];
[alert1 show];
[alert1 release];
break;
}
default:
break;
} }