UIDatePicker memory leak in IOS 8.3 - ios

I am seeing memory leaks in UIDatePicker when used in a popover on an iPad running IOS 8.3. I'm getting approx 5K in multiple memory leaks every time the date picker is popped up and then dismissed. The leaked object is NSDateComponents, and the responsible frame is [_UIDatePickerMode _yearlessYearForMonth:].
I have written a simple test app to demonstrate the problem (https://github.com/david-ape/datepickertest/). I've included both a UIPopoverController option and a UIPopoverPresentationController option, but it doesn't seem to matter which is used.
Am I doing something wrong, or is there a workaround, or do I need to wait for a fix from Apple? If the latter, then can anyone suggest a third party control that I could use in place of UIDatePicker?
Below is the code I'm using to pop up the date pickers.
Header file
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController <UIPopoverControllerDelegate,
UIPopoverPresentationControllerDelegate>
#end
Implementation file
#import "ViewController.h"
#interface ViewController ()
#property (nonatomic, strong) UIPopoverController *ios7Popover;
- (IBAction)datePickerPopupIOS7:(UIButton *)sender;
- (IBAction)datePickerPopupIOS8:(UIButton *)sender;
#end
#implementation ViewController
// helper - returns a view controller containing a date picker for use in a
// popup
+ (UIViewController *)buildDatePickerViewController
{
CGRect frame = CGRectMake(0, 0, 350, 216);
UIViewController *viewController = [[UIViewController alloc]init];
viewController.preferredContentSize = frame.size;
UIDatePicker *datepicker = [[UIDatePicker alloc]initWithFrame:frame];
datepicker.datePickerMode = UIDatePickerModeDate;
datepicker.hidden = NO;
datepicker.date = [NSDate date];
[viewController.view addSubview:datepicker];
return viewController;
}
// popup date picker using UIPopoverController (IOS7 compatible)
- (IBAction)datePickerPopupIOS7:(UIButton *)sender
{
UIViewController *viewController = [ViewController buildDatePickerViewController];
self.ios7Popover = [[UIPopoverController alloc]initWithContentViewController:viewController];
self.ios7Popover.delegate = self;
[self.ios7Popover presentPopoverFromRect:sender.frame
inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown| UIPopoverArrowDirectionLeft|UIPopoverArrowDirectionRight)
animated:YES];
}
// popup date picker using UIPopoverPresentationController (IOS8 or later required)
// Thanks to http://stackoverflow.com/a/26944036/1764243 for how to do this
- (IBAction)datePickerPopupIOS8:(UIButton *)sender
{
if ([UIPopoverPresentationController class])
{
UIViewController *viewController = [ViewController buildDatePickerViewController];
UINavigationController *destNav = [[UINavigationController alloc] initWithRootViewController:viewController];
destNav.modalPresentationStyle = UIModalPresentationPopover;
UIPopoverPresentationController *popover = destNav.popoverPresentationController;
popover.delegate = self;
popover.sourceView = self.view;
popover.sourceRect = [sender frame];
destNav.navigationBarHidden = YES;
[self presentViewController:destNav animated:YES completion:nil];
}
else
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Not supported"
message:#"UIPopoverPresentationController not supported in this version of IOS"
delegate:nil
cancelButtonTitle:#"Ok"
otherButtonTitles:nil];
[alert show];
}
}
#pragma mark - UIPopoverControllerDelegate methods
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
self.ios7Popover = nil;
}
#end

Related

Custom Views aren't dismissing keyboards?

