Eli*_*hme 0 xcode uitableview ios 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;
}
Run Code Online (Sandbox Code Playgroud)
部分代码:
//
// 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
Run Code Online (Sandbox Code Playgroud)
出于演示目的,我假设我们有两个视图控制器ListViewController和DetailViewController.ListViewController有tableview,你已经创建了一个segue between your tableview cell and details view controller.
我假设你将它命名为'DetailSegue'.您的故事板应该与下图相似.

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

现在运行应用程序,您应该看到从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];
}
}
Run Code Online (Sandbox Code Playgroud)
添加一个名为titleDetailViewController 的属性来存储所选标题,并为UILabel添加另一个出口以在视图中显示标题.
DetailViewController.h
#import <Foundation/Foundation.h>
@interface RecipeListViewController : NSObject
@property (weak, nonatomic) NSString *title;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@end
Run Code Online (Sandbox Code Playgroud)
DetailViewController.m
@implementation RecipeListViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.titleLabel.text = self.title;
}
@end
Run Code Online (Sandbox Code Playgroud)
不要忘记连接插座.我希望这能让您大致了解如何在视图控制器之间传递数据.
| 归档时间: |
|
| 查看次数: |
4203 次 |
| 最近记录: |