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];
Related
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];
Im making sending an email available for the user with this code:
MFMailComposeViewController *vc = [MFMailComposeViewController new];
[vc setSubject:#"Test Subject"];
[vc setMessageBody:#"Test Body" isHTML:NO];
[vc setMailComposeDelegate:self];
[self presentViewController:vc animated:YES completion:nil];
This opens a ViewController with all the stuff you need to sen an email, but it completely wipes everything from the ViewController the user is previously on. It only removes the subviews because the root view is still there because the backgroundColor is still the same.
I have already tried initWithRootViewController: but it crashes.
What is happening?
I found the bug... It wasn't in the code above. It seems the viewWillDissapear: is getting called when presenting the mailVC :/
In there I have code to remove every subview, so yeah, found the problem thanks anyways for those who answered and sorry for the inconvenience.
I'm not sure what you mean by "completely wipes everything". But suposing this is for iPad (and not iPhone), when presenting a view controller it goes full screen by default. If you want to change that, you have to set the modalPresentationStyle of the presented view controller (MFMailComposeViewController in your case)
Your code would look like this:
MFMailComposeViewController *vc = [MFMailComposeViewController new];
[vc setSubject:#"Test Subject"];
[vc setMessageBody:#"Test Body" isHTML:NO];
[vc setMailComposeDelegate:self];
vc.modalPresentationStyle = UIModalPresentationFormSheet; //You can use custom size too
[self presentViewController:vc animated:YES completion:nil];
I am trying to have it so a user can choose a photo from their album into an imageview. It seems to work fine if I'm using a push segue to that viewcontroller that has the button to perform the action, but when I change it to modal segue, nothing happens when I click the button to run the function. What is the cause of this exactly?
Method:
- (IBAction)choosePhotoFromAlbum:(id)sender {
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self.navigationController presentViewController:imgPicker animated:YES completion:nil];
}
You're trying to present it from the navigation controller, but your view controller is modal. Change
[self.navigationController presentViewController:imgPicker animated:YES completion:nil];
to
[self presentViewController:imgPicker animated:YES completion:nil];.
you're trying to present it from the navigation controller, but your view controller is modal. so just write
[self presentViewController:YourImagePickerController animated:YES completion:nil];.
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];
}];
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