Have tried [[self jotNotes] resignFirstResponder]; , have tried [self endEditing:YES];
So I have my NoteViewController, which inherits UIViewController, and tries to implement the delegate like so
#interface NOTEController : UIViewController <UITextViewDelegate>
#end
#implementation NOTEController
-(id)init {
self = [super init];
if (self) {
// self.delegate = self; //doesnt let me set this, so i assume i do not do that here
NOTEControllerView * mainView = [[NOTEControllerView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.view = mainView; //just a plain custom uiview subclass its boring and not special
}
return self;
}
#end
and then in the mainView, i have a bunch of sub-views that are basically a square with a UITextView inside.
the squares class is like this, and they're the ones im trying to dismiss the keyboard from, heres where i set the delegate, the dismissKB method, and the UITextView code. Currently, it will log my keyboard method when pressing the done button, but the keyboard is still present. Would really appreciate if anyone could help me understand why
#interface NOTESubview : UIView <UITextFieldDelegate>
#property (nonatomic, weak) id<UITextFieldDelegate> delegate;
-(UITextView *)jotNotes;
#end
#implementation NOTESubview
-(id)initWithFrame:(CGRect)arg1 {
self = [super initWithFrame:arg1];
if (self) {
self.delegate = self;
[self addSubview:[self jotNotes]];
}
return self;
}
-(UITextView *)jotNotes {
UITextView * jotNotes = [[UITextView alloc] initWithFrame:CGRectMake(0, self.frame.size.height/5.7, self.frame.size.width, self.frame.size.height - self.frame.size.height/5.7)];
UIToolbar* keyboardTextViewBar = [[UIToolbar alloc] init];
keyboardTextViewBar.barStyle = UIBarStyleDefault;
[keyboardTextViewBar sizeToFit];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStylePlain target:self
action:#selector(dismissKB:)];
[keyboardTextViewBar setItems:[NSArray arrayWithObjects:flexSpace, doneButton, nil]];
jotNotes.inputAccessoryView = keyboardTextViewBar;
jotNotes.delegate = self.delegate;
return jotNotes;
}
-(void)dismissKB:(UIBarButtonItem *)sender {
//this will log, so im not sure why it wont resign the board no matter what i try
NSLog(#"keyboard attempted to dismiss by %#", sender);
[[self jotNotes] resignFirstResponder];
}
I suspect that when the dismissKB method is called, its actually not the one who is currently the first responder.
However, there's a trick where you can just "dismiss the keyboard" from anywhere in your app. You might wanna give it a try:
[[[[UIApplication sharedApplication] delegate] window] endEditing:YES];
Add the following line where you are trying to dismiss the keyboard:
[self endEditing:YES];

'UIPopoverController' is deprecated: first deprecated in iOS 9.0 [duplicate]

This question already has answers here:
How can I fix the "UIPopoverController is deprecated" warning?
(4 answers)
Closed 6 years ago.
I have developed a project that shows error :
'UIPopoverController' is deprecated: first deprecated in iOS 9.0 - UIPopoverController is deprecated. Popovers are now implemented as UIViewController presentations. Use a modal presentation style of UIModalPresentationPopover and UIPopoverPresentationController.
My codings are:
ViewController.h:
#import <UIKit/UIKit.h>
#import <Photos/Photos.h>
#import <MobileCoreServices/MobileCoreServices.h>
#interface ViewController : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
- (IBAction)touch:(id)sender;
#end
#interface SecondView : UIViewController
<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
//video gallery
#property (strong,nonatomic) UIPopoverPresentationController *popOver;
#property (weak, nonatomic) IBOutlet UIView *studentView;
#property (strong, nonatomic) NSURL *videoURL;
#end
ViewController.m:
- (void)openGallery {
UIImagePickerController *Picker=[[UIImagePickerController alloc] init];
Picker.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
Picker.mediaTypes=[[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
Picker.delegate=self;
if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
UIPopoverController *popController=[[UIPopoverController alloc] initWithContentViewController:Picker];
[popController presentPopoverFromRect:CGRectMake(0, 600, 160, 300) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
self.popOver=popController;
}
else
{
[self presentViewController:Picker animated:YES completion:nil];
}
}
#pragma mark - UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
if (self.studentView) {
self.videoURL = info[UIImagePickerControllerMediaURL];
[picker dismissViewControllerAnimated:YES completion:NULL];
[[NSUserDefaults standardUserDefaults] setObject:[self.videoURL absoluteString] forKey:#"url1"];
}
}
I could not proper reference for UiModalPresentationPopover. Can someone help me to solve this error. Any help is much appreciated. Thank you.
use UIModalPresentationPopover
In a horizontally regular environment, a
presentation style where the content is displayed in a popover view.
The background content is dimmed and taps outside the popover cause
the popover to be dismissed. If you do not want taps to dismiss the
popover, you can assign one or more views to the passthroughViews
property of the associated UIPopoverPresentationController object,
which you can get from the popoverPresentationController property.
In a horizontally compact environment, this option behaves the same as
UIModalPresentationFullScreen.
Available in iOS 8.0 and later.
Reference UIModalPresentationStyle Reference
for example
ModalViewController *modal = [[ModalViewController alloc] init];
modal.modalPresentationStyle = UIModalPresentationPopover;
modal.transitioningDelegate = self;
modal.popoverPresentationController.sourceView = self.view;
modal.popoverPresentationController.sourceRect = CGRectZero;
modal.popoverPresentationController.delegate = self;
[self presentViewController:modal animated:YES completion:nil];
else use UIPopoverPresentationController
for example
UIPopoverPresentationController *popController = [self. popoverPresentationController];
popController.permittedArrowDirections = UIPopoverArrowDirectionAny;
popController.barButtonItem = self.leftButton;
popController.delegate = self;
additional reference

PopOverController parsing value or array in UIPopupViewController class

I have one calendar and some events in calendar.
When user click on event date then open popup, I know how to show popup, but problem is how to parsing table data in my popup class xib ?
Here is my code I get a null value.
Here is code for click on event then show popup.
Here I pass a array in popupController movies.listOfEvent=listOfEvent; movies.listofEvent this is declared in calendarpopupviewController.
NSMutableArray *listOfEvent=[[NSMutableArray alloc] init];
for (int i=0; i<getEventData.count; i++)
{
[listOfEvent addObject:[getEventData objectAtIndex:i]];
}
NSLog(#"%#",listOfEvent);
if (self.popoverController == nil)
{
CalendarPopupViewController *movies = [[CalendarPopupViewController alloc] initWithNibName:#"CalendarPopupViewController" bundle:[NSBundle mainBundle]];
UIPopoverController *popover = [[UIPopoverController alloc] initWithContentViewController:movies];
movies.getValue=#"nishant------------";
movies.listOfEvent=listOfEvent;
popover.delegate = self;
self.popoverController = popover;
}
CGRect popoverRect = getPoint;
popoverRect.size.width = MIN(popoverRect.size.width, 100);
[self.popoverController presentPopoverFromRect:popoverRect
inView:self.view
permittedArrowDirections:UIPopoverArrowDirectionAny
animated:YES];
Where do I mistake, how to pass value ? please help me
In my popupviewController code
#interface CalendarPopupViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
#property (nonatomic,strong) NSArray *listOfEvent;
#property (strong,nonatomic) NSString *getValue;
and .m file
#synthesize listOfEvent;
#synthesize getValue;
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"%#",getValue);
NSLog(#"%#",listOfEvent);
}

UIPopoverController for iPad "must not be called with `nil`" error

I am trying to make an iPhone app work in an iPad but the UIPopoverController is
comming back with error.
- (IBAction)photoTapped1 {
if(UI_USER_INTERFACE_IDIOM()== UIUserInterfaceIdiomPhone){
// If in editing state, then display an image picker; if not, create and push a photo view controller.
if (self.editing) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
[self presentViewController:imagePicker animated:YES completion:nil];
[imagePicker release];
} else {
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}
}else{
if (self.editing){
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
self.popover.delegate =self;
[popover release];
}else{
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
recipePhotoViewController.hidesBottomBarWhenPushed = YES;
recipePhotoViewController.recipe = recipe;
[self.navigationController pushViewController:recipePhotoViewController animated:YES];
[recipePhotoViewController release];
}}}
The error I am getting is:
'NSInvalidArgumentException', reason: '-[UIPopoverController initWithContentViewController:] must not be called with nil.'
Anyone available to give me a hand on this code, I have looked on the internet for solutions and samples but can not seem to make it work.
Thank you.
___ added to original question_____
I am adding the recipePhotoViewController here, I am assuming that ImageView manipulation is the same for iPhone and iPad.
my.h File
#class Recipe;
#interface RecipePhotoViewController : UIViewController {
#private
Recipe *recipe;
UIImageView *imageView;
}
#property(nonatomic, retain) Recipe *recipe;
#property(nonatomic, retain) UIImageView *imageView;
#end
Here is my .m file
#implementation RecipePhotoViewController
#synthesize recipe;
#synthesize imageView;
- (void)loadView {
self.title = #"Photo";
imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.backgroundColor = [UIColor blackColor];
self.view = imageView; }
- (void)viewWillAppear:(BOOL)animated {
imageView.image = [recipe.image valueForKey:#"image"];}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
[imageView release];
[recipe release];
[super dealloc];
} #end
You are initializing the popover controller in a wrong way:
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
You should pass the controller you would like to display inside of the popover -- not the popover itself (which is nil since you have not yet initialised it)!
Maybe this would do it for you?
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
self.popover = [[UIPopoverController alloc] initWithContentViewController:imagePicker];
If this is correct, then you should also present the pop over you have just created: – presentPopoverFromRect:inView:permittedArrowDirections:animated:
-- e.g.:
[self.popover presentPopoverFromRect:sender.frame inView:[self view] permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
where sender is the argument to :
- (IBAction)photoTapped1:(id)sender {
The content of your popover which you store in the variable popover is obviously nil. I can't see it ever being created in the code you provided.
Maybe you intended to present the Recipe photo controller as the content of the popover. In that case you would do something like
RecipePhotoViewController *recipePhotoViewController = [[RecipePhotoViewController alloc] init];
self.popover = [[UIPopoverController alloc] initWithContentViewController:recipePhotoViewController];
problem is in this line of your code ,
self.popover = [[UIPopoverController alloc] initWithContentViewController:popover];
try like this
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 400, 320, 260)];
popoverContent.view = popoverView;
//Add what ever you want popoverView
self.popover = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
self.popover.delegate =self;

Trying to access UINavigationController from within AppDelegate

Ok, I'm still pretty new to iOS development, so I apologize if this is a silly question.
But, I have an AlertView that I call from the AppDelegate then respond when clicking a button in the alert. I can do a NSLog and see that the methods are getting called. But, it's not pushing the view into the stack. Here's a sample of what I have (I'm sure it's wrong):
This is in the AppDelegate.m:
#import "AppDelegate.h"
#import "ProfileConnection.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize navController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
-(void)switchToController:(NSString *)controller animated:(BOOL)animated{
NSLog(#"switching to controller %#", controller);
// maybe we can do a check to see if a subview exists...and then push or pop accordingly.
// switch to the "TableView" view
if( [controller isEqualToString:#"ProfileConnection"]){
NSLog(#"switching to the ProfileConnection view");
ProfileConnection *profile = [[ProfileConnection alloc] initWithNibName:#"ProfileConnection" bundle:nil];
[self.navController pushViewController:profile animated:YES];
}
}
-(void)showConnectionFoundAlert
{
NSString *connectFoundMsg = [[NSString alloc] initWithFormat:#"We found someone we'd think you would like to meet: Tony Davis"];
UIAlertView *connectionFoundAlert = [[UIAlertView alloc] initWithTitle:#"Connection Found" message:connectFoundMsg delegate:self cancelButtonTitle:#"Decline" otherButtonTitles:#"Connect", #"View Profile", #"Save For Later", nil];
[connectionFoundAlert show];
//[connectionFoundAlert release];
}
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
NSString *alertString = [[NSString alloc] initWithFormat:#""];
if([title isEqualToString:#"Decline"])
{
alertString = #"Declied";
}
else if([title isEqualToString:#"Connect"])
{
alertString = #"Connected";
}
else if([title isEqualToString:#"View Profile"])
{
//alertString = #"Profile Viewed";
//NSLog(#"View Profile is being called");
[self switchToController:#"ProfileConnection" animated:YES];
//UIViewController *profile = [[UIViewController alloc] initWithNibName:#"ProfileConnection" bundle:nil];
//ProfileConnection *profile = [[ProfileConnection alloc] initWithNibName:#"ProfileConnection" bundle:[NSBundle mainBundle]];
//UINavigationController *nav = [[UINavigationController alloc] init];
//[nav pushViewController:profile animated:NO];
/*UIViewController *profile = [[UIViewController alloc] initWithNibName:#"ProfileConnection" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] init];
[navigation pushViewController:profile animated:YES];*/
/*
ProfileConnection *profile = [ProfileConnection alloc];
//UIView *current = self.window;
[self.window addSubview:profile.view];
*/
/*
[window addSubview:view1.view];
[window makeKeyAndVisible];
- (void)goToNextPage {
view2 = [ViewController2 alloc];
UIView *current = self.window;
[self.window addSubview:view2.view];
*/
}
else if ([title isEqualToString:#"Save For Later"])
{
alertString = #"Saved It";
}
UIAlertView *alertStr = [[UIAlertView alloc] initWithTitle:#"" message:alertString delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
if ([alertString isEqualToString:#""]) {
} else {
[alertStr show];
}
}
#end
This is the AppDelegate.h:
#import <UIKit/UIKit.h>
#import "ProfileConnection.h"
#interface AppDelegate : UIResponder <UIAlertViewDelegate, UIApplicationDelegate, UINavigationControllerDelegate> {
UINavigationController *navController;
}
#property (strong, nonatomic) UIWindow *window;
#property (nonatomic, retain) UINavigationController *navController;
-(void)showConnectionFoundAlert;
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex;
-(void)switchToController:(NSString *)controller animated:(BOOL)animated;
#end
I am able to add the view with this, but I lose my navigation controller:
ProfileConnection *profile = [ProfileConnection alloc];
[self.window addSubview:profile.view];
You can see I have tried a few approaches, but I'm confusing myself trying to use the storyboard approach.
Also, the ProfileConnection view is blank with a single label at the moment, if that helps.
You code looks ok [self.navController pushViewController:profile animated:YES]; is how you should do it.
You should make sure that you have added the navigation controller to the window. Perhaps this should already be done by the storyboard, but if not user the rootviewcontroller property of the window (its better than addSubview).
self.window.rootViewContorller = self.navController;
Now do a sanity check to make sure nothing is nil (profile or the navController).
NSLog(#"%# %#",self.navController, profile);
Does any of that help?

Resources