以上就是導航欄的效果,導航欄在項目中應用很廣泛,需要熟練掌握。
新建項目,選擇“Empty Application”,項目命名為:NavigationControllerTest
新建一個UIViewController視圖,命名為HomeViewConroller
修改AppDeledate.h和AppDolegate.m源代碼
思路: 將home"push到”navigationController中,再將navigationController.View 添加到window中
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
@property (strong, nonatomic) UIWindow *window;
@end
#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方法。
{
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的代碼:
{
[super viewDidLoad];
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle: @" 觸摸 " style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
self.navigationItem.leftBarButtonItem = leftBarBtn;
[leftBarBtn release];}
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @" 提示 " message: @" 左邊的BarButton被點擊! " delegate:self cancelButtonTitle: @" 確認 " otherButtonTitles:nil];
[alert show];
[alert release];
}
效果如下圖:
下面的和上面的原理一樣,還是看代碼吧,代碼勝於一切華麗的言語。
代碼如下:
{
[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];
//導航到另一個視圖后, 修改返回的按鈕的顯示文字,默認是當前視圖的導航標題,如“備忘錄”
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;
} }