• iOS基础-UINavigationController、界面通信


    UINavigationController 导航控制器

    是iOS中最常用的多视图控制器之一,它用来管理多个视图控制器

    导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器

    导航控制器以栈的方式管理所控制的视图控制器,至少要有一个被管理的视图控制器,这个控制器我们称作导航控制器的根视图控制器

    出栈和入栈pushViewController:animated:进入下一个视图控制器

    popViewControllerAnimated:返回上一个视图控制器

    popToviewController:animated:返回到指定的视图控制器

    popToRootViewControllerAnimated:返回到根视图控制器

    常用属性

    viewControllers:所有处于栈中的控制器

    topViewController:位于栈顶的控制器

    visibleViewController:当前正在显示的控制器

    navigationBar:导航条

    定制UINavigationBar

    navigationBar——导航条,iOS7之后默认是透明的

    navigationBar在透明情况,与contentView会重合一部分区域

    navigationBar在不透明情况下,contentView跟在navigationBar的下面

    navigationBar竖屏下默认高度为44,横屏下默认高度为32

    barTintColor:设置导航条的颜色

    setBackgroundImage:forBarMetrics:导航条加背景图片

    UIVavigationItem

    属于MVC中的M,封装了要显示在UINaviationBar上的数据

    title:标题

    titleView:标题视图

    leftBarButtonItem:左按钮

    rightBarButtonItem:右按钮

    UIBarButtonItem

    属于MVC中的M,定义了UINavigationItem上按钮的触发事件,外观

    - (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];
        [self.window makeKeyAndVisible];
        RootViewController *mvc = [[RootViewController alloc] init];
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:mvc];
        self.window.rootViewController = nav;
        [mvc release];
        [nav release];
        
        
        
        
        return YES;
    }
    
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        self.view.backgroundColor = [UIColor grayColor];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        btn.layer.borderWidth = 1;
        btn.layer.cornerRadius = 5;
        btn.frame = CGRectMake(20, 80, 335, 50);
        [btn setTitle:@"push到下一页" forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        self.navigationItem.title = @"第一级界面";
        
        //导航栏高度44,状态栏20
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 44)];
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"5.png"]];
        imageView.image = image;
        
    //    [self.navigationController.navigationBar addSubview:imageView];
        //只有当背景图片高度为44的时候,就会只覆盖导航栏,其他情况下会覆盖导航栏加状态栏
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"5.png"] forBarMetrics:UIBarMetricsDefault];
        
        
        
        
    }
    
    //隐藏状态栏
    - (BOOL)prefersStatusBarHidden{
        return NO;
    }
    - (void)btnClick{
        SecondViewController *svc = [[SecondViewController alloc] init];
        [self.navigationController pushViewController:svc animated:YES];
        svc.currentTitle = self.navigationItem.title;
        svc.delegate = self;
        [svc release];
        
        
        
    }
    - (void)sendBtnTitle:(NSString *)title{
        self.navigationItem.title = title;
    }
    

     界面通信

    属性传值  代理传值

    代理传值(从后往前传值:第二个页面的数据传到第一个页面)

    思路:①,在第二个页面设置代理,以及代理属性

    @protocol SendValueDelegate <NSObject>
    
    - (void)sendText:(NSString *)text;
    
    @end
    @interface TiredViewController : UIViewController
    @property (nonatomic,assign) id<SendValueDelegate> delegate;
    @end
    

     ②

    - (void)btn2Click{
        [self.navigationController popViewControllerAnimated:YES];
        if ([_delegate respondsToSelector:@selector(sendText:)]) {
            [_delegate sendText:_textFiled.text];
        }
    }
    

     ③第一个页面接收代理,实现方法

    @interface SecondViewController : UIViewController<SendValueDelegate>
    

     ④实现代理方法

    - (void)sendText:(NSString *)text{
        self.label2.text = text;
    }
    

     #pragma mark block传值1
    @property (nonatomic,copy) void(^block)(NSString


    - (void)btnClick{
        [self.navigationController popViewControllerAnimated:YES];
    #pragma mark block第二步
        //block调用
        self.block(self.textfiled.text);
    }


    *);


    实现在上一界面

    - (void)btnClick{
        PhotoOneViewController *photoOne = [[PhotoOneViewController alloc] init];
    #pragma mark  第三步
        //block的实现
        photoOne.block = ^(NSString *str){
            self.label.text = str;
        };
        
        [self.navigationController pushViewController:photoOne animated:YES];
        [photoOne release];
    }

  • 相关阅读:
    Javascript网页摇一摇
    移动端Web开发注意点
    Clappr——开源的Web视频播放器
    光看这图片就知道是大片--今天是五一劳动节尽管还是敲着代码(日常就是这样)然后想不出写什么了,也找不到好的素材,最后开心一下吧
    大放异彩的伪元素——可以做什么?(转)别人分享的文章,发现很不错,果断收藏了
    全屏滚动效果H5FullscreenPage.js
    今天我已无力吐槽了!写个没有营养的吐槽文。只是个人日记
    css的一些小技巧!页面视觉差!
    CSS3 transforms 3D翻开
    Javascript非构造函数的继承
  • 原文地址:https://www.cnblogs.com/dingjianjaja/p/4840982.html
Copyright © 2020-2023  润新知