0

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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
Elias Rahme
  • 2,226
  • 3
  • 28
  • 53

2 Answers2

6

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.

enter image description here

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

enter image description here

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.

RJR
  • 1,072
  • 2
  • 9
  • 21
  • Thank you for taking the time to answer..although i have changed the whole hierarchy since the time i posted this question, but your demonstration could help out someone in the future...thank you and your 100% right – Elias Rahme Nov 06 '13 at 09:53
0

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.

e.g

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

geminiCoder
  • 2,918
  • 2
  • 29
  • 50
  • could you please provide a code snippet explaining this matter? thank you in advance – Elias Rahme Jul 18 '12 at 07:14
  • Am i doing anything wrong ? : - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ labelText.text = author.title; } – Elias Rahme Jul 18 '12 at 07:19
  • @EliasRahme I added more detail to my answer check, it out and let me know if your still stuck. – geminiCoder Jul 18 '12 at 07:28
  • I am pretty lost, i am really new to Xcode , could you please check my edited question to make sure whether i am on the right track? Thank you sir – Elias Rahme Jul 18 '12 at 07:39
  • Ye your on the right track. but I think what you want is `dv.testLabel.text = author.title` your setting the destination view controllers attributes. if you do it the other way round your get nil – geminiCoder Jul 18 '12 at 07:57
  • I did try it, but no results...what could be the problem? – Elias Rahme Jul 18 '12 at 08:15
  • hmmm, could you past some more of your code. also you might want to log the variables to check they are not nil – geminiCoder Jul 18 '12 at 08:42
  • I see the problem the `prepareForSegue` needs to go in the previous ViewController not the details. it updates the detailsView controller from the previous (table)ViewController as it loads. – geminiCoder Jul 18 '12 at 10:37
  • I did try it but the same result :S nothing happens...what to do? – Elias Rahme Jul 18 '12 at 11:37
  • Add a break point on the `prepareForSegue` is the method being called? Single step through the method and check that all the attributes are correct. – geminiCoder Jul 18 '12 at 11:57
  • I just discovered that the method is not called at all! I put a breakpoint and the app continued running normally – Elias Rahme Jul 18 '12 at 12:06
  • I would suggest that your code does not use `performSegueWithIdentifier` that instead your just straight pushing to the UINavigationController? – geminiCoder Jul 18 '12 at 12:09
  • My application is based on navigation Controller that's right, but in the first steps i wasn't capable of pushing data through it, and till now the problem persists...so any ideas?\ – Elias Rahme Jul 18 '12 at 13:12
  • I can't see in your code what performs the move to the detailViewController? can you paste that? – geminiCoder Jul 18 '12 at 14:56
  • could you send me an in an email your own email? in that way i will send you the source code...coz actually being new to storyboard is really making me lost ... plz send me your email on elias.rahme.89@gmail.com...and i really appreciate it if you take the time to help me out...Thank you in advance kind Sir] – Elias Rahme Jul 19 '12 at 05:20
  • I can't give out my email address. I suggest if your still struggling you check out http://itunes.apple.com/us/itunes-u/advanced-iphone-development/id407243028 These should help you – geminiCoder Jul 19 '12 at 09:31