Objects return null after presentModalViewController - objective-c

After showing a camera, my objects return null:
Here's how I show my UIImagePickerController (I initialize it before):
[self presentModalViewController:pickerOne animated:NO];
I add a object in my scrollView like this,
UIImageView *overlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"black_overlay.png"]];
[overlay setFrame:CGRectMake(((columnCount-1)*100)+((columnCount-1)*5)+5 ,((rowCount-1)*100)+((rowCount-1)*5)+11, 100, 100)];
[overlay.layer setCornerRadius:8.0];
[overlay.layer setMasksToBounds:YES];
[overlay setTag:12];
[theImageViewer addSubview:overlay];
And access it like this (after the camera has been closed):
UIImageView *overlayImage = (UIImageView*)[theImageViewer viewWithTag:[[arrayOverlays objectAtIndex:[arrayIds indexOfObject:imageThumbnail]] intValue]];
NSLog(#"OverlayImage: %#",[arrayOverlays objectAtIndex:[arrayIds indexOfObject:imageThumbnail]]);
And I get: Real overlayImage, (null)
Please help! Thanks.

The pickerOne must be a property inside your current ViewController.
Your current ViewController must conform to protocol of UIImagePickerControllerDelegate.
In the imagePickerController:didFinishPickingMediaWithInfo: implementation from the delegate you'll get the image object and dismiss the modalViewController.
That should do the trick.
Edit: just found a tutorial that explains this in detail:
http://iphone.zcentric.com/2008/08/28/using-a-uiimagepickercontroller/
Note:imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo is deprecated in iOS 3.0

I realized I was calling a action from a different class right after I dismissed the view. I simply made it a NSNotification and works fine.

Related

Where to set popover size?

I have a UIViewController called HomeViewController and it has a model that contains an array of data. HomeViewController also has a button that when pressed will show a UITableViewController that display's the model's array of data.
My question is, what is the standard/best way to set the popover's size? I only want the popover to be tall enough to display the data, but no taller. What is common practice? I assume that I need to set contentSizeForViewInPopover at some point but I'm not sure where...In the viewDidLoad method of the popover class? In the prepareForSegue method?
Here's the code I have so far: (Note that DataPresentingViewController is my popover view controller class`)
//In HomeViewController
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.destinationViewController isKindOfClass:[DataPresentingViewController class]])
{
DataPresentingViewController *dest = (DataPresentingViewController *) segue.destinationViewController;
dest.mySavedData = self.myModel.mySavedData;
}
}
I know that I could just set the contentSizeForViewInPopover here in the prepareForSegue method; however, this seems like something the popover class should deal with.
As of iOS7 contentSizeForViewInPopover is deprecated. You should use UIViewController's preferredContentSize property instead.
Hope this helps!
set contentSizeForViewInPopover in ViewDidLoad method in HomeViewController
contentSizeForViewInPopover is deprecated in iOS 7
The property: popoverContentSize of UIPopoverController represents the size of the content view that is managed by the view controller in the contentViewController property of UIPopoverController.
Reference
The advantage is that it is available in iOS 3.2 and later, so you don't need to check device version everytime, just replace contentSizeForViewInPopover method with your UIPopOverController object instance.
Have you tried:
setPopoverContentSize:<#(CGSize)#> animated:<#(BOOL)#>
in your segue logic block. Useful if you want the popover size to be variable based upon a data set, or view placement or whatever.
// self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0); //Deprecated in ios7
self.preferredContentSize = CGSizeMake(320.0, 600.0); //used instead
A little known trick to sizing your UIPopoverViewController to match the height of your UITableView is to use the tableView's rectForSection method to give you the height. Use the height in your viewController's contentSizeForViewInPopover like this:
- (CGSize)contentSizeForViewInPopover {
// Currently no way to obtain the width dynamically before viewWillAppear.
CGFloat width = 200.0;
CGRect rect = [self.tableView rectForSection:[self.tableView numberOfSections] - 1];
CGFloat height = CGRectGetMaxY(rect);
return (CGSize){width, height};
}
Try the following code:
- (IBAction)setData:(id)sender
{
UIViewController *popoverContent=[[UIViewController alloc] init];
UITableView *tableView=[[UITableView alloc] initWithFrame:CGRectMake(265, 680, 0, 0) style:UITableViewStylePlain];
UIView *popoverView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 300)];
popoverView.backgroundColor=[UIColor whiteColor];
popoverContent.view=popoverView;
popoverContent.contentSizeForViewInPopover=CGSizeMake(200, 420);//setting the size
popoverContent.view=tableView;
tableView.delegate=self;
tableView.dataSource=self;
self.popoverController=[[UIPopoverController alloc] initWithContentViewController:popoverContent];
[self.popoverController presentPopoverFromRect:CGRectMake(400, 675, 0, 0) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];
}
It looks like you want to do it programmatically, but in the storyboard, before you hook up a view controller to a segue, under the attributes inspector there is a "popover, use explicit size option." Maybe you can set the size that would work the best for your App first and not worry about the size with using code. Hope this helps, let us know how it goes.

Fullscreen iPad UIPopoverController

