Opening UIImagePickerController from within modal view controler - iphone

I am trying to present a image selector to my user while they are on a view presented as modal view.
I am building the view in a table view app. Right now the code runs but does not open the ImagePicker.
In DetailTableViewController.m
#implementation DetailTableViewController
There is an (+) button in the nav and when it is clicked I call
- (void)add
{
DetailTableViewController *controller = [[DetailTableViewController alloc]
initWithStyle:UITableViewStyleGrouped];
UINavigationController *newNavController = [[UINavigationController alloc]
initWithRootViewController:controller];
newNavController.delegate = self;
[[self navigationController] presentModalViewController:newNavController
animated:YES];
DetailTableViewController *controller2 = [[DetailTableViewController alloc]
initWithStyle:UITableViewStyleGrouped];
imgPicker = [[UIImagePickerController alloc]
initWithRootViewController:controller2];
imgPicker.allowsEditing = YES;
imgPicker.delegate = self;
//______________HERE I CREATE THE INTERFACE FOR THIS VIEW IN CODE__________________
NSLog(#"Load add View");
}
One of the button on this page is a select image button which is here:
- (IBAction)grabImage {
[newNavController pushViewController:imgPicker animated:YES];
NSLog(#"grabImage");
}
This does not present the imgPicker view. But the button click is logged.
I have in the top of file: DetailTableViewController.m
#implementation DetailTableViewController
#synthesize imgPicker;
In DetailTableViewController.h
#interface DetailTableViewController : UITableViewController <UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextViewDelegate> {
MWFeedItem *item;
NSString *dateString, *summaryString;
IBOutlet UIWebView *webview;
IBOutlet UIButton *dismissViewButton;
UIImagePickerController *imgPicker;
UINavigationController *newNavController;
IBOutlet UIButton *upload;
IBOutlet UIButton *doneEdit;
IBOutlet UIImageView *image;
}
How can I make the imgPicker display?
I have tried:
using presentModalViewController instead of push in grabImage
List item many combinations of which controller imgPicker is presented to. With some
I get the error: (Application tried to present a nil modal view controller on target)
several variations from this question:
iPhone modal view inside another modal view?
UPDATED including corrections from #aBitObvious, but the issue remains.

#aBitObvious pointed me to the answer. The local override turned out to be the answer but not on imgPicker but on newNavController.
Thanks again!

Related

Set Title with Modal View doesn´t work

I initiate the following Modal Controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *modal = [storyboard instantiateViewControllerWithIdentifier:#"modalController"];
modal.title = #"Example Title";
[self presentModalViewController:modal animated:YES];
I set the title with:
modal.title = #"Example Title";
but this doesn´t work, can anyone help me?
Edit:
I have wrapped my ModalView with a UINavigationController like this
You are going about this in a slightly convoluted way... but to stay with your paradigm, you need to present the navigationController, not the contained viewController: trying to do it the latter way will instantiate the viewController, but this action will not pull the containing navController along with it out of the storyboard. You are setting the viewController's title property ok, but you have no (automated) way to display the title. Whereas if you instantiate the navController, it's contained viewController does get unarchived along with it as it's topViewController.
//give the navigation controller a storyboard id eg "navVC"
UINavigationController* modalNav = [self.storyboard instantiateViewControllerWithIdentifier:#"navVC"];
[modalNav topViewController].title = #"Example Title";
//[self presentModalViewController:modalNav animated:YES];
//deprecated method, use this instead:
[self presentViewController:modalNavController
animated:YES
completion:nil];
You have to add a IBOutlet property for UINavigationItem (the title) in your model controller with a classic drag & drop method.
#property (weak, nonatomic) IBOutlet UINavigationItem *navTitle;
Then set the title in viewDidLoad function.
- (void)viewDidLoad
{
[super viewDidLoad];
// title view
[self.navTitle setTitle: #"atitle"];
}

How to change first simple singleview viewController to navigation viewController?

I first like to load simple viewController which shows some option and then clicking on some button I would like to load navigationController or tabbarController depending on button click. How can I do this ?
I replace the root view controller on the window when I want to switch the views.
For example in my app I show a loading screen first then I switch the view to a login screen.
To do this you need a reference to your app delegate then you can access the window property and replace the root view controller:
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
LoginViewController *loginVC = [[LoginViewController alloc] init];
appDelegate.window.rootViewController = loginVC;
In your simpleViewController :
- (IBAction) yourButtonAction:(id)sender
{
UIViewController *Vc = [[theViewControllerYouWantToShow alloc]init];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:Vc];
[self presentModalViewController:nav animated:YES];
}
Edit :
you have three options to show your viewController content :
as the example above using presentModalViewController:
add the viewController view as a subView to the current viewController.
in your case : [simpleViewController.view addSubView:nav.view];
3.or if your simple ViewController is the navigation root viewController you can push other viewControllers to its navigation stack.
in appdelegate.h
#property (strong, nonatomic) id<UIApplicationDelegate>delegate;
in appdelgate.m
#synthesize delegate;
in my first viewController's .h file
AppDelegate *myappDelegate;
-(IBAction)start:(id)sender;
in my first viewController's .m file
-(IBAction)start:(id)sender
{
NSLog(#"Start Button is clicked");
mvc = [[MasterViewController alloc]initWithNibName:#"MasterViewController" bundle:nil];
myappDelegate = [[UIApplication sharedApplication]delegate];
myappDelegate.navigationController = [[UINavigationController alloc]initWithRootViewController:mvc];
myappDelegate.window.rootViewController = myappDelegate.navigationController;
[myappDelegate.window makeKeyAndVisible];
}

Switching views when pressing button

I am trying to load a new view when I click a button in the app.
The error I am getting is -
View Switcher[6867:207] -[UIView pushViewController:animated:]: unrecognized selector sent to instance 0x6010660
and the source code snippet is -
-(IBAction) blueButtonPressed:(id)sender{
if(self.yellowViewController == nil){
YellowViewController *yellowController = [[YellowViewController alloc] initWithNibName:#"YellowView" bundle:nil];
self.yellowViewController = yellowController;
//[yellowController release];
//[self.view addSubview:yellowController.view];
//[self.view pushViewController:self.yellowViewController];
[self.navigationController pushViewController:self.yellowViewController];
[yellowController release];
}
//[self.navigationController pushViewController:self.yellowViewController animated:YES];
}
Here is the header file I am using -
#import <UIKit/UIKit.h>
#class BlueViewController;
#class YellowViewController;
#interface BlueViewController : UIViewController {
YellowViewController *yellowViewController;
BlueViewController *blueViewController;
}
-(IBAction)blueButtonPressed:(id)sender;
#property(retain) YellowViewController *yellowViewController;
#property(retain, nonatomic) BlueViewController *blueViewController;
#end
The link to the xcode project is - https://rapidshare.com/files/2403429896/View_Switcher.zip
pushViewController is a UINavigationController method.
[self.navigationController pushViewController:self.yellowViewController animated:YES];
I'm assuming you are in a UIViewController subclass
Could you post an Xcode project I need to see this.
Ok, I've look into your code, the problem is that you don't have any navigationController. Your app is structured like a view base application not like a navigation base application.
The result is that your self.navigationController == nil, that is why that call is ignored.
In your application delegate you need some code looking like this
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:switchViewController];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[window makeKeyAndVisible];
In the applicationFinised...
In your code, when you click on your switch in the toolbar, it's "working" because your using this code :
[blueViewController.view removeFromSuperview];
[self.view insertSubview:yellowViewController.view atIndex:0];
And there is no navigationController in that process.
You should use [self.view addSubview:yellowController.view];
Best to have a navigation controller where you push and pop controllers to or from.
UIView does not have pushViewController:animated: as a method.
You can push only in UINavigationController

iPhone - Navigation controller not working

I'm going mad using navigation controllers on the iPhone.
I have an app, with a main xib (the one with the window) inside wich I have put a NavigationController, inside wich I have a viewController. Everything is connected and the ViewController is defined with the correct inherited class name.
In the didFinishLaunchingWithOptions, i have :
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
In the .h I have :
#interface MainAppDelegate : NSObject <UIApplicationDelegate> {
IBOutlet UIWindow *window;
IBOutlet UINavigationController* navigationController;
}
#property (nonatomic, retain) UIWindow *window;
#property (nonatomic, retain) UINavigationController* navigationController;
#end
Then in the First ViewController I have a button connected to this method :
- (IBAction) definePreferences:(id)sender {
PreferencesController *nextWindow = [[[PreferencesController alloc] initWithNibName:#"Preferences" bundle:nil] autorelease];
UINavigationController* navController = [[[UINavigationController alloc] initWithRootViewController:nextWindow] autorelease];
[self.navigationController presentModalViewController:navController animated:YES];
}
all items in the main xib seems to be connected... and retained by the properties. The AppDelegate with its window and navigationController... the Window rootviewcontroller with the same navigationController... and the file owner with the app delegate...
Everything runs fine, but the preferences window never appears...
Can you see why ?
If needed, I must say that this first view controller makes the camera interface appear and put an overlay over it. The button is onto this overlay. The imagePicker is show like this in viewDidAppear :
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
[self presentModalViewController:picker animated:YES];
[picker release];
EDIT :
In viewDidAppear, self.navigationController is ok at the start and end of method.
In definePreferences, self.navigationController is nil. Nothing is called beetween those two calls.
Nothing
EDIT :
The problem may come from the way I init the viewController on which the button is.
Here is the method called from the firstView called by the Navigation Controller.
- (void) viewDidAppear:(BOOL)animated {
UIImagePickerController* picker = [[UIImagePickerController alloc] init];
// Set the image picker source:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
picker.showsCameraControls = NO;
picker.navigationBarHidden = YES;
picker.wantsFullScreenLayout = YES;
// Insert the overlay
OverlayViewController* overlayController = [[OverlayViewController alloc] initWithNibName:#"Overlay" bundle:nil];
picker.cameraOverlayView = overlayController.view;
// Show the picker:
[self presentModalViewController:picker animated:NO];
[picker release];
[super viewDidAppear:YES];
}
But... how should I do ?
First, never call an IBAction setPreferences:. That violates KVC, and can eventually cause all kinds of bizarre behaviors. setX: is a reserved name for the setter of the property named x.
You should not be creating a nav controller in this method (i.e. navController). You should be using the one you created in the NIB (self.navigationController). Check if that is nil. If it is, then you either didn't set up a navigation controller in the NIB, or you didn't wire it to this view controller.
You should also verify that nextWindow is non-nil.
I've finaly solved the problem.
See https://stackoverflow.com/questions/5317968/iphone-camera-overlay-going-down-after-a-modal-view-transition
I have some hair less... :-)

Use modal view as "normal" view

I want to use a modal view (UIViewController) as a "normal" view, which can be pushed on the navigation controller stack. Normally, a modal view is presented like this:
LoginViewController *myView = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
[self.navigationController presentModalViewController:navController animated:YES];
[myView release];
myView = nil;
[navController release];
navController = nil;
But I want to do something like this:
[[self navigationController] pushViewController:myView animated:YES];
The problem is that my modal view has a right and a left button. So I would have to check how the view is loaded and present the buttons in another way. The idea behind this is to have the back button. So I can use the same modal view a few times.
Edit:
#petert:
Now I followed your example. My issue is that I'm using a UINavigationBar for the modal view. To get this UINavigationBar I create a navigation controller. I'm using the navigation bar because I have my buttons in it. So checking if parentViewController is of type UINavigationController does not work for me. I'm always getting a modal view. Here is how I do it:
// load modal view
MyViewController *myView = [[MyViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:myView];
[[self navigationController] presentModalViewController:navController animated:YES];
[navController release];
navController = nil;
[myView release];
myView = nil;
// load as normal view
MyViewController *myView = [[MyViewController alloc] init];
[[self navigationController] pushViewController:myView animated:YES];
Good tips in this StackOverflow answer.
I prefer to use UIViewController's property:
#property(nonatomic, readonly) UIViewController *parentViewController
in a view controller's subclass:
Look at the value of the controller's parentViewController property. If it's an instance of UINavigationController, then you're in the navigation stack. If you're being displayed modally, it'll be an instance of your last view controller.
So in -viewDidLoad for example:
- (void)viewDidLoad
{
if ([self.parentViewController isKindOfClass:[UINavigationController class]])
{
// navigation controller
self.title = #"...";
}
else
{
// modal
self.title = #"Modal";
// add cancel and done buttons now...
}
}
Or, a pretty simple solution would be to customize your init method to your MyViewController class to encode your intent for the view controller.
Add the following to the MyViewController header:
#interface MyViewController : UIViewController
{
BOOL modal;
}
- (id)initForModal:(BOOL)isModal;
#end
Now in the implementation file:
#interface MyViewController ()
#property (nonatomic) BOOL modal;
#end
#implementation MyViewController
#synthesize modal;
- (id)initForModal:(BOOL)isModal;
{
if (self = [super initWithNibName:#"MyViewController" bundle:nil])
{
self.modal = isModal;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
if (self.modal)
{
// add cancel and done buttons …
}
else
{
// assuming we're presented from a navigation view …
}
}
Now to use this modally:
// load modal view
MyViewController *myView = [[MyViewController alloc] initForModal:YES];
Or not modally:
// load as normal view
MyViewController *myView = [[MyViewController alloc] initForModal:NO];
I'm assuming you're creating the view controller(s) from NIBs, but as always see the View Controller Progamming Guide for iOS and especially the section titled "Defining a Custom View Controller Class".
For clarification: myView isn't modal. You just present it as a modal one.
If you just push it into a UINavigationController hierarchy it will behave like a "normal" one.
You can't push the same view controller onto the navigation stack several times. Just once.
Also see this for how to customize the view:
SO modal question