gunas
gunas

Reputation: 1907

Passing parameters from one View Controller to another in Swift

I am new to Swift. I just want to pass an indexPath.row value from a UITableViewController to another UIViewController. I have done this using Objective-C but I don't know how to do this using Swift. I have tried many tutorials but I am not getting proper result. I am using code for navigation (not storyboard).

Can anyone please help me. Thanks in advance.

Upvotes: 1

Views: 504

Answers (1)

user1502383
user1502383

Reputation: 325

You can use prepareForSegue:sender: for this.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
       if (!self.selectedIndexPath) {
          self.selectedIndexPath = indexPath;
          [self performSegueWithIdentifier:@"kYourSegueName" sender:nil];
    }
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if([segue.identifier isEqualToString:"kYourSegueName"]) {
        ViewController *viewController = (ViewController *)segue.destinationViewController;
        viewController.selectedRow = self.selectedIndexPath.row;
        self.selectedIndexPath = nil;
    }
}

Upvotes: 1

Related Questions