1、创建一个UIViewController子类
2、分配一个导航控制器,并给他的根视图指定一个自定义的视图控制器实例。
3、在自定义的视图控制器中,创建一个或两个按钮并将它们添加到视图的导航栏中。
4、构建回调例程,在用户点击按钮时候触发。
核心宏定义:--当传递标题和选择器时,这个宏返回一个正确初始化的自动释放栏按钮项,用于指定到一个导航项。
#define BARBUTTON(TITLE,SELECTOR) [[[UIBarButtonItem alloc] initWithWithTitle:TITLE style:UIBarbuttonItemStylePlain target:self action:SELECTOE] autorelease]
@implementation TestViewController
-(void)right:(id)sender{
showAlert(@"press right button");
}
-(void)left:(id)sender{showAlert(@"press leftbutton");}-(void)viewdidload{self.view = [[[NSBundle mainBundle] loadNibnamed:@"mainview" owner:self options:nil] lastOBject];self.navigationItem.rightBarButtonItem= BARBUTTON(@"rightTitle",@selector(right)); self.navigationItem.leftBarButtonItem= BARBUTTON(@"leftTitle",@selector(left));
@end
@implement AppDelegate
-(void)applicationDidFinishLaunching:()application{ UIWindow *window = [[UIWindow alloc] initwithfram:[[UIScreen mainscreen] bounds]]; UINavigationController *nav [[UINavigationController alloc] initWithRootController:[[TestViewController allco] init]]; [window addsubviw:nav]; [window makeKeyandvisiable]; }