在视图控制器之间传递数据:从uitableview到详细信息视图控制器 [英] Passing Data between View Controllers : from uitableview to a details view controller

查看:87
本文介绍了在视图控制器之间传递数据:从uitableview到详细信息视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using IOS 5 and Storyboard coding. I have built an application in which i have a tableView connected to a sqlite database with a search bar. 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. Any ideas?

编辑问题:

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


推荐答案

出于演示目的,我假设我们有两个视图控制器 ListViewController DetailViewController 。 ListViewController有tableview,你在tableview单元格和详细信息视图控制器之间创建了一个 segue

For demo purpose, I will assume we have two view controllers ListViewController and DetailViewController. ListViewController has tableview and you have created a segue between your tableview cell and details view controller.

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

I'm assuming you are naming it as 'DetailSegue'. Your storyboard should look similar to the below image.

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

Select the segue between tableViewCell and DetailViewController, open the attributes inspector and specify the identifier as 'DetailSegue'. See the image below,

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

Run the application now, you should see a smooth navigation from ListViewController to DetailViewController.

假设您的数据源是一组叮咬,并且您的数据源和委托设置已经正确用于列表tableView

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

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.

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

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

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

这篇关于在视图控制器之间传递数据:从uitableview到详细信息视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