Skip to content

Instantly share code, notes, and snippets.

@scotteg
Last active November 4, 2016 06:28
Show Gist options
  • Save scotteg/f088e1de6d2de3690865 to your computer and use it in GitHub Desktop.
Save scotteg/f088e1de6d2de3690865 to your computer and use it in GitHub Desktop.
Example of iOS -prepareForSegue:sender: in Objective-C and Swift
// Objective-C
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Item Detail"]) {
DetailViewController *d = (DetailViewController *)segue.destinationViewController;
d.title = segue.identifier;
d.itemForDetail = item;
} else if ([segue.identifier isEqualToString:@"Related Items"]) {
UINavigationController *nc = segue.destinationViewController;
RelatedViewController *r = (RelatedViewController *)nc.topViewController;
r.title = segue.identifier;
r.category = item.category;
}
}
// Swift
override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
switch (segue.identifier, segue.destinationViewController) {
case let (i, d as DetailViewController) where i == "Item Detail":
d.title = i
d.itemForDetail = item
case let (i, nc as UINavigationController) where i == "Related Items":
let r = nc.topViewController as RelatedViewController
r.title = i
r.category = item.category
default:
break;
}
}
@ShrikantPanchal
Copy link

In the above examples of both Obj-C and Swift, the "title" is from child View right?
I've tried the above code as it is, but it is not working. The label on the next view controller is not getting updated.
I'm stuck with this since last 2 days, please help.!!

Thanks in advance..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment