在视图控制器之间传递数据:从uitableview到详细信息视图控制器

时间:2012-07-18 07:02:52

标签: ios xcode uitableview uistoryboard

我正在使用IOS 5和Storyboard编码。我已经构建了一个应用程序,其中我有一个tableView连接到带有搜索栏的sqlite数据库。 当我们触摸一行时,它会自动转到另一个名为“详细信息”的视图控制器。 我需要将数据从我的表视图传递到详细信息视图控制器,例如将author.title传递给labelText.text字段。 有什么想法吗?

编辑问题:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//    NSString *Title;
    Details *dv = (Details*)segue.destinationViewController;
    author.title = dv.labelText.text;
}

部分代码:

//
//  Details.m
//  AuthorsApp
//
//  Created by georges ouyoun on 7/17/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "Details.h"
#import "Author.h"


@interface Details ()

@end

@implementation Details

@synthesize labelText;
@synthesize selectedAuthors;
@synthesize author;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
   self.labelText.text = self.author.title;
    // Do any additional setup after loading the view.
    NSLog(@"Everything is ok now !");
}

- (void)viewDidUnload
{
  //  [self setLabelText:nil];
    NSLog(@"U have entered view did unload");
    [super viewDidUnload];

    [self setLabelText:Nil];
    // Release any retained subviews of the main view.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
//    NSString *Title;
    Details *dv = (Details*)segue.destinationViewController;
  //  author.title = dv.labelText.text;
    dv.labelText.text = author.title;
}




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

    if ([segue.identifier isEqualToString:@"AuthorsCell"]) {

        [segue.destinationViewController setLabelText:author.title];

    }


}




/*
-(void)viewWillAppear:(BOOL)animated
{ 

    self.labelText.text = author.title;

    NSLog(@"U have entered the viewWillAppear tag");
  //  detailsLabel.text = food.description;
}
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (void)dealloc {
    [labelText release];
    [super dealloc];
}
@end

2 个答案:

答案 0 :(得分:6)

出于演示目的,我假设我们有两个视图控制器ListViewControllerDetailViewController。 ListViewController具有tableview,您已创建segue between your tableview cell and details view controller

我假设你将它命名为'DetailSegue'。您的故事板应该与下图相似。

enter image description here

选择tableViewCell和DetailViewController之间的segue,打开属性检查器并将标识符指定为“DetailSegue”。见下图,

enter image description here

现在运行应用程序,您应该看到从ListViewController到DetailViewController的平滑导航。

Assuming you datasource is an array of stings and you have your datasource and delegate setup already properly for your list tableView

在List视图控制器中添加方法prepareForSegue:sender,只要segue即将执行,就会调用此方法。此方法将自动调用。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
   if ([segue.identifier isEqualToString:@"DetailSegue"]) {
    DetailViewController *detailVC = (DetailViewController*)segue.destinationViewController;
    NSIndexPath *indexPath = [self.tableView indexPathForCell:(UITableViewCell *)sender];
    detailVC.title = [self.source objectAtIndex:indexPath.row];
  }
}

在DetailViewController中添加一个名为title的属性来存储所选标题,并为UILabel添加另一个插座以在视图中显示标题。

DetailViewController.h

#import <Foundation/Foundation.h>

@interface RecipeListViewController : NSObject

@property (weak, nonatomic) NSString *title;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@end

DetailViewController.m

@implementation RecipeListViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.titleLabel.text = self.title;
}
@end

不要忘记连接插座。我希望这能让您大致了解如何在视图控制器之间传递数据。

答案 1 :(得分:0)

是的,您需要使用

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

方法(覆盖)女巫允许你通过获取segue.destinationViewController来传递细节,然后设置目标控制器的属性。

e.g

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
detailViewController *dv = (detailViewController*)segue.destinationViewController;
dv.attribute1 = dataFromTable;
}

例如,如果您在数组中有表数据,那么执行此操作的简单方法是获取行的行号并将其设置为变量

int rowPressed;

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

rowPressed = indexPath.row;

}

然后在PrepareForSegue中将数据传递给DetailViewController。使用rowPressed从数据模型中获取相关数据。

希望这有帮助!