码迷,mamicode.com
首页 > 移动开发 > 详细

iOS ViewController之间传递数据

时间:2015-04-20 12:59:23      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

翻译自stackoverflow的问题

前向传递数据:

例如你有ViewControllerA和ViewControllerB,从ViewcontrollerA向ViewcontrollerB传递一个BOOL变量:

(1)在ViewControllerB.h创建一个属性

@property (nonatomic, assign) BOOL *isSomethingEnabled;

(2)在ViewcontrollerA中引入头文件

#import "ViewControllerB.h"

 当点击didSelectRowAtIndex或者IBAction的时候可以用以下代码设置ViewControllerB的属性

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.isSomethingEnabled = YES;
[self pushViewController:viewControllerB animated:YES];

用Segue前向传递数据:

用以下方法传递数据:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

(1)在ViewControllerB.h创建一个属性

@property (nonatomic, assign) BOOL *isSomethingEnabled;

(2)在ViewcontrollerA中引入头文件

#import "ViewControllerB.h"

(3)建立一个从ViewControllerA到ViewControllerB的segue,不妨取名为showDetailSegue

(4)实现方法:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"showDetailSegue"]){
        ViewControllerB *controller = (ViewControllerB *)segue.destinationViewController;
        controller.isSomethingEnabled = YES;
    }
}

如果你的view是嵌套在navigation controller里

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.identifier isEqualToString:@"showDetailSegue"]){
        UINavigationController *navController = (UINavigationController *)segue.destinationViewController;
        ViewControllerB *controller = (ViewControllerB *)navController.topViewController;
        controller.isSomethingEnabled = YES;
    }
}

后向传递数据:

ViewControllerA里面点击某些按钮,跳转到ViewControllerB,里面的事件执行完成后,又要跳转回ViewControllerA,并且把一些数据传递回ViewControllerA。这里需要用到代理模式。

(1)在ViewControllerB.h中,#import下,@interface上声明一个协议

@protocol ViewControllerBDelegate <NSObject>
- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item;
@end

(2)在ViewControllerB.h设置一个delegate属性

@property (nonatomic, weak) id <ViewControllerBDelegate> delegate;

(3)在ViewControllerB中,当我们退出这个ViewController的时候

NSString *itemToPassBack = @"Pass this value back to ViewControllerA";
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];

(4)在ViewControllerA.h中,引入头文件并实现协议

#import "ViewControllerB.h"

@interface ViewControllerA : UIViewController <ViewControllerBDelegate>

(5)在ViewControllerA.m中,

- (void)addItemViewController:(ViewControllerB *)controller didFinishEnteringItem:(NSString *)item
{
    NSLog(@"This was returned from ViewControllerB %@",item);
}

(6)设置代理

ViewControllerB *viewControllerB = [[ViewControllerB alloc] initWithNib:@"ViewControllerB" bundle:nil];
viewControllerB.delegate = self
[[self navigationController] pushViewController:viewControllerB animated:YES];

 

 

 

  

  

iOS ViewController之间传递数据

标签:

原文地址:http://www.cnblogs.com/leizh007/p/4441187.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!