UINavigationController

做点记录,虽然东西比较简单,但好记性不如记博客嘛。

AppDelegate.h中写如下代码

#import <UIKit/UIKit.h>

@class ViewController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController *_navController;
}

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) ViewController *viewController;
//
@property (strong, nonatomic) UINavigationController *navController;//add by XiaoT

@end

AppDelegate.m中加入如下代码

@synthesize  navController =  _navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //modify by chen tao to chang the root view for test navigationcontroller.
    
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    RootViewController *rootViewCtl = [[RootViewController alloc] init];
    rootViewCtl.title = @"Root viwe";
    
    self.navController = [[UINavigationController alloc] initWithRootViewController:rootViewCtl];
    [rootViewCtl release];
//
    [self.window addSubview: self.navController.view];
    [self.navController release];
    [self.window makeKeyAndVisible];
    return YES;
}

添加RootViewController类,继承自UIViewController.

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //self.title = @"Root View";
    [self.view setBackgroundColor:[UIColor colorWithRed:0.2 green:0.3 blue:0.5 alpha:0.9 ]];
      //
     UIBarButtonItem *buttonItm = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(gotoSecondPage:)];
     self.navigationItem.rightBarButtonItem  = buttonItm;    //添加UISegmentdControl(分段控件).
    NSArray *array = [NSArray arrayWithObjects:@"test1", @"test2",nil];
    UISegmentedControl *segCtrl = [[UISegmentedControl alloc] initWithItems:array];
    segCtrl.segmentedControlStyle = UISegmentedControlSegmentCenter;
    [segCtrl addTarget:self action:@selector(tesx:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segCtrl;
    
    //自定义回退按钮
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根视图" style:UIBarButtonItemStyleDone target:nil action:nil];
    self.navigationItem.backBarButtonItem = backButton;
    [backButton release];
    //添加Toolbar
    UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
    
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self setToolbarItems:[NSArray arrayWithObjects: one, flexItem, two, nil]];
    //
    [self.navigationController setToolbarHidden:NO animated:YES];
}
//UISegmentedControl点击消息
-(void)tesx:(id)sender { int iIndex = [sender selectedSegmentIndex]; }
//push到SecondViewController
-(void)gotoSecondPage:(id) sender { SecondViewController *sencViewCtrl = [[SecondViewController alloc] init]; [self.navigationController pushViewController:sencViewCtrl animated:YES]; [sencViewCtrl release]; }

加入SecondViewController

BOOL bHidden = NO;
-(void)test:(id)sender
{
    
    [self.navigationController setToolbarHidden:bHidden animated:YES];
    bHidden = !bHidden;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"Second View";
    [self.view setBackgroundColor: [UIColor colorWithRed:0.1 green:0.2 blue:0.4 alpha:0.9]];
    UIView  *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [view setBackgroundColor:[UIColor colorWithWhite:0.8 alpha:1]];
   // self.navigationItem.titleView = view;//自定义navigationItem titleView窗口
    [self.navigationController setToolbarHidden:bHidden animated:YES];
    //
    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.titleLabel.text = @"hidden toolbar";
    [button addTarget:self action:@selector(test:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(50, 80, 60, 30);
    [self.view addSubview: button];
    //ToolbarItems
    UIBarButtonItem *one = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:nil action:nil];
    UIBarButtonItem *two = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:nil action:nil];
    UIBarButtonItem *three = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:nil action:nil];
    UIBarButtonItem *four = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:nil action:nil];
    UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    [self setToolbarItems:[NSArray arrayWithObjects:flexItem, one, flexItem, two, flexItem, three, flexItem, four, flexItem, nil]];
}

 

 还有个网上比较好的介绍:http://blog.sina.com.cn/s/blog_5fb39f910101a7zc.html

posted @ 2013-04-12 16:41  酱酱爱  阅读(299)  评论(0编辑  收藏  举报