使用委托传输数据

时间:2014-08-12 03:18:25

标签: ios ios7 delegates tableview

我有一个名为'Make'的静态单元格的视图控制器我有两个名为“AddCarTableViewController”和“MakeTableViewController”的控制器,当你点击名为'Make'的静态单元格时,它会显示make table view controller你可以在哪里选择make,然后弹出视图控制器,并尝试将所选值存储在静态单元格的detailTextLabel中。这是我所有控制器的代码。

我遇到的问题是,一旦我选择让一切都发生,因为我应该记录所选项目并在弹出视图控制器后保存它,但我无法弄清楚如何将所选项目实现到detailTextLabel。任何帮助将不胜感激!

“MakeTableViewController.h”

#import <UIKit/UIKit.h>
#import "AddCarTableViewController.h"

@protocol CarMakeDelegate <NSObject>
- (void)updateCarMake:(NSString *)updateMake;
@end


@interface MakeTableViewController : UITableViewController

@property (nonatomic, strong) NSArray *carMakes;
@property (nonatomic, weak) id <CarMakeDelegate> delegate;



@end

MakeTableViewController.m

#import "MakeTableViewController.h"


@interface MakeTableViewController ()

@end

@implementation MakeTableViewController {
    NSIndexPath *oldIndexPath;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.carMakes = [[NSArray alloc] initWithObjects:@"Acura", @"Aston Martin", nil];
}

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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return [self.carMakes count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }

    cell.textLabel.text = [self.carMakes objectAtIndex:indexPath.row];

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    oldIndexPath = indexPath;

    NSString *addMake = self.carMakes[indexPath.row];
    [self.delegate updateCarMake:addMake];

    NSLog(@"%@", addMake );

    [[self navigationController] popViewControllerAnimated:YES];

}


@end

AddCarTableViewController.h

#import <UIKit/UIKit.h>
#import "MakeTableViewController.h"


@interface AddCarTableViewController : UITableViewController 

@property (strong, nonatomic) NSString *makeName;
@property (weak, nonatomic) IBOutlet UITableViewCell *makeCell;


@end

AddCarTableViewController.m

#import "AddCarTableViewController.h"

@interface AddCarTableViewController ()

@end

@implementation AddCarTableViewController



- (void)viewDidLoad
{
    [super viewDidLoad];


}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return 4;
}

-(void)updateCarMake:(NSString *)updateMake {

    self.makeCell.detailTextLabel.text = updateMake;
}
@end

1 个答案:

答案 0 :(得分:0)

在这种情况下,您不需要使用委托。只需更新基础数据模型。并致电

[tableview reloadData];

弹出makeViewController时。

在AddCarVC的cellForRowAtIndex中,添加另一行以检查当前indexPath是否对应于Make cell以及是否确实更新了detailLabel文本。