以上就是导航栏的效果,导航栏在项目中应用很广泛,需要熟练掌握。
新建项目,选择“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;
} }