您的位置:首页 > 产品设计 > UI/UE

6.4 UINavigationController

2015-06-05 09:07 501 查看
1、数据结构中,栈的工作原理,进栈操作和出栈操作

栈,

先进后出.

//1 ,设置window
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    //2, 配置根控制器
    ViewController *detailvc = [[ViewController alloc]init];
    //导航控制器:利用栈的结构,本身不做呈现,管理栈的元素:栈,先进后出
    //导航控制器:导航栏默认高度:64 
    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:detailvc];
    self.window.rootViewController = nav;


2、UINavigationController的简介和基本原理

#pragma mark - 配置导航栏标题,颜色字体阴影
    NSShadow *shadow = [[NSShadow alloc]init];
    shadow.shadowOffset = CGSizeMake(5, 5);

    NSDictionary *arribute =
    @{
    //设置字体属性
      NSFontAttributeName:[UIFont systemFontOfSize:10],
    // 设置颜色
      NSForegroundColorAttributeName:[UIColor redColor],
    // 设置阴影
      NSShadowAttributeName:shadow
      };
    //设置文本属性字典
    nav.navigationBar.titleTextAttributes =arribute;

#pragma mark - 配置色调
    nav.navigationBar.tintColor = [UIColor purpleColor];
    nav.navigationBar.barTintColor = [UIColor brownColor];
    return YES;


3、UINavigationController的层次结构

#pragma mark - 配置导航栏按钮
    //导航栏上刷新按钮
    UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(handleBarButtonEvent:)];

    UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(handleBarButtonEvent:)];

    //设置标签值
    shareItem.tag = 100;
    //点击分享事件按钮的时候
    self.navigationItem.rightBarButtonItems =@[refreshItem,shareItem];


#pragma mark - 配置工具栏
    //默认情况,工具栏是隐藏的
    self.navigationController.toolbarHidden = NO;
    // 设置按钮
    UIBarButtonItem *Item1 = [[UIBarButtonItem alloc]initWithTitle:@"刷新" style:UIBarButtonItemStylePlain target:nil action:nil];

    UIBarButtonItem *Item2 = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:nil action:nil];
    //弹性控件
    UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];

    //进行按钮关联
    self.toolbarItems =@[flexibleItem,Item1,
                         flexibleItem,Item2,
                         flexibleItem];


4、如何通过UINavigationController推送到另一个控制器中(不同于模态视图推送)

    //添加点击事件
button1.tag = 50;
button2.tag = 51;

