I have a unwind segue in my WishlistVC
(a VC that lists all wishlists). Now, this VC allows a user to tap on a wishlist and, if it's empty, bring up the EditWishlistItems
VC so one can select which items to put inside that wishlist. The WishlistVC also has another button that allows a user to create a new wishlist, and then invokes the EditWishlistItems
so the user can add items to the new wishlist. Both these are different segues to the same VC.
When I unwind, I want to know who invoked the EditWishlistItems
VC, i.e. which segue did I perform going forward in my unwind segue. If it's from the addItemsToWishlist
modal segue, then I want to add the wishlist to Core Data, and if it's from editWishlistItems
modal segue, I want to edit the wishlist's items in CD.
Any way to know this information when I unwind?
I would do it by adding a property
@property (nonatomic, strong) NSString *recentSegue;
and then creating
-(void)addItemsToWishList:{
//business logic
self.recentSegue = @"addItems";
[self performSegueWithIdentifier:@"EditWishlistItems" sender:self];
}
-(void)editWishListItems:{
//business logic
self.recentSegue = @"editItems";
[self performSegueWithIdentifier:@"EditWishlistItems" sender:self];
}
Then link your buttons to those methods rather than directly performing segues
and finally
-(void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if(self.recentSegue){
if(self.recentSegue isEqualToString:@"addItems") {
//do Whatever
} else if(self.recentSegue isEqualToString:@"editItems") {
//do whatever else
}
self.recentSegue = nil;
}
}
While this should work, another way that might be better depending on your design is to let the EditWishListItems handle the coredata interaction directly.
THE_DOM's answer is good, but this is how I fixed my issue.
To see whether or not we need to modally segue, we check and see if our wishlist has no items in it in the following delegate method. If not, we tell it to segue. If it does have items, the push segue will automatically get called instead (set up in IB).
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
Wishlist *wishlist = [self.fetchedResultsController objectAtIndexPath:indexPath];
if([wishlist.items count] == 0) {
// execute our segue
[self performSegueWithIdentifier:@"editWishlistItems" sender:self];
}
}
In our prepareForSegue
, we set a BOOL
to indicate the user tapped on an empty wishlist (this is in our destination VC).
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
... // other segues
if([segue.identifier isEqualToString:@"editWishlistItems"])
{
UINavigationController *navigationVC = (UINavigationController *)segue.destinationViewController;
ManageWishlistItemCDTVC *manageWishlistItemVC = (ManageWishlistItemCDTVC *)navigationVC.topViewController;
manageWishlistItemVC.tappedOnEmptyWishlist = YES;
manageWishlistItemVC.wishlist = [self.fetchedResultsController objectAtIndexPath:indexPath];
}
}
And then, when we unwind, we check to see if the BOOL
is YES
or NO
.
- (IBAction)saveWishlistItem:(UIStoryboardSegue *)segue {
ManageWishlistItemCDTVC *manageWishlistItemVC = (ManageWishlistItemCDTVC *)segue.sourceViewController;
// check to see if we came from edit segue or add segue
if(manageWishlistItemVC.tappedOnEmptyWishlist) { // edit segue
// saved edited wishlist
} else { // add segue
if([manageWishlistItemVC.selectedItems count] > 0) {
// save new wishlist
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With