Update at bottom of question
Here's what I'm looking to do:
Main ViewController opens a new ViewController (i don't want a navigation bar, so do I have to do this modaly?) which then opens the camera.
In the main view controller I have this being called on a button press:
CameraProcessViewController *controller = [[CameraProcessViewController alloc] initWithNibName:#"CameraProcessViewController" bundle:nil];
[self presentModalViewController:controller animated:NO];
Which opens up the next controller. In that next controller in the viewDidLoad method I have:
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
[imagePicker setDelegate: self];
[imagePicker setAllowsEditing:NO];
[self presentModalViewController:imagePicker animated:YES];
NSLog(#"Has camera");
} else {
NSLog(#"Has no camera");
}
I've tried both presentModalViewController and presentViewController and every combination of the two between the main view controller and this child one, however the camera never opens. When I move the camera code into the main view controller it opens fine.
I've looked and looked and I can't find anything saying why this would be.
Thanks!
UPDATE
I found that in the viewDidLoad method I can call this:
[self performSelector:#selector(openCamera) withObject:nil afterDelay:0];
And it loads it up just fine. Is this okay or is this not recommended? Seems a tad hacky!
viewDidLoad is not the right place to do this. You could try viewWillAppear but that might be too early, too. Otherwise, viewDidAppear.
Related
I am using AddressBookUI Framework for Adding contact, when I tried to pushing this view controller then cancel and done button not working properly, I don't want to present it
Here is my code
ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
[self.navigationController pushViewController:abnpvc animated:YES];
I am also tried add as subview rather then pushing it but when I am adding as subview then it was not added
As per comment i have tried like
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:abnpvc];
[self presentViewController:navigation animated:YES completion:nil];
Can anyone help me out why properly not working ?
You can implement that too considering also the other answers and the deprecations to ABNewPersonViewController in iOS 9.
As per your remarks:
cancel and done button not working properly
They are working if you have included the ABNewPersonViewControllerDelegate on interface like this:
#interface ViewController () <ABNewPersonViewControllerDelegate>
Pushing the viewController on navigation stack like this:
ABNewPersonViewController *controller = [[ABNewPersonViewController alloc] init];
controller.newPersonViewDelegate = self;
[self.navigationController pushViewController:controller animated:YES];
And by conforming to the protocol by implementing this method:
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(nullable ABRecordRef)person {
// Trick to go back to your view by popping it from the navigation stack when done or cancel button is pressed
[self.navigationController popViewControllerAnimated:YES];
}
The tricky line is to pop the newPersonController from the navigation stack when either Done or Cancel button are pressed.
Enjoy it
Why can't you just do it as the docs say?
It is recommended that you present a new-person view controller modally.
Use
ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
[self presentViewController:abnpvc animated:YES completion:nil];
That should work fine.
Edit
On second thought, did you set your delegate correctly and do the implementations get called? I suspect they are not implemented or the delegate is not set correctly.
Apple guideline(IMPORTANT) :: New-person view controllers must be used with a navigation controller in order to function properly. It is recommended that you present a new-person view controller modally.
Add Delegate
#interface ViewController () <ABNewPersonViewControllerDelegate>
Pushing the viewController
ABNewPersonViewController *abnpvc = [[ABNewPersonViewController alloc] init];
[abnpvc setNewPersonViewDelegate: self];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:abnpvc];
[self presentModalViewController:navController animated:YES];
And Now Add Delegate Method
#pragma mark ABNewPersonViewControllerDelegate methods
- (void)newPersonViewController:(ABNewPersonViewController *)newPersonViewController didCompleteWithNewPerson:(ABRecordRef)person
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
That will work fine.
I am trying to switch views in my application. I have the following code to take me from the Main View to the first level view:
-(IBAction)levelOneButton
{
levelOneView *testView = [[levelOneView alloc] initWithNibName:nil bundle:nil];
testView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:testView animated:YES completion:NULL];
}
Which works GREAT! Then the Code to take me to the next level is the exact same but slightly different!
-(IBAction)nextLevelButton
{
LevelTwoView *levelTwo = [[LevelTwoView alloc] init];
levelTwo.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:levelTwo animated:YES completion:nil];
}
Now when I want to go back to the Main menu from here is where the issue occurs. I have the following code which only dismisses the level2 and shows level1 again.
- (IBAction)goBackButton {
[self dismissViewControllerAnimated:YES completion:NULL];
}
Like I said when this code is used it just releases the current view and takes you back to the last view instead of taking you back to the main menu. After realizing it was not working I tried the following code to take me back to the main menu.
- (IBAction)goBackButton {
ViewController *mainMenu = [[ViewController alloc] init];
mainMenu.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:mainMenu animated:YES completion:nil];
}
When this code is run, I get presented with a black screen.
My question remains how may I go back to the view ViewController? Thank you so much!!
Try this when dismissing the second level view controller.
[[self presentingViewController] presentingViewController] dismissModalViewControllerAnimated:YES]
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];.
I read a lot of documentation about this before i decided to ask.
So I have navigationController in my app. When user enters the first time in my app I do this
RegViewController *opr = [[RegViewController alloc] init];
self.regController = opr;
[self.navigationController presentModalViewController:self.regController animated:NO];
[opr release];
And it works fine. But when I click OK button in RegViewController I call this method
-(IBAction)btn_regPressed:(id)sender
{
NSLog(#"start to dissmis modal");
[self.navigationController dismissModalViewControllerAnimated:YES];
//[self.navigationController.parentViewController dismissModalViewControllerAnimated:YES];
}
But this code doesn't want to dismissModalViewController
Can somebody help me with thios issue please. Thanks
try to change your call to (remove the navigationController):
[self presentModalViewController:self.regController animated:NO];
and in your button (remove the navigationController):
[self dismissModalViewControllerAnimated:YES];
You are telling to your navigationController dismiss your modal, but your viewcontroller should do it.
Navigation controllers are used to navigation (go and back) purposes. And probably, your navigationController is nil.
In your RegViewController, try to dismiss self like so:
-(IBAction)btn_regPressed:(id)sender
{
NSLog(#"start to dissmis modal");
[self dismissModalViewControllerAnimated:YES];
}
I have been beating my head on this for a few hours. I have some sample code (using a UINavigationController) when the view loads the camera roll will be presented. However, when I try to incorporate the same code into my app, which has a tabBarController, I get a blank modal UIImagePickerController. I didn't track down what I am doing wrong.
// bring up image picker
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum]) {
NSLog(#"UIImagePickerControllerSourceTypePhotoLibrary available");
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.delegate = self;
ipc.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
ipc.allowsEditing = YES;
[self.tabBarController presentModalViewController:ipc animated:YES];
[ipc release];
}
Any insight would be appreciated.
Not sure what has changed, but this is possible by calling presentViewController from your tabBarController. This is the standard now and ensures your camera or image picker always gets presented as a full screen modal view.
For reference: presentViewController