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];
}

原文地址:https://www.cnblogs.com/dingjianjaja/p/4840982.html