[课堂实践与项目]使用NavigationController建立多视图

1.首先,我们需要问问自己,我们在什么时候建立导航视图?

我就第一个视图就是到航视图的情况进行说明.

先看看我们的代理文件

.h

//
//  LCAppDelegate.h
//  navigationController
//
//  Created by lichan on 13-12-10.
//  Copyright (c) 2013年 com.lichan. All rights reserved.
//

#import <UIKit/UIKit.h>

//@class LCRootViewController;
@interface LCAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong,nonatomic)UINavigationController *navigationController;

//@property (strong,nonatomic)LCRootViewController *viewController;



@end
有朋友在问,为什么你的属性 是新建立的导航控制器,而不是你继承的?
由于我想利用我建立的 rootVieController进行设置导航.当然,你可以使用你继承导航的vc进行设置成window的根视图

#import "LCAppDelegate.h"

#import "LCRootViewController.h"

@implementation LCAppDelegate


#pragma mark 1: 将UiviewController 作为导航控制器来使用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    LCRootViewController *rootView = [[LCRootViewController alloc]init];
    rootView.title = @"Root View";
    
    self.navigationController = [[UINavigationController alloc]init];
    [self.navigationController pushViewController:rootView animated:YES];
    
    [self.window addSubview:self.navigationController.view];
    
    
    [self.window makeKeyAndVisible];
    return YES;
}
为什么这样做呢?  因为我们有的时候在加载最初视图的时候不会使用导航,所以我才有意写成这样.

大致的思路就是:先建立我们想初始化的viewController,然后通过我们的导航控制器进行Push.于是viewController 就成了我们的初始页面.最后把导航加载到window的rootController中.就完成了第一步的操作.

效果如下:



2.如何在导航视图中添加按钮呢?

看看我们的rootController.m文件

 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(pressedTheLeftBarItem)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(pressedTheAddItem)];
    


 - (void)pressedTheLeftBarItem
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你点击了左边的Action Item" delegate:self cancelButtonTitle:@"知道啦~" otherButtonTitles:@"取消", nil];
    
    [alertView show];
    

}

- (void)pressedTheAddItem
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你点击了右边的ADD Item" delegate:self cancelButtonTitle:@"知道啦~" otherButtonTitles:@"取消", nil];
    
    [alertView show];

}

由于系统会给我们的一些系统的图标,我们也可以使用自己的image作为图标.




3.讲UISegementControl添加到导航中去

//如何在navigationBar上面添加一个  segementControl
    
    NSArray *array = [NSArray arrayWithObjects:@"好友",@"群",@"最近联系人", nil];
    
    UISegmentedControl *segmentControl = [[UISegmentedControl alloc]initWithItems:array];
    
    [segmentControl addTarget:self action:@selector(segmentedPressed:) forControlEvents:UIControlEventValueChanged];
    //切记这一行的forControlEvents的类型是UIControlEventValueChanged,而不是我们Button的  UIControlEventtouchedUpInsider
    self.navigationItem.titleView = segmentControl;

#pragma mark 3:UISegmentedControl 添加到 navigationItem 中去
- (void)segmentedPressed:(id)sender
{
    switch ([sender selectedSegmentIndex]) {
        case 0:
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你点击selectedSegmentIndex 为 0 的好友" delegate:self cancelButtonTitle:@"知道啦~" otherButtonTitles:@"取消", nil];
            
            [alertView show];
            break;
        }
        case 1:
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你点击selectedSegmentIndex 为 1 的群" delegate:self cancelButtonTitle:@"知道啦~" otherButtonTitles:@"取消", nil];
            
            [alertView show];
            break;
        }
        case 2:
        {
            UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你点击selectedSegmentIndex 为 2 的最近联系人" delegate:self cancelButtonTitle:@"知道啦~" otherButtonTitles:@"取消", nil];
            
            [alertView show];
            break;
        }
            
        default:
            break;
    }
    

}


4.如何跳转到下一个到航视图?

viewdidLoad文件

 UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(200, 200, 100, 20)];
    [button setTitle:@"去第二页" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];//如果不设置颜色,就看不到title~~~
    [button addTarget:self action:@selector(toSecondButtonPressed:) forControlEvents:UIControlEventTouchUpInside];

- (IBAction)toSecondButtonPressed:(id)sender
{
    LCSecondViewController *secondViewController = [[LCSecondViewController alloc]init];
    
    secondViewController.title  = @"Second View";
    
    [self.navigationController pushViewController:secondViewController animated:YES];
    
    
    

}


是不是很简单呢?

5.如何自定义 UINavigationController的title?

- (void)viewDidLoad
{
    [super viewDidLoad];
    //添加第三个view作为 子视图
    UIButton *toThirdButton = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 50, 10)];
    [toThirdButton setTitle:@"去第三个视图" forState:UIControlStateNormal];
    [toThirdButton sizeToFit];
    [toThirdButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [toThirdButton addTarget:self action:@selector(toThirdViewController) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:toThirdButton];
    
    //



6.如何在视图中添加 toolBar? 我在第二个视图做说明.

// 5:在视图中添加 toolBar  并添加按钮
    [self.navigationController  setToolbarHidden:NO animated:YES];
    
    UIBarButtonItem *friendsItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showFriends:)];
    UIBarButtonItem *gamesItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showFriends:)];
    UIBarButtonItem *addItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showFriends:)];
    UIBarButtonItem *zoneItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(showFriends:)];
     UIBarButtonItem *fixItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(showFriends:)];
    //UIBarButtonItem *fixItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:@selector(showFriends:)];
    
    NSArray *toolbarItem = [NSArray arrayWithObjects:friendsItem,fixItem, gamesItem,fixItem,addItem,fixItem,zoneItem, nil];
    
    self.toolbarItems = toolbarItem;
    //用   [self.navigationController.toolbar setItems:(NSArray *) animated:]这个方法添加item是不起效果的。下面我动态自己添加Toolbar时,这个才起效果。
    
	// Do any additional setup after loading the view.
}
#pragma mark 5:在视图中添加 toolBar

- (IBAction)showFriends:(id)sender
{
    UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"提示" message:@"你点击显示好友Item" delegate:self cancelButtonTitle:@"知道啦~" otherButtonTitles:@"取消", nil];
    
    [alertView show];

}



7.如何动态的添加 toolbar呢?

#pragma mark 6:动态添加 toolBar
//大致的意思是说:我们不使用系统带的那个toolbar了,让它一直隐藏着,我们自己建立一个新的自定义尺寸的toolbar,放在屏幕的下面
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self.navigationController setToolbarHidden:YES animated:YES];
    
    UIBarButtonItem *toButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(toFourButton)];
      
    myToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 460, 320, 20)];
    [myToolbar setBarStyle:UIBarStyleDefault];
    
    myToolbar.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    
    [myToolbar setItems:[NSArray arrayWithObjects:toButton, nil] animated:YES];
    
    [self.view addSubview:myToolbar];
	// Do any additional setup after loading the view.

}

- (void)toFourButton

{

    LCFourViewController *fourViewController = [[LCFourViewController alloc]init];

    fourViewController.title = @"Third view";

    [self.navigationController pushViewController:fourViewController animated:YES];

    

}


toolbar最左边只有一个 搜索图标,点击我进入了view4.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值