Last active
November 4, 2016 06:28
-
-
Save scotteg/f088e1de6d2de3690865 to your computer and use it in GitHub Desktop.
Example of iOS -prepareForSegue:sender: in Objective-C and Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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..