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];
}];
Related
On my project we have three windows A, B and C. From A I would like to push view B, and from B I would like to present view C.
My code:
ViewController A:
ViewControllerB *vcB = [[viewControllerB alloc]
initWithNibName:#"ViewControllerB" bundle:nil];
[[self navigationController] pushViewController:vcB animated:YES];
View Controller B:
ViewControllerC *vcC = [[ViewControllerC alloc]
initWithNibName:#"ViewControllerC" bundle:nil];
[self presentViewController:vcC animated: true completion: nil];
Everything is ok until now, but when I dismiss the last view controller with:
[[self presentingViewController] dismissViewControllerAnimated:NO completion:nil];
The app goes back to first view controller (vcA) instead the second one (vcB)
What am I doing wrong?
Thank you, guys.
You must be doing something else that you're not telling us about...
This works as expected:
In MyFirstViewController.m
- (IBAction)pushTapped:(id)sender {
MyPushedViewController *vc = [[MyPushedViewController alloc] initWithNibName:#"MyPushedViewController" bundle:nil];
[self.navigationController pushViewController:vc animated:YES];
}
In MyPushedViewController.m
- (IBAction)presentTapped:(id)sender {
MyPresentedViewController *vc = [[MyPresentedViewController alloc] initWithNibName:#"MyPresentedViewController" bundle:nil];
[self presentViewController:vc animated:YES completion:nil];
}
In MyPresentedViewController.m
- (IBAction)dismissTapped:(id)sender {
[self dismissViewControllerAnimated:NO completion:nil];
}
Tapping the "dismiss" button in MyPresentedViewController dismisses the presented view controller (your vcC), leaving me at MyPushedViewController (your vcB) ... NOT at MyFirstViewController (your vcA).
try this to dismiss vc
[self dismissViewControllerAnimated:NO completion:nil]
and use this to pop
[self.navigationController popToViewController:controller animated:YES];
[self presentingViewController] = Bvc , so here you actually dismissing B not C
[Bvc dismissViewControllerAnimated:NO completion:nil];
but self = Cvc so here you dismissing C only
[self dismissViewControllerAnimated:NO completion:nil];
I wanted to Push New View Controller with Transparent Background on top of one View Controller which is already shown. I know How to PRESENT but I wanted to PUSH new View Controller.
UIViewController *controller = [[UIViewController alloc] init];
self.definesPresentationContext = YES;
controller.modalPresentationStyle = UIModalPresentationOverCurrentContext;
[self presentViewController:controller animated:YES completion:^{}];
You need to give memory to your ViewController and set to RootController of UINavigationController. After that, you can push it from your current controller...
UIViewController *yourViewController = [[UIViewController alloc] init];
UINavigationController *navigationController1 = [[UINavigationController alloc] initWithRootViewController:yourViewController];
[self presentViewController:navigationController1 animated:YES completion:nil];
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
I'm presenting an instance of CNContactViewController in my app. I want the user to be able to both edit the contact, as well as to dismiss this view controller. Below is the code to present the view controller, which is embedded in a UINavigationController. As you can see in the code, I have allowsEditing = YES, but looking at the screenshot; you can see that I'm not able to edit. Anyone able to help me see what I'm missing? Thanks!
CNContactViewController *contactController = [CNContactViewController viewControllerForUnknownContact:contact];
contactController.allowsEditing = YES;
contactController.delegate = self;
contactController.contactStore = store;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
EDIT: I tried a different method suggested by #WrightCS:
[self presentViewController:contactController animated:YES completion:nil];
And, made sure to add this delegate method:
- (void)contactViewController:(CNContactViewController *)viewController
didCompleteWithContact:(CNContact *)contact{
[self dismissViewControllerAnimated:YES completion:nil];
}
But, repeatedly get this error log:
[CNUI ERROR] Contact view delayed appearance timed out
Instead of creating a UINavigationController instance, try presenting the contact controller directly.
[self presentViewController:contactController animated:YES completion:nil];
CNContactViewControllerDelegate
- (void)contactViewController:(CNContactViewController *)viewController
didCompleteWithContact:(CNContact *)contact;
Present on the main thread
dispatch_async(dispatch_get_main_queue(), ^{
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:contactController];
navController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentViewController:navController animated:YES completion:nil];
});
The problem ended up being that I was initializing the CNContactViewController incorrectly...instead of:
CNContactViewController *contactController = [CNContactViewController viewControllerForUnknownContact:contact];
Which would display a contact, but not allow for the option of cancelling out of the contact view or editing it, the correct option (for my scenario) is to use
CNContactViewController *contactController = [CNContactViewController viewControllerForNewContact:contact];
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