I have an iPad app which displays a UIImagePicker through a UIPopoverController.
I would like to make the popovercontroller fullscreen (or at least as big as possible).
I'm using the presentPopoverFromRect method with a new CGRect which I have set to various widths and heights with no result. The source of the imagepicker is UIImagePickerControllerSourceTypePhotoLibrary.
UIPopoverController* popoverController = [[UIPopoverController alloc] initWithContentViewController:photoPicker];
popoverController.delegate = self;
popoverController presentPopoverFromRect:CGRectMake(0, 0, 2500, 2500) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Can I even make this fullscreen? What about sourcetype UIImagePickerControllerSourceTypeCamera?
To set popover size you can override -contentSizeForViewInPopover in viewcontroller which is
placed in popovercontroller.
presentPopoverFromRect is used for defining frame from which popover will be opened, not for setting it's size.
I just verified that this code works on an iPad 1 and iPad 4 (meaning it is supported in iOS 5.1.1 - 6.1.2)
popover = [[UIPopoverController alloc] initWithContentViewController:yourVC];
[popover setPopoverContentSize:CGSizeMake(1024, 1024)];
[popover presentPopoverFromRect:CGRectZero
inView:appDelegate.splitViewController.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
You should note that it's difficult to dismiss the popover since it will "fill the screen" in portrait or landscape. I do have these methods in my appDelegate.splitViewController:
- (BOOL)shouldAutomaticallyForwardRotationMethods {
NSLog(#"MG - shouldAutomaticallyForwardRotationMethods");
return YES;
}
- (BOOL)automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers {
NSLog(#"MG - automaticallyForwardAppearanceAndRotationMethodsToChildViewControllers");
return YES;
}
Just unhide/hide a button in a corner of yourVC to dismiss with this:
[popover dismissPopoverAnimated:YES];
It should also be noted that "popover" is a strong property, or you will get a deallocated message when it tries to present.
You might also want a ModalViewController instead? This one has 4 different presentationstyles (one of them fullscreen)
A good example is the prototype written in this question: presentModalViewController fullscreenmode issue
(it's in the //---EDIT---//)
Check it out if it's something for you.

Create subview on awakeFromNib

I'm trying to create a NSImageView programmatically as a subview of another NSImageView when awakeFromNib is called.
My code is as follows (Fader is defined in MyImageView.h):
#implementation MyImageView
- (void)awakeFromNib {
Fader = [NSImageView initWithFrame: [self frame]];
}
I get the warning message "NSImageView may not respong to +initWithFrame". When I build, the app simply frizzes without showing anything, and I have to "force quit".
What am I doing wrong?
You’ve forgotten to send +alloc in order to allocate the object. Change that line to:
Fader = [[NSImageView alloc] initWithFrame: [self frame]];

UINavigationBar Background Image Problem

i have set my navigationbar background with this code in my App delegate:
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect {
UIImage *backgroundImage = [UIImage imageNamed: #"Nav-rightpane.png"];
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(0, 0, self.frame.size.width, self.frame.size.height),
backgroundImage.CGImage);
}
#end
This works perfect but now i must change the background to the default at UIPopoverController. Any idea for this?
Thanks
Do not use this solution. Simply delete your UINavigationBar Category and implement your own UINavigationController.
#interface XNavigationController : UINavigationController{}
and change override viewDidLoad method in implementation
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *img = [UIImage imageNamed: #"Nav-rightpane.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
[imageView setFrame:self.navigationBar.bounds];
[imageView setContentMode:UIViewContentModeScaleToFill];
[self.navigationBar addSubview:imageView];
[imageView release];
}
so use this navigationController when you want to change the background of navigation bar.
The correct solution pre-iOS5, as recommended by Apple engineers is to not use categories and to not use method swizzling. This is confirmed to break on iOS 5 which is very close to being released.
Instead, you should create a subclass of UINavigationBar, override the drawRect: method, and then use Interface Builder to define your navigation controller. Interface Builder will let you change the classname for the navigation bar to your custom subclass:
If you want to avoid Interface Builder, you can create a temporary XIB and load an instance of the navigation controller from it, then use NSKeyedArchiver to archive it to a file. Then use xxd -i <filename> on the command line to generate C-code for the raw bytes of the navigation controller. Then paste that into a source file and unarchive it when you want an instance.
It seems like a lot of work, but the WWDC videos and engineers on the forums have repeatedly stated that this is the only safe and reliable way to do what you want.
In iOS 5 this all becomes a whole lot easier.

UIView transition and animation

I understand modal views cover the entire screen. But I really want a view that covers only half the screen just like the keyboard. So, please tell me why this doesn't work
MyController *controller = [[MyController alloc] initWithNibName:#"MyView" bundle:nil];
CGRect frame = CGRectMake(0,44,768,264);
[controller view].frame = frame;
contoller.delegate = self;
[[self view] addSubView:[controller view]];
[controller release];
I am trying to add a sub view to my current view and make it appear where the keyboard appears.
It throws a BAD ACCESS exception
In my code (above), I was using a custom UIViewController with it's own view [set to UIView on IB]. I couldn't get it to work by setting frame for the view controller's view.
So I added a custom UIView without a Nib file with all the controls (buttons, textfields) added on initWithFrame.
MyCustomView = [[MyCustomView] alloc] initWithFrame:frame delegate:self];
[self.view addSubView:MyCustomView];
Thanks for your comment, Jacob.