iOS入门-30多界面传值

概述

多界面传值的

  • 分栏控制器、导航控制器、视图控制器嵌套使用
  • 视图控制器之间传值(正向传值,反向传值)
  • 协议函数
  • 代理

示例

先看图

iOS入门-30多界面传值_第1张图片

示例代码

仔细看代码注释。
关于如何配置UIWindow,参看iOS入门-06UIWindow

AppDelegate.h

#import 

@interface AppDelegate : UIResponder 
//声明UIWindow属性
@property(retain,nonatomic) UIWindow* window;

@end

AppDelegate.m

#import "AppDelegate.h"
#import "VCFirst.h"
#import "VCThird.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    //初始化UIWindow
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    //视图一控制器初始化
    VCFirst* vcFirst = [VCFirst new];
    vcFirst.title = @"第一页";
    vcFirst.view.backgroundColor = [UIColor redColor];
    //将第一个视图控制器放入导航栏控制器01
    UINavigationController* nav01 = [[UINavigationController alloc]initWithRootViewController:vcFirst];
    
    //视图控制器三初始化
    VCThird* vcThird = [VCThird new];
    vcThird.title = @"第三页";
    vcThird.view.backgroundColor = [UIColor blueColor];
    //视图控制器三添加到导航栏控制器02中
    UINavigationController* nav02 = [[UINavigationController alloc] initWithRootViewController:vcThird];
    
    //将两个导航栏控制器添加到导航栏控制器中
    NSArray* array = [NSArray arrayWithObjects:nav01,nav02, nil];
    UITabBarController* tabC = [UITabBarController new];
    tabC.viewControllers = array;
    
    //设置UIWindow的根视图控制器为导航栏控制器
    self.window.rootViewController = tabC;
    //使UIWindow生效并显示
    [self.window makeKeyAndVisible];
    
    return YES;
}

@end

VCFirst.h

#import 
//引入VCSecond.h
#import "VCSecond.h"

NS_ASSUME_NONNULL_BEGIN

@interface VCFirst : UIViewController 
//声明要实现的协议函数
-(void) changeColor:(UIColor *)color;

@end

NS_ASSUME_NONNULL_END

VCFirst.m

#import "VCFirst.h"
#import "VCSecond.h"

@interface VCFirst ()

@end

@implementation VCFirst

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //导航栏添加按钮,点击跳转到视图控制器二
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"到第二页" style:(UIBarButtonItemStylePlain) target:self action:@selector(toSecond)];
    
    self.navigationItem.rightBarButtonItem = item;
}

-(void)toSecond{
    //创建视图控制器二
    VCSecond* vcSecond = [VCSecond new];
    //设置视图控制器二的背景色
    vcSecond.view.backgroundColor = [UIColor orangeColor];
    //设置视图控制器二的代理对象为本视图控制器对象
    vcSecond.delegate = self;
    //跳转到视图控制器二
    [self.navigationController pushViewController:vcSecond animated:YES];
}

-(void) changeColor:(UIColor *)color{
    //设置本视图的背景色
    self.view.backgroundColor = color;
}

@end

VCSecond.h

#import 

NS_ASSUME_NONNULL_BEGIN

//定义代理协议,声明视图控制器二的协议(类似于java中接口,dart中由于可以传递“函数”所以不用使用这种代理或者观察者模式)
@protocol VCSecondDelegate 
//定义一个协议函数(改变背景颜色)
-(void) changeColor:(UIColor*) color;
@end

@interface VCSecond : UIViewController

//声明一个属性
@property(assign,nonatomic)NSInteger tag;
//声明代理对象(类似于java中声明的接口对象)
//代理对象用来执行协议函数
//代理对象一定要实现代理协议
@property(assign,nonatomic) id delegate;

@end

NS_ASSUME_NONNULL_END

VCSecond.m

#import "VCSecond.h"

@interface VCSecond ()

@end

@implementation VCSecond

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //导航栏上添加个按钮
    UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:@"返回第一页" style:UIBarButtonItemStylePlain target:self action:@selector(changeFirstPageColor)];
    
    self.navigationItem.rightBarButtonItem = item;
}

-(void)changeFirstPageColor{
    //点击按钮改变上一个页面颜色
    [_delegate changeColor:[UIColor purpleColor]];
    //退出本视图控制器
    [self.navigationController popViewControllerAnimated:YES];
}

@end

你可能感兴趣的:(iOS,ios,objective-c,视图控制器,页面传值)