Question

I'm having difficulty in passing current selected row of TableView to other ViewController? How to do that easily?

Here's my didSelectRowAtIndexPath

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

    WishlistItem *wish = [self.wishlistItem objectAtIndex:indexPath.row];
    // Pass these data to DetailViewController
    NSLog(@"Wish Item: %@", wish.wishlistItem);
    NSLog(@"Description: %@", wish.descWishItem);
    NSLog(@"Target Date: %@", wish.targetDate);
}

How would I pass these data to my DetailViewController which has

@property (strong, nonatomic) IBOutlet UILabel *wishLabel;
@property (strong, nonatomic) IBOutlet UITextView *descriptionTextView;
@property (strong, nonatomic) IBOutlet UILabel *detailTargetDate;

I would like to know also how to go to DetailViewController when row was pressed.

That's all. TIA. Please let me know if I need to provide more info about my question. Thanks.

Was it helpful?

Solution 2

For this purpose you can use segue. First you need to create a segue to your destination view controller in your storyboard. Give an identifier for this segue .Then implement these methods in your controller

#pragma mark - Navigation

 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
 {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
     if ([segue.identifier isEqualToString:SEGUE_IDENTIFIER]) {
         DestinationController *dest = (DestinationController *)[segue destinationViewController];
         //here you can pass the data
         dest.wishLabel.text = wish.wishlistItem;
         dest.descriptionTextView.text = wish.descWishItem;
         dest.detailTargetDate.text = wish.targetDate;
         // or you can pass the entire wish object
         dest.wish = wish;
     }
 }

- (BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return NO;
}

You need to go to the destination view controller only when user select a cell. That's why we return NO in the second delegate method. Now implement didSelectRowAtIndexPath

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == ROW_INDEX {
        [self performSegueWithIdentifier:SEGUE_IDENTIFIER sender:self];
        wish = [self.wishlistItem objectAtIndex:indexPath.row];
    }
}

Here, performSegueWithIdentifier is used for navigation

OTHER TIPS

DetailViewController *dvc = [[DetailViewController alloc] initWithNibName:@"DetailViewController" bundle:nil];
[self.navigationController pushViewController:dvc animated:YES];
dvc.wishLabel.text = wish.wishlistItem;
dvc.descriptionTextView.text = wish.descWishItem;
dvc.detailTargetDate.text = wish.targetDate;

You should define a seque for row selection and pass data in prepareForSegue: method, and you should declare a public readwrite property in type Wish * and name with wish, so this way you can pass the selected WishlistItem object to the DetailViewController instance.

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([[segue identifier] isEqualToString:@"DetailViewSegue"]) {
        WishlistItem *wish = [self.wishlistItem objectAtIndex:self.tableView.indexPathForSelectedRow.row];
        DetailViewController *detailViewController = [segue destinationViewController];
        detailViewController.wish = wish;
    }
}

You need to do something like this. If you are using interface builder then the init part of declaring the controller would need to change but im sure you can figure that out.

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

    WishlistItem *wish = [self.wishlistItem objectAtIndex:indexPath.row];

    // Pass these data to DetailViewController
    DetailViewController *controller = [[DetailViewController alloc] init];

    controller.wishLabel.text = wish.wishlistItem;
    controller.descriptionTextView.text = wish.descWishItem;
    controller.detailTargetDate.text = wish.targetDate;

    [self.navigationController pushViewController:controller animated:YES];
}

pass variable like this. make property for variable wishtolistItem, desWichItem and targetdat in DetailViewController.

Create an Object of detailViewController in didSelectRowAtIndexPath method in your viewcontroller.

 DetailViewController *playviewcontroller = [[DetailViewController alloc] init];

playviewcontroller.wishlistItem.text = variableWhichtYouWantTopass1
playviewcontroller.descWishItem.text = variableWhichYouWantTopass2;
playviewcontroller.targetDate.text  =variableWhichYouWantTopass1;

 [self.navigationController pushViewController:playviewcontroller animated:YES];

If you are using Storyboard, then please follow these code. I think this will help you.

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
  DetailViewController *detailView_controller = [self.storyboard instantiateViewControllerWithIdentifier:@"ProductID"]; // set your storyboard identifier
  detailView_controller.wishlistItem = wishLabel.text;
  detailView_controller.descItem = descriptionTextView.text;
  detailView_controller.dateItem = detailTargetDate.text;
  [self.navigationController pushViewController:detailView_controller animated:YES];
}

In DetailViewController.h class

 @property (nonatomic, retain) NSString *ProductName, *wishlistItem;
 @property (nonatomic, retain) NSString *ProductName, *descItem;
 @property (nonatomic, retain) NSString *ProductName, *dateItem;

In DetailViewController.m class

 @implementation DetailViewController
 @synthesize wishlistItem;
 @synthesize descItem;
 @synthesize dateItem;

- (void)viewDidLoad
{
    NSLog(@"wishlistItem: %@", self.wishlistItem);
    NSLog(@"descItem: %@", self.descItem);
    NSLog(@"dateItem: %@", self.dateItem);
}

I am sharing a screenshot for set the identifier for storyboard. (see right side bar)

Select detailsView controller on storyboard and set identifier.

Identity -> storyboard ID -> ProductID

enter image description here

I think this will help you..:)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top