VC间的传值

property、initializer 这两种适合VCA传值给VCB,在VCB做界面展示

  • property传值
#import "xxxxxxxA.h"
//property button
    UIButton *propertyButton = [[UIButton alloc] init];
    [propertyButton setBackgroundColor:[UIColor orangeColor]];
    [propertyButton setTitle:@"next VC (property)" forState:UIControlStateNormal];
    [propertyButton addTarget:self action:@selector(propertyButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    propertyButton.frame = CGRectMake(50, 50, 200, 30);
    [view addSubview:propertyButton];
 
- (void)propertyButtonClicked:(id)sender
{
    TestViewControllerB *viewControllerB = [[TestViewControllerB alloc] init];
    viewControllerB.property1 = @"via Property value";
    [self.navigationController pushViewController:viewControllerB animated:YES];
}   

#import "xxxxxB.h"
@property (nonatomic, retain) NSString *property1;

   //label
    UILabel *label = [[UILabel alloc] init];
    [label setBackgroundColor:[UIColor orangeColor]];
    [label setTextColor:[UIColor whiteColor]];
    [label setFont:[UIFont systemFontOfSize:15]];
    [label setFrame:CGRectMake(50, 50, 200, 30)];
    [view addSubview:label];
    
    label.text = self.property1;//属性赋值
  • initializer
#import "xxxxA.h"
//initializer button
    UIButton *initializerButton = [[UIButton alloc] init];
    [initializerButton setBackgroundColor:[UIColor orangeColor]];
    [initializerButton setTitle:@"next VC (initializer)" forState:UIControlStateNormal];
    [initializerButton addTarget:self action:@selector(initializerButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    initializerButton.frame = CGRectMake(50, CGRectGetMaxY(propertyButton.frame) + 30, 200, 30);
    [view addSubview:initializerButton];

- (void)initializerButtonClicked:(id)sender
{
    TestViewControllerB *viewControllerB = [[TestViewControllerB alloc] initWithProperty1:@"via Initializer value"];
    [self.navigationController pushViewController:viewControllerB animated:YES];
}
#import "xxxxxB.h"
- (instancetype)initWithProperty1:(NSString *)property1Value;
- (instancetype)initWithProperty1:(NSString *)property1Value
{
    self = [self init];
    if (self)
    {
        self.property1 = property1Value;
    }
    return self;
}

delegate、block ,VCA到VCB,VCB回调值给VCA,适合当前页面往pop之后的页面传值

  • delegate
#import "xxxxxxA.h"
@interface xxxxxxA ()
{
    UILabel *_delegateLabel;
}
  UIButton *delegateButton = [[UIButton alloc] init];
    [delegateButton setBackgroundColor:[UIColor orangeColor]];
    [delegateButton setTitle:@"next VC (delelgate)" forState:UIControlStateNormal];
    [delegateButton addTarget:self action:@selector(delegateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    delegateButton.frame = CGRectMake(50, CGRectGetMaxY(initializerButton.frame) + 30, 200, 30);
    [view addSubview:delegateButton];
    
    _delegateLabel = [[UILabel alloc] init];
    [_delegateLabel setBackgroundColor:[UIColor lightGrayColor]];
    [_delegateLabel setTextColor:[UIColor whiteColor]];
    [_delegateLabel setFont:[UIFont systemFontOfSize:15]];
    _delegateLabel.frame = CGRectMake(CGRectGetMaxX(delegateButton.frame) + 5, CGRectGetMinY(delegateButton.frame), 150, 30);
    [view addSubview:_delegateLabel];

- (void)delegateButtonClicked:(id)sender
{
    TestViewControllerB *viewControllerB = [[TestViewControllerB alloc] init];
    viewControllerB.delegate = self;
    [self.navigationController pushViewController:viewControllerB animated:YES];
}

#pragma mark - delegate
- (void)testViewControllerB:(TestViewControllerB *)testViewControllerB sendDataBack:(NSString *)data
{
    _delegateLabel.text = data;
}

#import"xxxxB.h"
@protocol TestViewControllerBDelegate 

- (void)testViewControllerB:(TestViewControllerB *)testViewControllerB sendDataBack:(NSString *)data;

@end
@interface xxxxB : UIViewController
@property (nonatomic, weak) id delegate;
@end

   if (_delegate)
    {
        UIButton *notifyDelegateButton = [[UIButton alloc] init];
        [notifyDelegateButton setBackgroundColor:[UIColor orangeColor]];
        [notifyDelegateButton setTitle:@"notify delegate" forState:UIControlStateNormal];
        [notifyDelegateButton addTarget:self action:@selector(notifyDelegateButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        notifyDelegateButton.frame = CGRectMake(50, 50, 200, 30);
        [view addSubview:notifyDelegateButton];
    }

#pragma mark - button
- (void)notifyDelegateButtonClicked:(id)sender
{
    if ([_delegate respondsToSelector:@selector(testViewControllerB:sendDataBack:)])
    {
        [_delegate testViewControllerB:self sendDataBack:@"notify delegate"];
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}
  • block

#import "xxxxxA.h"
@interface TestViewControllerA ()
{
    UILabel *_blockLabel;
}

 //block button
    UIButton *blockButton = [[UIButton alloc] init];
    [blockButton setBackgroundColor:[UIColor orangeColor]];
    [blockButton setTitle:@"next VC (block)" forState:UIControlStateNormal];
    [blockButton addTarget:self action:@selector(blockButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    blockButton.frame = CGRectMake(50, CGRectGetMaxY(delegateButton.frame) + 30, 200, 30);
    [view addSubview:blockButton];
    
    _blockLabel = [[UILabel alloc] init];
    [_blockLabel setBackgroundColor:[UIColor lightGrayColor]];
    [_blockLabel setTextColor:[UIColor whiteColor]];
    [_blockLabel setFont:[UIFont systemFontOfSize:15]];
    _blockLabel.frame = CGRectMake(CGRectGetMaxX(blockButton.frame) + 5, CGRectGetMinY(blockButton.frame), 150, 30);
    [view addSubview:_blockLabel];

- (void)blockButtonClicked:(id)sender
{
    TestViewControllerB *viewControllerB = [[TestViewControllerB alloc] initWithDataBlock:^(NSString *data) {
       
        _blockLabel.text = data;
        
    }];
    
    [self.navigationController pushViewController:viewControllerB animated:YES];
}
#import "xxxxxxB.h"
typedef void(^DataBlock) (NSString * data);
- (instancetype)initWithDataBlock:(DataBlock)dataBlock;

#import"xxxxxxB.m"
@interface xxxxxxB ()
{
    DataBlock _dataBlock;
    
}
- (instancetype)initWithDataBlock:(DataBlock)dataBlock
{
    self = [self init];
    if (self) {
        
        _dataBlock = [dataBlock copy];
    }
    return self;
}
if (_dataBlock)
    {
        UIButton *blockButton = [[UIButton alloc] init];
        [blockButton setBackgroundColor:[UIColor orangeColor]];
        [blockButton setTitle:@"notify block" forState:UIControlStateNormal];
        [blockButton addTarget:self action:@selector(blockButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        blockButton.frame = CGRectMake(50, 50, 200, 30);
        [view addSubview:blockButton];

    }
- (void)blockButtonClicked:(id)sender
{
    if (_dataBlock) {
        _dataBlock(@"notify Block");
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

notification 一对多的传值,不用关心逻辑

#import "xxxxA.h"
 @interface TestViewControllerA ()
    UILabel *_notificationLabel;
}
    //notification button
    UIButton *notificationButton = [[UIButton alloc] init];
    [notificationButton setBackgroundColor:[UIColor orangeColor]];
    [notificationButton setTitle:@"next VC (notification)" forState:UIControlStateNormal];
    [notificationButton addTarget:self action:@selector(notificationButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    notificationButton.frame = CGRectMake(50, CGRectGetMaxY(blockButton.frame) + 30, 200, 30);
    [view addSubview:notificationButton];

    _notificationLabel = [[UILabel alloc] init];
    [_notificationLabel setBackgroundColor:[UIColor lightGrayColor]];
    [_notificationLabel setTextColor:[UIColor whiteColor]];
    [_notificationLabel setFont:[UIFont systemFontOfSize:15]];
    _notificationLabel.frame = CGRectMake(CGRectGetMaxX(notificationButton.frame) + 5, CGRectGetMinY(notificationButton.frame), 150, 30);
    [view addSubview:_notificationLabel];

- (void)notificationButtonClicked:(id)sender
{
    TestViewControllerB *viewControllerB = [[TestViewControllerB alloc] initWithNotification];
    [self.navigationController pushViewController:viewControllerB animated:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationReceived:) name:TestViewControllerBNotification object:nil];
}
#pragma mark - notification
- (void)notificationReceived:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    if ([userInfo isKindOfClass:[NSDictionary class]])
    {
        _notificationLabel.text = userInfo[@"data"];
    }
}

#import "xxxxxxxB.h"
FOUNDATION_EXTERN NSString *const TestViewControllerBNotification;
- (instancetype)initWithNotification;

#import "xxxxxB.m"
 if (_useNotification)
    {
        UIButton *notificationButton = [[UIButton alloc] init];
        [notificationButton setBackgroundColor:[UIColor orangeColor]];
        [notificationButton setTitle:@"notify notification" forState:UIControlStateNormal];
        [notificationButton addTarget:self action:@selector(notificationButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        notificationButton.frame = CGRectMake(50, 50, 200, 30);
        [view addSubview:notificationButton];
    }
- (void)notificationButtonClicked:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:TestViewControllerBNotification object:nil userInfo:@{@"data" : @"notification data"}];
    
    [self.navigationController popViewControllerAnimated:YES];
}

mediator 两个VC间相互传值,这里需要加一个管理值得类

#import "AppDelegate.h"
#import "xxxxxxxA.h"
@interface xxxxxxxA ()
{
    UILabel *_mediatorLabel;
}
 //mediator button
    UIButton *mediatorButton = [[UIButton alloc] init];
    [mediatorButton setBackgroundColor:[UIColor orangeColor]];
    [mediatorButton setTitle:@"next VC (mediator)" forState:UIControlStateNormal];
    [mediatorButton addTarget:self action:@selector(mediatorButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    mediatorButton.frame = CGRectMake(50, CGRectGetMaxY(notificationButton.frame) + 30, 200, 30);
    [view addSubview:mediatorButton];
    
    _mediatorLabel = [[UILabel alloc] init];
    [_mediatorLabel setBackgroundColor:[UIColor lightGrayColor]];
    [_mediatorLabel setTextColor:[UIColor whiteColor]];
    [_mediatorLabel setFont:[UIFont systemFontOfSize:15]];
    _mediatorLabel.frame = CGRectMake(CGRectGetMaxX(mediatorButton.frame) + 5, CGRectGetMinY(mediatorButton.frame), 150, 30);
    [view addSubview:_mediatorLabel];
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    
    if (_mediatorLabel) {
        _mediatorLabel.text = ((AppDelegate *)[UIApplication sharedApplication].delegate).sharedString;
    }
}
- (void)mediatorButtonClicked:(id)sender
{
    TestViewControllerB *viewControllerB = [[TestViewControllerB alloc] initWithMediator];
    [self.navigationController pushViewController:viewControllerB animated:YES];
}

#import "AppDelegate.h"
#import "xxxxB.h"
- (instancetype)initWithMediator;
@interface xxxxB ()
{
    BOOL _useMediator;      //demo
}
- (instancetype)initWithMediator
{
    self = [self init];
    if (self) {
        _useMediator = YES;
    }
    return self;
}
if (_useMediator)
    {
        UIButton *mediatorButton = [[UIButton alloc] init];
        [mediatorButton setBackgroundColor:[UIColor orangeColor]];
        [mediatorButton setTitle:@"notify mediator" forState:UIControlStateNormal];
        [mediatorButton addTarget:self action:@selector(mediatorButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        mediatorButton.frame = CGRectMake(50, 50, 200, 30);
        [view addSubview:mediatorButton];

    }
- (void)mediatorButtonClicked:(id)sender
{
    ((AppDelegate *)[UIApplication sharedApplication].delegate).sharedString = @"shared mediator";
    
    [self.navigationController popViewControllerAnimated:YES];
}

#import "AppDelegate.h"
@interface AppDelegate : UIResponder 

@property (nonatomic, retain) NSString *sharedString;

@end


你可能感兴趣的:(VC间的传值)