如何将数据从主视图显示到详细视图控制器

时间:2013-12-10 08:17:21

标签: ios objective-c uitableview

我是Ios编程的新手我希望将主视图控制器中的数据显示到详细视图控制器中,我正在使用xcode中的主详细信息模板。详细数组工作它显示标签中的细节,我想如何显示标题以及存储在_objects数组中的标题,

这是我的代码

#import "MasterViewController.h"

#import "DetailViewController.h"

@interface MasterViewController () {
    NSMutableArray *_detailObjects;
    NSMutableArray *_thumbnailImage;
}
@end

@implementation MasterViewController
@synthesize _objects;

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

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.title= @"Zodiac List";

    _objects = [[NSMutableArray alloc] initWithObjects:
                @"value 1",
                @"value 2",
                @"value 3",
                @"value 4",
                @"value 5",
              ,nil];
    _detailObjects = [[NSMutableArray alloc] initWithObjects:
                      @"detail of value 1",
                      @"detail of value 2",
                      @"detail of value 3",
                      @"detail of value 4",
                      @"detail of value 5",
                      , nil];
   }

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

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

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

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

    NSDate *object = _objects[indexPath.row];
    cell.textLabel.text = [object description];
    return cell;
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        NSMutableArray *object = _detailObjects[indexPath.row];
        [[segue destinationViewController] setDetailItem:object];
    }
}

@end

细节工作正常我怎么想在标签中显示值1,值2,值2 .. 这是我的详细视图控制器代码..

@property (strong, nonatomic) id detailItem;
@property (strong, nonatomic) id detailItemTitle;

@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@property (weak, nonatomic) IBOutlet UILabel *zodiacNameLabel;

这里是.m文件代码

#import "DetailViewController.h"
#import "MasterViewController.h"

@interface DetailViewController ()
- (void)configureView;
@end

@implementation DetailViewController
@synthesize zodiacNameLabel;


#pragma mark - Managing the detail item

- (void)setDetailItem:(id)newDetailItem
{
    if (_detailItem != newDetailItem) {
        _detailItem = newDetailItem;

        // Update the view.
        [self configureView];
    }
}

-(void) setDetailItemTitle:(id)detailItemTitle{
    if (_detailItemTitle !=detailItemTitle) {
        _detailItemTitle = detailItemTitle;

        [self configureView];
    }
}

- (void)configureView
{
    // Update the user interface for the detail item.

    if (self.detailItem) {
        self.detailDescriptionLabel.text = [self.detailItem description];
      //  self.zodiacNameLabel.text = [self.]

            MasterViewController *master = [[MasterViewController alloc] init];
            zodiacNameLabel.text = [master._objects objectAtIndex:0];
    }

//    if (self.detailItemTitle) {
//        self.zodiacNameLabel.text = [self.detailItem description];
//    }
}

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

//    MasterViewController *master = [[MasterViewController alloc] init];
//    zodiacNameLabel.text = [master._objects objectAtIndex:0];

//    MasterViewController *master = [[MasterViewController alloc]init];
//    zodiacNameLabel.text = [master._objects objectAtIndex:0]; // you can get first element of an array

}

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

@end

2 个答案:

答案 0 :(得分:0)

在detailViewController中创建一个“tmpArray”数组,然后可以将_objects数组复制到tmpArray中。像这样

MastrerViewController.m

DetailViewController *detail = [[DetailViewController alloc]init];
detail.tmpArray = [[NSMutableArray alloc] initWithArray:_objects];

DetailViewController.h

@property (nonatomic,retain) NSMutableArray *tmpArray;

答案 1 :(得分:0)

实施delegatetableview的{​​{1}}方法,然后将{/ 1}}中的masterviewcontroller内的数据推送到您的数据中: -

假设下面的代理人在您的masterviewcontroller

masterViewController

现在,- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { _yourController = [[yourController alloc] initWithNibName:@"yourController" bundle:[NSBundle mainBundle]]; [self.navigationController pushViewController:_yourController animated:YES]; } allocinitializeload转换为nib,如果您要将消息从主要内容发送到详细信息,请声明{{ 1}} detailviewcontroller中的方法,只需将其从上方传递出来,如下所示: -

property