在tableviewcontroller中获取Object属性

时间:2013-12-27 14:30:54

标签: ios objective-c object

我只是疯了。我想在TableViewController中显示一些对象的属性。

要恢复:

我有一个第一个屏幕,上面有不同的飞机列表。每架飞机都不同,并获得2个属性(名称和标识号)。当我点击一架飞机时,我想在一个新的视图控制器(这里是一个TableViewController)中显示它的信息。 我唯一得到的是一个空字符串...我不明白该怎么做。

这里是AircraftViewController.h的代码(不同飞机的列表)

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

@interface AircraftViewController : UITableViewController <AircraftInfoViewControllerDelegate>

@property (nonatomic, strong) NSMutableArray *aircraft;

@end

这是我的AircraftViewController.m代码

#import "AircraftViewController.h"
#import "Aircraft.h"

@interface AircraftViewController ()

@end

@implementation AircraftViewController
{
    NSString *_info;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (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.aircraft count];
}

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

    AircraftCell *cell = (AircraftCell *)[tableView dequeueReusableCellWithIdentifier:@"AircraftCell"];

    Aircraft *aircraft = (self.aircraft)[indexPath.row];
    cell.immatLabel.text = aircraft.immat;

    return cell;
}


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

    if ([segue.identifier isEqualToString:@"PickInfo"]) {
        AircraftInfoViewController *aircraftInfoViewController = segue.destinationViewController;
        aircraftInfoViewController.delegate = self;
        aircraftInfoViewController.info = _info;
    }
}


- (void)aircraftInfoViewController:(AircraftInfoViewController *)controller didSelectInfo:(NSString *)info
{

    _info = info;
    Aircraft *aircraft = [[Aircraft alloc] init];

    // Here is my problem !
    NSLog(@"String is %@", aircraft.name);

    [self.navigationController popViewControllerAnimated:YES];
}

@end

这是我的飞机对象

#import <Foundation/Foundation.h>

@interface Aircraft : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, copy) NSString *immat;

@end

这是我的AircraftInfoViewController.h(我在哪里显示信息)

@class AircraftInfoViewController;

@protocol AircraftInfoViewControllerDelegate <NSObject>
- (void)aircraftInfoViewController:(AircraftInfoViewController *)controller didSelectInfo:(NSString *)info;
@end

@interface AircraftInfoViewController : UITableViewController

@property (nonatomic, weak) id <AircraftInfoViewControllerDelegate> delegate;
@property (nonatomic, strong) NSString *info;

@end

这是我的AircraftInfoViewController.m

#import "AircraftInfoViewController.h"

@interface AircraftInfoViewController ()

@end

@implementation AircraftInfoViewController
{
    NSArray *_infos;
    NSUInteger _selectedIndex;
}

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    _infos = @[@"TEST"];

    _selectedIndex = [_infos indexOfObject:self.info];
}

// self.navigationItem.rightBarButtonItem = self.editButtonItem;


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

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_infos count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AircraftCell"];
    cell.textLabel.text = _infos[indexPath.row];

    if (indexPath.row == _selectedIndex) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
    } else {
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    if (_selectedIndex != NSNotFound) {
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:
                                 [NSIndexPath indexPathForRow:_selectedIndex inSection:0]];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }

    _selectedIndex = indexPath.row;

    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryCheckmark;

    NSString *info = _infos[indexPath.row];
    [self.delegate aircraftInfoViewController:self didSelectInfo:info];
}


@end

帮助......

1 个答案:

答案 0 :(得分:0)

我没有完全解决您的问题,但在您的代码中,您是:

//1. creating a new aircraft
 Aircraft *aircraft = [[Aircraft alloc] init];
//2. this aircraft object is new and dose not have a name yet as you never assigned a name to it.
//3. this is why your log shows an empty string
 NSLog(@"String is %@", aircraft.name);

看起来您需要打印信息:

  NSLog(@"String is %@", _info);

但如果你能解释一下你想要变得更好的话,那将对你有所帮助。