简体   繁体   English

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

[英]Passing Data between View Controllers : from uitableview to a details view controller

I am using IOS 5 and Storyboard coding. 我正在使用IOS 5和Storyboard编码。 I have built an application in which i have a tableView connected to a sqlite database with a search bar. 我建立了一个应用程序,其中有一个tableView通过搜索栏连接到sqlite数据库。 When we touch a row, it will take us automatically to another view controller called "Details". 当我们触摸一行时,它将自动将我们带到另一个名为“详细信息”的视图控制器。 I need to pass data from my table view to the details view controller, pass for example the author.title to the labelText.text field. 我需要将数据从表视图传递到详细信息视图控制器,例如将author.title传递给labelText.text字段。 Any ideas? 有任何想法吗?

Edited question: 编辑的问题:

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

Partial code: 部分代码:

//
//  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

For demo purpose, I will assume we have two view controllers ListViewController and DetailViewController . 出于演示目的,我假设我们有两个视图控制器ListViewControllerDetailViewController ListViewController has tableview and you have created a segue between your tableview cell and details view controller . ListViewController具有tableview,并且已经在segue between your tableview cell and details view controller创建了一个segue between your tableview cell and details view controller

I'm assuming you are naming it as 'DetailSegue'. 我假设您将其命名为“ DetailSegue”。 Your storyboard should look similar to the below image. 您的情节提要板应类似于下图。

在此处输入图片说明

Select the segue between tableViewCell and DetailViewController, open the attributes inspector and specify the identifier as 'DetailSegue'. 在tableViewCell和DetailViewController之间选择序列,打开属性检查器,并将标识符指定为'DetailSegue'。 See the image below, 见下图,

在此处输入图片说明

Run the application now, you should see a smooth navigation from ListViewController to DetailViewController. 现在运行应用程序,您应该会看到从ListViewController到DetailViewController的顺利导航。

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

Add a method prepareForSegue:sender in your List view controller, this method will get called whenever segue is about to execute.This method will be automatically invoked. 在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];
  }
}

Add a property called title in DetailViewController to store the selected title and add another outlet for UILabel to display the title in the view. 在DetailViewController中添加一个名为title的属性以存储所选标题,并为UILabel添加另一个出口以在视图中显示该标题。

DetailViewController.h DetailViewController.h

#import <Foundation/Foundation.h>

@interface RecipeListViewController : NSObject

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

@end

DetailViewController.m DetailViewController.m

@implementation RecipeListViewController

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

Don't forget to connect the outlet. 不要忘记连接电源插座。 I hope this gives you an rough idea of how we can pass data between view controllers. 我希望这能使您大致了解如何在视图控制器之间传递数据。

Yes you need to use the 是的,您需要使用

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

method (override) witch allows you to pass details by getting the segue.destinationViewController, then set the attributes of the destination controller. 方法(重写)witch允许您通过获取segue.destinationViewController来传递详细信息,然后设置目标控制器的属性。

eg 例如

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

If, for example you have your tables data in the array an easy way to do this is to get the row number of the row pushed and set it to a variable 例如,如果将表中的数据存储在数组中,一种简单的方法是获取被推入的行的行号并将其设置为变量

int rowPressed;

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

rowPressed = indexPath.row;

}

Then in the PrepareForSegue pass the data to the DetailViewController. 然后在PrepareForSegue中将数据传递给DetailViewController。 Using the rowPressed to get the relevant data from your data model. 使用rowPressed从数据模型中获取相关数据。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM