Driving UIImagePickerController from UITabBarController - iphone

Here's my goal:
I'd like to show the UIImagePickerController from a TabBarController and once the photo is taken, I want the "Use" button to take me another view controller.
My problem:
I have MainCameraTabController which inherits from UIViewController and serves as the class that orchestrates the launching of UIImagePickerController and the picker's delegate. When the picker is finished, I try to launch another a different ViewController from MainCameraTabController but I get the error,
*** Assertion failure in -[UIWindowController transition:fromViewController:toViewController:target:didEndSelector:
If I put a timed delay between when the UIImagePickerController is dismissed and when I launch the next controller, it works okay but I'd like to do this more elegantly.
Is there a better way to structure my class inheritance so that I can have MainCameraTabController display the Picker and then a 2nd view controller?
// #
// # 1. Create the tab bar and add the MainCameraTabController:
// #
// tab1Controller and tab3Controller are also created
cameraTabController = [[MainCameraTabController alloc] init];
tabBarController = [[UITabBarController alloc] init];
NSArray *tabViewControllers = [NSArray arrayWithObjects:tab1Controller,
cameraTabController
tab3Controller, nil];
tabBarController.viewControllers = tabViewControllers;
self.window.rootViewController = self.tabBarController;
// #
// # 2. MainCameraTabController interface & implementation
// #
#interface MainCameraTabController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
}
- (void)showCamera;
#end
#implementation MainCameraTabController
// #
// # 3. Show the camera when the view loads
// #
- (void)viewDidLoad
{
[self startCameraController:self usingDelegate:self];
}
- (void)showNextController
{
FollowupController *fc = [[FollowupController alloc] initWithNibName:#"SomeView" bundle:nil];
// THIS IS THE PROBLEM
[self presentModalViewController:cameraPicker animated: YES];
}
- (BOOL)startCameraController:(UIViewController *)controller
usingDelegate:(id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>)pickerDelegate
{
UIImagePickerController *cameraPicker = [[UIImagePickerController alloc] init];
// configure the cameraPicker
// #
// # Apple's doc specifies that UIImagePickerController must be launched with
// # presentModalViewController
// #
[controller presentModalViewController:cameraPicker animated: YES];
}
// UIImagePickerControllerDelegate method called when photo taking is finished
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// work to handle the photo
// Dismiss the picker
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
[self showNextController];
}
#end
And on a related note, I checked to see what the picker's parentViewController is when
imagePickerController:didFinishPickingMediaWithInfo
is invoked and the parent is not MainCameraTabController but instead UITabBarController. Not sure why this is the case.

Because of the animation when dismissing the view, you need delay for the animation to finish before it can execute the next animation. You can solve this by setting the animated to NO. This will cut the animation and execute the next one immediately.
For the views, i had the similar experience, what i did was to switch the sequence of the views. I launched the view that i want to show after the picker first, in the viewDidload method, i created and display the picker so after the picker is dismissed, it will show the view that i want. If you want to make them look natural you could always play with the hidden property of the view to smoothen the flow.

Related

UIImagePickerController not showing

I am trying to show a UIImagePickerController from a button click. When I click the button, I get a SIGABRT at the line:
[self presentModalViewController:camera animated:YES];
from the code block:
camera = [[UIImagePickerController alloc]init];
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
[camera setDelegate:self.view];
camera.showsCameraControls = NO;
camera.navigationBarHidden = YES;
camera.wantsFullScreenLayout = YES;
camera.toolbarHidden = YES;
camera.cameraOverlayView = bottomArrow;
[self presentModalViewController:camera animated:YES];
where camera is the name of the UIImagePickerController defined as such:
UIImagePickerController *camera;
in the #interface.
My interface declaration is:
#interface cameraViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate> {
Can someone see what I'm doing wrong?
Besides the good point made by #Vikings, always check if your device has a camera before trying to use it:
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[camera setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[camera setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
}
Make sure you are using the both the Navigation Controller Delegate and the Image Picker Controller Delegate. The Image Picker is actually a Navigation Controller, which is why you have to implement its delegate.
#interface YourViewController : UITableViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
Also, set the delegate correctly, not to the view, but the View Controller.
camera.delegate = self;
The delegate needs to be set to the View Controller, and not the View Controller's View.
Check out the code below:
(1) You do not need to hide the navigation bar, because there is not one
(2) You do not need to hide the toolbar, because there is not one
(3) You do not need to specify wantsFullScreenLayout, because a Modal View Controller will always take up the full screen
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.showsCameraControls = NO;
// Comment out the line below to make sure it is not causing a problem.
// This just expects a view, so if bottomArrow is a view you should be fine
picker.cameraOverlayView = bottomArrow;
[self presentModalViewController:picker animated:YES];
Also, I did not realize you were loading this code in viewDidLoad, this will crash, because the View Controller itself is not finished it's transition, so you cannot begin another transition. Instead use viewDidAppear for the same effect:
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
// Place code here
}
Do not put your code in viewDidLoad create one IBAction and put your code inside that function.
and set delegate to your view controller. and in .h file make sure that your wrote
<UIImagePickerControllerDelegate>
[camera setDelegate : self];

iOS: Dismissing and Presenting ModalViewController without access to its Parent ViewController

Background: I would like to dismiss a modalView that I have presented earlier and right away present the same viewController that I just dismissed with new information.
Problem: I have not been very successful in doing so without an explicit pointer to the parent ViewController that presented the first ViewController modally. I am trying to write this class that works without messing around with the previous viewController's code.
Possible lead: There are couple of things I have been experimenting with:
1.) Trying to get access to the parent ViewController, which at this time I don't know how to.
2.) Once access to the parent is gained, I can simply apply the following code:
UIViewController* toPresentViewController = [[UIViewController alloc] init];
[self dismissViewControllerAnimated:YES completion:^{
[parentViewControllerAccessor presentModalViewController:toPresentViewController animated:YES];
}];
In theory this should work given the access to parent viewController. I am open to other ways of doing this.
Assumption: You do not have permission to change any code in the parent ViewController.
Your code looks like it should work. If you are using iOS 5 there is a UIViewController property called presentingViewController.
#property(nonatomic, readonly) UIViewController *presentingViewController;
So you can use this property to get the view controller that presented your modal controller.
Note: In iOS 4 parentViewController would be set to the presenting controller, so if you are supporting both iOS 4 and 5 you will have to check the OS version first to decide which property to access. In iOS 5 Apple have fixed this so that parentViewController is now exclusively used for the parent of contained view controllers (see the section on Implementing a Container View Controller in the UIViewController documentation).
Edit: Regarding accessing self.presentingViewController from within the block: By the time the block is called (after the modal view controller is dismissed) the presentingViewController property may get set to nil. Remember that self.presentingViewController inside the block gives the value of the property when the block is executed, not when it was created. To protect against this do the following:
UIViewController* toPresentViewController = [[UIViewController alloc] init];
UIViewController* presentingViewController = self.presentingViewController;
[self dismissViewControllerAnimated:YES completion:^
{
[presentingViewController presentModalViewController:toPresentViewController animated:YES];
}];
This is necessary not because self is gone/dismissed (it is safely retained by the block), but because it is no longer presented, therefore its presentingViewController is now nil. It is not necessary to store the presentingViewController anywhere else, the local variable is fine because it will be retained by the block.
You could accomplish this using notifications.
For example, fire this notification from outside the modal view when you want it to be dismissed:
[[NSNotificationCenter defaultCenter] postNotificationName:#"dismissModalView"
object:nil
userInfo:nil];
And then handle that notification inside your modal view:
- (void)viewDidLoad {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(dismissMe:)
name:#"dismissModalView"
object:nil];
}
- (void)dismissMe:(NSNotification)notification {
// dismiss it here.
}
the solution for ios5:
-(void)didDismissModalView:(id)sender {
// Dismiss the modal view controller
int sold=0;
if(sold==0){
//Cash_sold.delegate = self;
// Cash_sold.user_amount.text=[NSString stringWithFormat:#"%d",somme];
Cash_sold = [[CashSoldview alloc] initWithNibName:#"CashSoldview" bundle:nil];
CGRect fram1 = CGRectMake(200,20,400,400);
Cash_sold.view.superview.frame = fram1;
Cash_sold.view.frame=fram1;
Cash_sold.modalTransitionStyle= UIModalTransitionStyleCoverVertical;
Cash_sold.modalPresentationStyle=UIModalPresentationFormSheet;
UIViewController* presentingViewController = self.parentViewController;
[self dismissViewControllerAnimated:YES completion:^
{
[presentingViewController presentModalViewController:Cash_sold animated:YES];
}];
}
}
Try the following code:
[self dismissViewControllerAnimated:NO
completion:^{
// instantiate and initialize the new controller
MyViewController *newViewController = [[MyViewController alloc] init];
[[self presentingViewController] presentViewController:newViewController
animated:NO
completion:nil];
}];

Problem with NSNotification & Dismissing modal View Controllers

So, I have a tabbarcontroller, and I pass a notification to dismissModalViewController when a particular tabBarItem is touched.
It is working well and the modal View Controller is dismissed. But I want to change it in a particular way, and it does not work as I expect it to...
I have the observer initialized before the notification is posted. These are the tabBarItems -
NSArray *viewControllerss = [[NSArray alloc] initWithObjects: myProfileDataViewController,
sampleViewController,reminderInfoViewController, nil];
[self.tabBarContr setViewControllers:viewControllerss animated:YES];
self.tabBarContr.selectedIndex = 2;
I send a notification on the viewWillAppear of sampleViewController and when I choose that tabBarIcon, it dismisses the TabBarController.
BUT I want the sampleViewController to be on the left most of the UITabBar.
And so I add it like
NSArray *viewControllerss = [[NSArray alloc] initWithObjects: sampleViewController,
myProfileDataViewController, reminderInfoViewController, nil];
THIS DOES NOT DISMISS TAB BAR CONTROLLER.
Note: Please see the order in which NSArray is initialized.
The notification is posted in the viewWillAppear ofsampleViewController` and observer in the respective view controller which presents the modal view controller
Could you put a NSLog right before you post the notification?
See if you get any output when the app loads.
EDIT: Adding onto the answer based on your response
In your sampleViewController could you try this:
Make it conform to the UITabBarControllerDelegate. Your sampleViewController class interface should be something like this:
#interface SampleViewController : UIViewController <UITabBarControllerDelegate>
Then in the .m of your sampleViewController, in the viewDidLoad, set the delegate to be the sampleViewController (self in this case)
-(void) viewDidLoad
{
[super viewDidLoad];
// Assuming you have a reference to your tabBarController somewhere
[self setDelegate:self]; // try this line or the line below
// [[self tabBarController] setDelegate:self];
// The rest of your drawing code here
}
Now implement the delegate method somewhere inside the sampleViewController .m file.
-(void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
// I've included this to see if this method actually gets called or not.
NSLog(#"Dismissing modal view controller");
// check to make sure sampleViewController tab was pressed by checking
// the class type of the viewController parameter being passed in
if ([viewController isKindOfClass:[SampleViewController class]]
{
// I assume you have a pointer reference to that modal view controller
// you want to dismiss
[self dismissModalViewController:theUnwantedViewController animated:YES];
}
}
See if that works.

ImagePicker in view hierarchy problem

I can't figure out the most effective and "elegant" method for doing some task. The problem definition is:
I want to display several views, one of them is the ImagePicker with camera roll source.
The hierarchy looks similar to this:
MAIN VIEW ---> PICKER ---> IMAGE PROCESSING VIEW
When the user tap "back button" UI has to allow backward displaying.
I have tried several options:
1.
a) MAIN VIEW presents picker view modally.
b) In didFinishPickingMediaWithInfo delegate method dismiss picker modal view and after that invoke presentModalViewController with IMAGE PROCESSING VIEW.
Sample code:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* pickedImage = [info valueForKey:UIImagePickerControllerEditedImage];
LastViewController* vc = [[LastViewController alloc] init];
vc.mainImage = pickedImage;
[self dismissModalViewControllerAnimated:NO];
[self presentModalViewController:vc animated:NO];
}
Problem is that, it doesn't works, cause controller can't display next modal after dismissing another (previouse won't disappear immediately, MAIN isn't active immediately, and can't present the new one).
2.
MAIN VIEW presents IMAGE PROCESSING VIEW modally, but immediately after that IMAGE PROCESSING VIEW is presenting picker view modally, waiting for done, dismissing picker and user can see IMAGE PROCESSING VIEW with image from library.
Sample code:
in some action of main view controller:
ImageProcessViewController *vc = [[ImageProcessViewController alloc] initWithNibName:#"ImageProcessViewController" bundle:nil];
vc.delegate = self;
vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:vc animated:NO];
[vc release];
in ImageProcessViewController:
- (void)viewDidLoad {
[super viewDidLoad];
//some UI init here
if(self.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.delegate = self;
ipc.allowsEditing = YES;
[self presentModalViewController:ipc animated:NO];
[ipc release];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage* pickedImage = [info valueForKey:UIImagePickerControllerEditedImage];
self.mainImage = pickedImage;
[self dismissModalViewControllerAnimated:NO];
}
Problem is that, i can't call presentModalViewController in viewDidLoad method because it won't work (it is too early in view controller live cycle I guess). I tried also in viewDidAppear, but in that case I have to set some ivar flag, to display picker view only once and empty IMAGE PROCESSING VIEW is displayed for short time before image picker view and i don't want this.
3.
I tried to figure out how to use Navigation Controller to do this, beacause UIImagePickerController is using his nav controller, but this is private structure and documentation says programmers can't do that.
Please give me some suggestions because I really lost my way at the moment
In your main view controller, use viewDidAppear:animated: - this will be called when the modal transition has finished. You can safely start another modal transition from there.

Switching views - iPhone development

I need help figuring out how to change out the view in my application. I have a wonderfully working view that I have finished and now I'd like to be able to switch the view to a brand new, blank white screen to display.
I have these files:
HelloAppDelegate.h,
HelloAppDelegate.m,
HelloViewController.h, and
HelloViewController.m
Then, I added a new View Controller so now I have two more files:
SecondViewController.h and
SecondViewController.m
In my first view (HelloViewController), I have a button. When the user presses this button, I'd like SecondViewController to show up. So, in my HelloViewController.m, I have an action method
-(IBAction)switchToSecondView:(id)sender {
}
In this method, how can I go about initializing my second view and displaying it?
Thanks in advance!
If you want to do something like flipping view, making it modal and then returning back to the main view do following:
Define a delegate to indicate that secondary view finished its work
#protocol FlipsideViewControllerDelegate
- (void)flipsideViewControllerDidFinish;
#end
In main view do following:
- (void)flipsideViewControllerDidFinish {
[self dismissModalViewControllerAnimated:YES];
}
- (IBAction)showInfo {
FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:#"FlipsideView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
In secondary view do following:
- (IBAction)done {
[self.delegate flipsideViewControllerDidFinish];
}