I have xib of passcode and after I tap on login button there will be web service that will call and after success response, I have to present xib of passcode.
When I complete that portion I have to push to another view controller from presented xib.
Here is my code:
[self dismissViewControllerAnimated:NO completion:^{
if ([_delegate respondsToSelector:#selector(unlockWasSuccessfulLockScreenViewController:pincode:)]) {
[_delegate unlockWasSuccessfulLockScreenViewController:self pincode:pincode];
PassCodeVC *sgn = [self.storyboard instantiateViewControllerWithIdentifier:#"PassCodeVC"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:sgn];
[nav pushViewController:sgn animated:YES];
}
}];
and I have presented xib like this:
JKLLockScreenViewController * viewController = [[JKLLockScreenViewController alloc] initWithNibName:NSStringFromClass([JKLLockScreenViewController class]) bundle:nil];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:viewController];
[viewController setLockScreenMode:LockScreenModeNew]; // enum { LockScreenModeNormal, LockScreenModeNew, LockScreenModeChange }
[viewController setDelegate:self];
[viewController setDataSource:self];
[viewController setTintColor:[UIColor colorWithRed:53.0 / 255.0 green:115.0 / 255.0 blue:157.0 /255.0 alpha:1]];
[self presentViewController:nav animated:YES completion:nil];
If you wish to use current navigation then Just simple pass new controller using current navigation controller.
PassCodeVC *sgn = [self.storyboard instantiateViewControllerWithIdentifier:#"PassCodeVC"];
[self.navigationController pushViewController:childViewController animated:YES];
You may need to set the Storyboard ID of the View Controller you are trying to load. This is located in the inspector, just below where you assign a custom class to your view controller.see this image
Related
I have a UIViewController, and inside that view controller I would like to call a CustomUIViewController without the use of Interface Builder. I've been using this and it works but it requires a storyboard ID.
UIStoryboard *storyboard = self.storyboard;
UIViewController *myVC = (UIViewController *)[storyboard instantiateViewControllerWithIdentifier:#"PageID"];
[self presentModalViewController:myVC animated:YES];
Is there a way of going from a UIViewController to a customViewController without the use of segues and IB?
Use this:
UIViewController *myVc = [[UIViewController alloc] init];
[self presentViewController:myVc animated:YES completion:nil];
This will obviously present a new blank view controller, if you want your PageID view then you replace UIViewController with the name of the PageID class (i,e the .h/.m file names)
So i'm assuming this:
PageID *myVc = [[PageID alloc] init];
[self presentViewController:myVc animated:YES completion:nil];
If you want it inside a navigation controller do:
PageID *myVc = [[PageID alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myVc];
[self presentViewController: navController animated:YES completion:nil];
OR if you don't want it to be modal (don't need to create a new navigation controller as it will be added to the existing navigation controller stack i.e. [self navigationController]:
[[self navigationController] pushViewController:myVc animated:YES];
Currently, when a button is tapped, a UIModalPresentationSheet comes up. I'd like to add a navigation bar at the top of this when it slides up. I've tried a lot of things but nothing seems to work. Here's what i'm currently trying and it returns this error.
AthleteAdd *addAthlete = [self.storyboard instantiateViewControllerWithIdentifier:#"addAthlete"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addAthlete];
//[self.navigationController pushViewController:addAthlete animated:YES];
addAthlete.delegate = self;
addAthlete.modalPresentationStyle = UIModalPresentationFormSheet;
// UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:addAthlete];
[self presentViewController:navigationController animated:YES completion:nil];
But it pushes it up modally, and without the modalpresentationsheet form. How can I make it so the navigation controller is sized correctly?
Try to change your code like this :
AthleteAdd *addAthlete = [self.storyboard instantiateViewControllerWithIdentifier:#"addAthlete"];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:addAthlete];
addAthlete.delegate = self;
navigationController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navigationController animated:YES completion:nil];
Because here, you try to present addAthlete from itself. So you get this error.
You should present navigationController in which you encased your addAthlete.
[self presentViewController:navigationController animated:YES completion:nil];
You are presenting from current viewcontroller itself.
Try something like,
[self dismissViewControllerAnimated:YES completion:^{
[self.parentViewController presentViewController: navigationController animated:YES completion:nil];
}];
This only accours if you are presenting in a view controller that is managed by a navigation controller.
The reproduction steps are:
1 - Present a view controller using UIModalPresentationCurrentContext
self.definesPresentationContext = YES;
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
2 - Present a view controller over the top using the default full screen presentation style
ViewController* viewController = [[ViewController alloc] init];
[self presentViewController:viewController animated:YES completion:nil];
3 - Dismiss the top presented view controller (the full screen one)
[self dismissViewControllerAnimated:YES completion:nil];
Now the problem is the 2nd view controller (presented using UIModalPresentationCurrentContext) disappears. Also it is impossible to present another view controller using UIModalPresentationCurrentContext, because the system thinks its still there.
I believe the issue is a bug in the framework. As mentioned it only occurs when the presenting in a view controller managed by a navigation controller. There is a nasty work around which uses the containment API. It creates a dummy view controller which views are presented from. The steps are:
1 - When presenting a view in context who's parent is a navigation controller, use a dummy view controller:
- (void)presentInContext
{
UIViewController* presentOnViewController = self;
if ([self.parentViewController isKindOfClass:[UINavigationController class]])
{
// Work around - Create an invisible view controller
presentOnViewController = [[DummyViewController alloc] init];
presentOnViewController.view.frame = self.view.frame;
// Containment API
[self addChildViewController:presentOnViewController];
[self.view addSubview:presentOnViewController.view];
[presentOnViewController didMoveToParentViewController:self];
presentOnViewController.definesPresentationContext = YES;
}
ViewController* viewController = [[ViewController alloc] init];
viewController.modalPresentationStyle = UIModalPresentationCurrentContext;
[presentOnViewController presentViewController:viewController animated:YES completion:nil];
}
2 - When dismissing the view controller tidy up
- (void)dismissSelf
{
__weak UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^{
// Remove the dummy view controller
if ([presentingViewController isKindOfClass:[DummyViewController class]])
{
[presentingViewController willMoveToParentViewController:nil];
[presentingViewController.view removeFromSuperview];
[presentingViewController removeFromParentViewController];
}
}];
}
Thats it... The fix is dirty, but does the trick with no visual flicker.
Is it possible inside Xcode to add a button inside a popover that will take the user to a new view?
Yes.
[myPopOver.contentViewController.view addSubview:myButton];
[myButton addTarget:self action:#selector(goToNextView) forControlEvents:UIControlEventTouchUpInside];
- (void)goToNextView
{
//if you are using xibs use this line
UIViewController *controller = [[UIViewController alloc] initWithNibName:#"myXib" bundle:[NSBundle mainBundle]];
//if you are using storyboards use this line
UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"myViewControllersID"];
//to present the controller modally use this
[self presentViewController:controller animated:YES completion:nil];
//or if you are pushing to this controller using a navigation controller use this
[self.navigationController pushViewController:controller animated:YES];
}
Im calling this code from the MasterViewController in a UISplitVC for an iPad app:
-(void)viewWillAppear:(BOOL)animated{
//PRESENT MODALVC
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[self setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:modalVC animated:YES];
}
but it doesn't work. No ModalVC appears.
Try this code:
ModalViewController *modalVC = [[ModalViewController alloc] initWithNibName:#"ModalViewController" bundle:nil];
[modalVC setModalPresentationStyle:UIModalPresentationFullScreen]; //You set the presentation style of the controller that would be presented, not the presenting controller
//This check is needed, because presentModalViewController:animated is depreciated in iOS5.0 and presentViewController:animated:completion must be used instead. The same is valid for dismissModalViewControllerAnimated and dismissViewControllerAnimated:completion
if([self respondsToSelector:#selector(presentViewController:animated:completion:)])
[self presentViewController:modalVC animated:YES completion:nil];
else
[self presentModalViewController:modalVC animated:YES];
If you are targeting iOS5.0+ only this check is not needed and you should use only presentViewController:animated:completion and dismissViewControllerAnimated:completion