Reputation: 29
Due to I have to slide UIViewController from left to right by a finger swipe, I have created a custom uiviewcontroller transition, after slide and clicked the navigation inside the destination controller, error occurred.
Terminating app due to uncaught exception 'NSGenericException', reason: 'Could not find a navigation controller for segue 'stepsPage'. Push segues can only be used when the source controller is managed by an instance of UINavigationController.'
- (void) returnHome
{
UIStoryboard *storyboard = self.storyboard;
MainViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:@"main"];
// Get the views.
UIView * toView = destVC.view;
UIView * fromView = self.view;
// Get the size of the view area.
CGRect viewSize = fromView.frame;
// Add the toView to the fromView
[fromView.superview addSubview:toView];
// Position it off screen.
toView.frame = CGRectMake( -320 , viewSize.origin.y, 320, viewSize.size.height);
NSLog(@" viewSize.origin.y %f", viewSize.size.height);
[UIView animateWithDuration:0.3 animations:
^{
// Animate the views on and off the screen. This will appear to slide.
fromView.frame =CGRectMake( 320 , viewSize.origin.y, 320, viewSize.size.height);
toView.frame =CGRectMake(0, viewSize.origin.y, 320, viewSize.size.height);
}
completion:^(BOOL finished)
{
if (finished)
{
// Remove the old view from its parent.
[fromView removeFromSuperview];
//I use it to have navigationnBar and TabBar at the same time
//self.tabBarController.selectedIndex = indexPath.row+1;
}
}];
NSLog(@"swipe");
}
Segue Function in the destination view controller
- (void) prepareForSegue: (UIStoryboardSegue *) segue sender: (id) sender
{
GeneralViewController *GeneralView = (GeneralViewController*)segue.destinationViewController;
NSString *titleString;
// Set the photo if it navigates to the PhotoView
if ([segue.identifier isEqualToString:@"stepsPage"])
{
titleString = @"Steps";
GeneralView.goalStr = [NSString stringWithFormat:@"%d", [mySetting integerForKey:@"dailyStep"]];
GeneralView.currentStr = [NSString stringWithString:_stepLabel.text];
GeneralView.percentage = grpPercent.step;
}
else
{
return;
}
}
Upvotes: 2
Views: 905
Reputation: 7474
Error you are getting you dont have Naviation Controller around controller through which you want to push it to next controller
Below approaches can be applied to such situation
Approach 1 - Visually add Navigation Controller
You need to your first controller or your existing controller must be embedded in UINavigation
controller - you can do it in xcode 5 by going to Editor -> EmbedIn -> Select Navigation Controller
If you dont want Navigation to appear in your app you can use below line
self.navigationController.navigationBar.hidden =YES;
Approach 2 - Programatically add Navigation Controller
if you want to do it programatically you can do it as follows
MainViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:@"main"];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:destVC];
Upvotes: 2