[分享]iOS开发-导航控制器下UIViewController之间的6种传值方式

1.代理(delegate)传值 ---- 顾名思义就是委托别人办事,就是当一件事情发生后,自己不处理,让别人来处理。
实质就是:比如右AB两个页面,A想要传值给B ,就只要先在A中得到B的指针,然后将想要传的值赋给B,之后跳转

代码如下:

A.h  
@protocol HMTShowViewControllerDelegate <NSObject>  
  
@optional  
-(void)showViewGiveValue:(NSString *)text;  
@end  
  
@interface HMTShowViewController : UIViewController <UITextFieldDelegate>  
  
@property (nonatomic,copy)NSString * text;  
// 定义一个代理  
  
@property (nonatomic,assign)id<HMTShowViewControllerDelegate> delegate;  
  
@end  
  
  
A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    // 在页面跳转前将参数传出去  
    if ([self.delegate respondsToSelector:@selector(showViewGiveValue:)]) {  
        [self.delegate showViewGiveValue:_showTextField.text];  
    }  
    [self.navigationController popViewControllerAnimated:YES];  
      
}  
  
  
  
B.h  
@interface HMTInputViewController : UIViewController <HMTShowViewControllerDelegate>  
  
B.m  
-(void)pushNextViewControl:(UIBarButtonItem *)button{  
  
    HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
    showVC.text = _textField.text;  
    // 将代理对象设置成B  
    showVC.delegate = self;  
    [self.navigationController pushViewController:showVC animated:YES];   
  
}
// B实现协议里面的方法  
-(void)showViewGiveValue:(NSString *)text{  
  
    _inputLabel.text = text;  
}  


2.单例传值 ------- 如果页面之间相隔很多,要进行传值,将值保存到第三方,将第三方设置为单例模式

代码如下:

static HMTInputViewController * shareRootViewController = nil;  
  
@implementation HMTInputViewController  
  
+(HMTInputViewController *)sharedController{  
    @synchronized(self){  
        if(shareRootViewController == nil){  
            shareRootViewController = [[self alloc] init] ;  
        }  
    }  
    return shareRootViewController;  
} 

要将HMTInputViewController中的一个UITextField编辑的内容传到HMTShowViewController中的UILabel
_showLabel.text = [HMTInputViewController sharedController].textField.text;


3.Target-Action传值
实质就是:A页面要给B页面传值,A就提供接口出去,抓A到B内部来,A间接调用自己内部方法(相当于,A把自己内部需要操作的方法,传到B内来,到B内部进行赋值,这样就不存在访问不到各自的局部实例变量)

@property (nonatomic,assign)id target;
@property (nonatomic,assign)SEL action;
[self.traget performSelector:self.action withObject:nil];(是否需要传参数自己定,最多2个)

代码如下:

A.h  
@interface HMTShowViewController : UIViewController <UITextFieldDelegate>  
  
@property (nonatomic,assign) id traget;  
@property (nonatomic,assign) SEL action;  
  
@end  
  
A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    [self.traget performSelector:self.action withObject:_showTextField.text];  
    [self.navigationController popViewControllerAnimated:YES];  
  
}  
  
B.m  
-(void)pushNextViewControl:(UIBarButtonItem *)button{  
  
    HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
    showVC.traget = self;  
    showVC.action = @selector(changeLabelText:);  
    [self.navigationController pushViewController:showVC animated:YES];  
  
}  
  
-(void)changeLabelText:(NSString *)text{  
      
    _inputLabel.text = text;  
      
}  


4.属性/方法传值

A - pushViewController - B 在A视图中有B视图的对象 ,可直接操作

-(void)didClickRightButtonItemAction  
{  
    InputViewController * inputVC = [[InputViewController alloc] init];  
  
    [self.navigationController pushViewController:inputVC animated:YES];  
  
}

B - popViewControllerAnimated - A 因为A推出B,A只是隐藏了,退到栈底去了,在B返回A,A的viewDidLoad并不会再次调用

-(void)didClickLeftButtonItemAction  
{  
    ShowViewController * showView = (ShowViewController *)[self.navigationController.viewControllers objectAtIndex:0];  
      
    [self.navigationController popViewControllerAnimated:YES];  
}


5.block传值(A-view 传值给 B-view)
@释放 Block_release(_doTransferMsg);

A.h  
@property (nonatomic,copy) void(^doTransferMsg)(NSString * msg);  
  
A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    if (_doTransferMsg) {  
        _doTransferMsg(_showTextField.text);  
          
    }  
      
    [self.navigationController popViewControllerAnimated:YES];  
}  
  
  
B.m  
-(void)pushNextViewControl:(UIBarButtonItem *)button{  
  
    HMTShowViewController * showVC = [[HMTShowViewController alloc]init];  
      
    // block传值  
    [showVC setDoTransferMsg:^(NSString * msg) {  
        dispatch_async(dispatch_get_main_queue(), ^{  
            _inputLabel.text = msg;        });  
    }];  
      
    [self.navigationController pushViewController:showVC animated:YES];   
  
}

1344323584_7609.png


6.通知传值(同上,也是A传给B)

A.m  
-(void)popPerView:(UIBarButtonItem *)barButton{  
  
    [[NSNotificationCenter defaultCenter] postNotificationName:@"changeLabelTextNotification" object:_showTextField.text];  
      
    [self.navigationController popViewControllerAnimated:YES];  
}  
  
B.m  
-(void)viewDidLoad  
{  
    [super viewDidLoad];  
      
    // 通知中心,发送一条消息传值--------其中name千万不要写错了,会出现在3个地方  
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showViewGiveValue:) name:@"changeLabelTextNotification" object:nil];  
      
}  
  
-(void)showViewGiveValue:(NSNotification *)notification{  
    // 取出notification的object  
    id text = notification.object;  
    _inputLabel.text = text;  
}  
  
-(void)dealloc{  
    // 消息发送完,要移除掉  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"changeLabelTextNotification" object:nil];  
    [super dealloc];  
      
} 


分享来源:
http://blog.csdn.net/hmt20130...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值