[button1 addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
[button2 addTarget:self action:@selector(handleButtonEvent:) forControlEvents:UIControlEventTouchUpInside];
#pragma mark - 配置导航栏按钮 //导航栏上刷新按钮 UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(handleBarButtonEvent:)]; UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithTitle:@"分享" style:UIBarButtonItemStylePlain target:self action:@selector(handleBarButtonEvent:)]; //设置标签值 shareItem.tag = 100; //点击分享事件按钮的时候 self.navigationItem.rightBarButtonItems =@[refreshItem,shareItem];


- (void)handleBarButtonEvent:(UIBarButtonItem *)sender
{
    if (sender.tag == 100) {
        //分享按钮点击的时候
        UIActionSheet   *sheet = [[UIActionSheet alloc]initWithTitle:@"分享" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"新浪" otherButtonTitles:@"腾讯", nil];
        [sheet showInView:self.view];
    }

}


//界面推送
- (void)handleButtonEvent:(UIBarButtonItem *)sender;
{
    DetailViewController *detailVC =[[DetailViewController alloc]init];

    if (sender.tag == 50) {
            //模态推送界面 ,由下往上推送
        [self presentViewController:detailVC animated:YES completion:nil];
    } else {
        //导航推送
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}


5、如何在UINavigationController管理的控制器之间进行传值操作?

UINavigationController的重要作用,页面的管理和切换。
1、RootView 跳到SecondView
首先我们需要新一个View。新建SecondView,按住Command键然后按N,弹出新建页面,我们新建SecondView

2、为Button 添加点击事件,实现跳转
在RootViewController.xib中和RootViewController.h文件建立连接

在RootViewController.m中实现代码,alloc一个SecondViewController,用pushViewController到navigationController中去,并为
SecondViewController这是title为    secondView.title =@"Second View"; 默认情况下,titie为下个页面返回按钮的名字。

- (IBAction)gotoSecondView:(id)sender {  
    SecondViewController *secondView = [[SecondViewController alloc] init];  
    [self.navigationController pushViewController:secondView animated:YES];  
    secondView.title = @"Second View";  
}  
这是点击GotoSecondView 按钮,出现

这就是SecondView了。

栈的方式管理?
就是先进后出的管理方式,通过出栈和入栈来展示各个视图控制器
UINavigationController的ContentView里始终显示栈顶控制器的view
viewControllers属性存储了栈中的所有被管理的控制器
navigationController属性,父类中的属性,每个在栈中的控制器,都能通过此属性,获取自己所在的UINavigationController对象
至少要有一个被管理的视图控制器,需要一个根控制器,任何一个继承自UIViewControl的类(多态)都可以作为跟控制器
多态:同一个事件,不同对象响应这个事件的方法是不同的

pushViewController:animated  进入下一个视图控制器,即即将推入的视图控制器
popViewControllerAnimated:   返回上一个视图控制器
popToViewController:animated  返回到指定的视图控制器
popToRootViewControllerAnimated   返回到根视图控制器

常用属性
viewControllers 所有处于栈中的控制器
topViewController 位于栈顶的控制器
visibleViewController 当前正在显示的控制器
navigation 导航条

定制UINavigationBar
首页属于UIBarButtonItem - UIBarItem-NSObject  而不继承于UIView

两种传值方式:
属性传值:(A传给B)
把A控制器导航栏输入框的字符串值传递给B页面控制器导航栏
第一步:
在B控制器的.h文件中声明输入框的字符串属性
// 第一步  
// 声明字符串属性
@property (nonatomic, copy) NSString *titleStr;     // 传值属性
第二步:
在A控制器中声明B控制器的h文件
在A控制器的按钮切换方法中让输入框的值赋给B控制器的字符串属性值
// 把当前页面输入框的值给了firstVC.titleStr属性
    _textField.text = firstVC.titleStr;
第三步:
在B控制器中让输入框接收字符串属性中接收过来的A导航控制器的输入框的值
// 把传递过来的值赋到输入框中
    _textField.text = _titleStr;

代理传值:(B传给A)
第一步:
在B文件的h文件中创建B文件的协议,并创建发送字符串的方法方法
// 第一步 创建协议
@protocol JYFFirstViewControllerDelegate <<span style="color: #703daa">NSObject>
- (void)sendValue:(NSString *)str;
@end
第二步:
创建id类型的并遵守协议的delegate属性
//   第二步 声明代理属性
@property (nonatomic, assign) id<</span>JYFFirstViewControllerDelegate> delegate;
第三步:
在B文件的跳转按钮中实现delegate对发送字符串方法的调用以获得本文件输入框中的内容
- (void)buttonAction:(UIButton *)sender
{
//    JYFSecondViewController *secondVC = [[JYFSecondViewController alloc] init];
//    [self.navigationController pushViewController:secondVC animated:YES];
//    [secondVC release];

    //  第三步 让代理发送协议中的方法
    [_delegate sendValue:_textField.text];
    // 返回上一视图
    [self.navigationController popToRootViewControllerAnimated:YES];
}
第四步:
在A文件的m文件中遵守B控制器的代理协议
@interface JYFRootViewController ()<</span>JYFFirstViewControllerDelegate> // 第四步 让此控制器遵守协议
第五步:
在A文件中新创建的B控制器后面,让A做B的代理
 // 1. 创建出要跳转的对象
    JYFFirstViewController *firstVC = [[JYFFirstViewController alloc] init];
    // 第五步  本控制器作为firstVC的代理
    firstVC.delegate = self;
第六步:
在A文件中实现协议中的方法
即接收B文件传过来的输入框的值
// 第六步 
#pragma mark - JYFFirstViewControllerDelegate Methods
- (void)sendValue:(NSString *)str
{
    // 将传过来的值设置到输入框中
    _textField.text = str;
}
 http://blog.sina.com.cn/s/blog_68d8771b0102v4if.html http://blog.csdn.net/totogo2010/article/details/7682433
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: