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?
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
. ListViewController has tableview and you have created a segue between your tableview cell and details view controller
.
I'm assuming you are naming it as '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'. See the image below,
Run the application now, you should see a smooth navigation from ListViewController to DetailViewController.
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.
- (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.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
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.
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. Using the rowPressed to get the relevant data from your data model.
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.