如何使用push segue将viewcontrollerB之间的值传递给viiewcontrollerA

时间:2016-09-08 09:47:48

标签: ios uitableview segue

我有两个视图控制器A和B.当点击视图控制器A上的按钮时,它将导航到查看controllerB,当我点击tableviewcell时,它们是 tableview with textlabel 导航回viewcontrollerA并希望将该textlabel值传递回viewcontrollerA

2 个答案:

答案 0 :(得分:0)

如果您使用push seque是最糟糕的方式,那么您需要增加堆栈跟踪中的内存。

您可以通过多种方式执行此操作,在Google中搜索一次,您可以获得与此相关的多个答案

  1. UnWind Segue

  2. 协议&代表

  3. 本地数据库方法

  4. 使用Singleton类。

  5. 使用bean对象方法。

  6. 使用Plist

  7. 最后去了NSUserDefault

答案 1 :(得分:0)

我使用Protocols and Delegates创建了一个示例,如下所示:

ViewController.m

#import "ViewController.h"
#import "ColorsTableViewController.h"

@interface ViewController () <ColorsDelegate>

 @property (weak, nonatomic) IBOutlet UILabel *textDataLabel;
- (IBAction)goButtonTapped:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)sendColor:(NSString *)colorName{
    self.textDataLabel.text = colorName;
}

- (IBAction)goButtonTapped:(id)sender {

    ColorsTableViewController *colorsTableViewController=(ColorsTableViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"colorTableIdentifier"];
    colorsTableViewController.delegate = self;
    [self.navigationController pushViewController:colorsTableViewController animated:YES];
}
@end

ColorsTableViewController.h

#import <UIKit/UIKit.h>

@protocol ColorsDelegate <NSObject>

@optional

- (void)sendColor:(NSString *)colorName;

@end

@interface ColorsTableViewController : UIViewController

@property (nonatomic, assign) id<ColorsDelegate> delegate;

@end

ColorsTableViewController.m

#import "ColorsTableViewController.h"

@interface ColorsTableViewController () <UITableViewDelegate, UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *colorsTableView;
@property (strong, nonatomic) NSArray *colorsArray;

@end

@implementation ColorsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.colorsArray = @[@"Red",@"Green",@"Blue",@"Brown",@"Yellow"];

    [self.colorsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"colorCell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.colorsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    UITableViewCell *myCell = [self.colorsTableView dequeueReusableCellWithIdentifier:@"colorCell" forIndexPath:indexPath];

    UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
    if(!myTextLabel)
    {
        myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, CGRectGetWidth(myCell.frame), CGRectGetHeight(myCell.frame))];
        myTextLabel.tag = 100;
    }
    myTextLabel.text = self.colorsArray[indexPath.row];
    [myCell.contentView addSubview:myTextLabel];
    return myCell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
     UITableViewCell *myCell = (UITableViewCell *)[self.colorsTableView cellForRowAtIndexPath:indexPath];
     UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
    if(myTextLabel)
    {
        [_delegate sendColor:myTextLabel.text];
    }
}

截图:

enter image description here

以下是我的GitHub链接,请下载并查看:

https://github.com/k-sathireddy/sendDataBack