UIDatePicker memory leak in IOS 8.3

2019-02-18 12:13发布

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

0条回答
登录 后发表回答