I have a memory leak that I do not understand. I am switching views with a segmentcontrol like this:
- (void)didChangeSegmentControl:(UISegmentedControl *)control {
if (self.activeViewController) {
[self.activeViewController viewWillDisappear:NO];
[self.activeViewController.view removeFromSuperview];
[self.activeViewController viewDidDisappear:NO];
}
self.activeViewController = [self.segmentedViewControllers objectAtIndex:control.selectedSegmentIndex];
[self.activeViewController viewWillAppear:NO];
[self.containerView addSubview:self.activeViewController.view];//Here is the memory leak
[self.activeViewController viewDidAppear:NO];
[self BuildBottomBarButtons];
}
I leak appears in Instruments and I do not have any idea why...
Thanks!!!
Edit:
The "BuildBottomBarButtons":
-(void) BuildBottomBarButtons{
//create toolbar using new
UIToolbar *toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleBlackTranslucent;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 330, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareClicked)];
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"scroll left.PNG"] style:UIBarButtonItemStylePlain target:self action:#selector(upClicked)];
UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"scroll right.PNG"] style:UIBarButtonItemStylePlain target:self action:#selector(downClicked)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *fixItem50 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *fixItem70 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
fixItem50.width = 50;
fixItem70.width = 64;
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, fixItem70, systemItem2, fixItem50,systemItem3, nil];
//release buttons
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[fixItem70 release];
[fixItem50 release];
//add array of buttons to toolbar
[toolbar setItems:items animated:NO];
[self.containerView addSubview:toolbar];
[toolbar release];
}
And the "self.segmentedViewControllers" init method is:
- (NSMutableArray *)segmentedViewControllerContent {
JobGeneralDetailsController * controller1 = [[JobGeneralDetailsController alloc] initWithSelectedRowID:selectedRowID andWithJobBoardID:jobBoardId andWithJobDetails:jobDetails];
[controller1 setViewType:jobDetailsViewType];
//initWithParentViewController:self];
JobMapDetailsController * controller2 = [[JobMapDetailsController alloc] initWithJobDetails:jobDetails];//[[AustraliaViewController alloc] initWithParentViewController:self];
[controller1 setJobMapDetailsController: controller2];
JobReviewsController *controller3 = [[JobReviewsController alloc] initWithStyle:UITableViewStyleGrouped];
[controller3 setJobDetails:jobDetails];
[controller3 setViewType:jobDetailsViewType];
[controller1 setJobReviewsController: controller3];
NSMutableArray * controllers = [NSMutableArray arrayWithObjects:controller1, controller2, controller3,nil];
[controller1 release];
[controller2 release];
[controller3 release];
return controllers;
}
The only line I could point to is [self BuildBottomBarButtons];
Looking at your code the only problem I can see is in the [self BuildBottomBarButtons]
It looks like you are adding the toolbar to the container view everytime you switch view controllers.
What type of object does Instruments say is leaking, is it possible to see a screen shot of your instruments screen showing the leaked objects?
Related
I have a UIButton that when clicked, displays a UIPopover with a UIDatePicker in it.
It is supposed to display under button for date of birth, but it is covering it instead.
This is the code I have:
- (IBAction)dateOfBirthButtonPressed:(id)sender{
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIDatePicker *datePickerTemp = [[UIDatePicker alloc]init];
datePickerTemp.frame=CGRectMake(0,44,320, 216);
datePickerTemp.datePickerMode = UIDatePickerModeDate;
datePickerTemp.maximumDate = [NSDate date];
self.datePicker = datePickerTemp;
[popoverView addSubview:self.datePicker];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame=CGRectMake(0,0 ,320, 40);
toolbar.barStyle = UIBarStyleBlackOpaque;
NSMutableArray *toolbarItems = [NSMutableArray array];
UIBarButtonItem *cancelButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(datePickerCancelButtonClicked)];
UIBarButtonItem *doneButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(datePickerSaveButtonClicked)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolbarItems addObject:cancelButton1];
[toolbarItems addObject:space];
[toolbarItems addObject:doneButton1];
toolbar.items = toolbarItems;
[popoverView addSubview:toolbar];
[cancelButton1 release];
[space release];
[toolbar release];
popoverContent.view = popoverView;
UIPopoverController *popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
popoverController.delegate=self;
self.datePopoverController = popoverController;
[popoverContent release];
[popoverView release];
[self.datePopoverController setPopoverContentSize:CGSizeMake(320, 264) animated:NO];
[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
NSLog for frame:
DOB button: {{160, 129}, {329, 37}}
Last name label: {{160, 77}, {329, 31}}
Is your button a subview of your view, or buried within another subview? If it is not a direct subview of your view then your coordinate space is off. Try changing your code to:
CGRect buttonRect = [self.dateOfBirthButton convertRect:self.dateOfBirthButton.frame toView:self.view];
[self.datePopoverController presentPopoverFromRect:buttonRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
You can try either one of these to see that fix the problem. The second one fix my problem that is similar to yours.
[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.dateOfBirthButton permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
or
[self.datePopoverController presentPopoverFromRect:self.dateOfBirthButton.frame inView:self.dateOfBirthButton.superview permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
I try to present a modal view (InfoViewController) from a navigation view controller(DateViewController).
I add a toolbar on top of InfoViewContoller's view. Now I want to add a title "Info" and a "Done" button on the toolbar.(The Done button will perform the infoDismissAction method)
Can anyone give me som tips? Thanks a lot!
Here's code of DateViewController.h
#import <UIKit/UIKit.h>
#import "InfoViewController.h"
#interface DateViewController : UIViewController
{
InfoViewController *infoViewController;
}
#property (nonatomic, retain) InfoViewController *infoViewController;
#end
DateViewController.m
- (IBAction)modalViewAction:(id)sender{
if (self.infoViewController == nil)
self.infoViewController = [[[InfoViewController alloc] initWithNibName:
NSStringFromClass([InfoViewController class]) bundle:nil] autorelease];
[self presentModalViewController:self.infoViewController animated:YES];
}
- (void)dealloc{
if (self.infoViewController != nil)
{
[infoViewController release];
}
[super dealloc];
}
- (void)viewDidLoad{
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonSystemItemPlay target:self action:#selector(modalViewAction:)] autorelease];
[modalBarButtonItem release];
[super viewDidLoad];
}
Here's InfoViewController.m
- (IBAction)infoDismissAction:(id)sender{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
[backButton release];
[super viewDidLoad];
}
Try this code.
- (void)viewDidLoad {
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
UIBarButtonItem *flexibleSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc] initWithTitle:#"INFO" style:UIBarButtonItemStyleBordered target:self action:#selector(InfoAction:)] autorelease];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"DONE" style:UIBarButtonItemStyleBordered target:self action:#selector(doneAction:)];
NSArray *barButton = [[NSArray alloc] initWithObjects:flexibleSpace,infoButton,flexibleSpace,doneButton,nil];
[toolBar setItems:barButton];
[self.view addSubview:toolBar];
[toolBar release];
[barButton release];
barButton = nil;
[super viewDidLoad];
}
I'd recommend setting up another UINavigationController with your InfoViewController and present the navigation controller as your modal view.
To answer your question you'd want to fill in your UIToolbar like this:
- (void)viewDidLoad {
[super viewDidLoad];
UIToolbar *toolBar;
toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 50)];
toolBar.frame = CGRectMake(0, 0, 320, 50);
toolBar.barStyle = UIBarStyleDefault;
[toolBar sizeToFit];
[self.view addSubview:toolBar];
[toolBar release];
UIBarButtonItem* bbiInfo = [[UIBarButtonItem alloc] initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(tappedInfoButton)];
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* bbiDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(tappedDoneButton)];
NSArray* items = [[NSArray alloc] initWithObjects:bbiInfo, flexibleSpace, bbiDone, nil];
[toolBar setItems:items];
[items release];
[bbiInfo release];
[flexibleSpace release];
[bbiDone release];
}
How to add button to UIPopoverController ?
I am attaching a picture for able to understand very clear.
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView *popoverView = [[UIView alloc] init];
popoverView.backgroundColor = [UIColor blackColor];
UIToolbar *toolbar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0,400 ,44)];
toolbar.barStyle =UIBarStyleBlackTranslucent;
NSMutableArray *ButtonArray=[[NSMutableArray alloc ]init];
Save=[[UIBarButtonItem alloc ]initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(save_pressed)];
UIBarButtonItem *space=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *cancel=[[UIBarButtonItem alloc ]initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel_pressed)];
[ButtonArray addObject:cancel];
[cancel release];
[ButtonArray addObject:space];
[space release];
[ButtonArray addObject:Save];
[Save release];
[toolbar setItems:ButtonArray];
[ButtonArray release];
[popoverView addSubview:toolbar];
[toolbar release];
picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 400, 340);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[popoverView addSubview:picker];
popoverContent.view = popoverView;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
[popoverController setPopoverContentSize:CGSizeMake(400, 264) animated:NO];
[popoverController presentPopoverFromRect:btn.frame inView:scr_AddLocation permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
[popoverView release];
[popoverContent release];
here I m adding Picker To Popover When We Pressed Button .
The cancel button (if that is what you are referring to) is the rightBarButtonItem of the UINavigationController's navigation bar. You can set it to be a standard UIBarButtonItem (as is done here) or a custom one (whose view you can create).
This question has been asked many times, but I tried every solution and none of them solved my problem.
Here's the toolbar creation code :
- (id)initWithBlogArticle:(BlogArticle *)theArticle
{
if (self = [super init])
{
CustomToolbar* tools = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 48.01)];
[tools setTintColor:[UIColor colorWithRed:0.62 green:0.70 blue:0.13 alpha:1.0]];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* previousArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
target:self
action:#selector(displayPreviousArticle)];
[previousArticle setStyle:UIBarButtonItemStyleBordered];
[buttons addObject:previousArticle];
[previousArticle release];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem *nextArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward
target:self
action:#selector(displayNextArticle)];
[nextArticle setStyle:UIBarButtonItemStyleBordered];
[buttons addObject:nextArticle];
[nextArticle release];
[tools setItems:buttons animated:NO];
[buttons release];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease]];
[tools release];
[self setWebView:[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
[[self webView] setDelegate:self];
[self loadBlogArticle:theArticle];
[self setView:[self webView]];
}
return self;
}
I've tried to use setEnabled:NO in every possible way and it never works, which is driving me crazy as disabling a button should be so simple...so either this is extremely complicated to do or I'm not understanding something very basic.
Please help, thanks in advance.
Solution:
self.navigationItem.rightBarButtonItem.enabled = NO/YES;
there is also:
self.navigationItem.leftBarButtonItem.enabled = NO/YES;
works in iOS 5.
Declare previousArticle and nextArticle in header file
- (id)initWithBlogArticle:(BlogArticle *)theArticle
{
if (self = [super init])
{
CustomToolbar* tools = [[CustomToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 48.01)];
[tools setTintColor:[UIColor colorWithRed:0.62 green:0.70 blue:0.13 alpha:1.0]];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
previousArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
target:self
action:#selector(displayPreviousArticle)];
[previousArticle setStyle:UIBarButtonItemStyleBordered];
[buttons addObject:previousArticle];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
nextArticle = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward
target:self
action:#selector(displayNextArticle)];
[nextArticle setStyle:UIBarButtonItemStyleBordered];
[buttons addObject:nextArticle];
[tools setItems:buttons animated:NO];
[buttons release];
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:tools] autorelease]];
[tools release];
[self setWebView:[[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
[[self webView] setDelegate:self];
[self loadBlogArticle:theArticle];
[self setView:[self webView]];
}
return self;
}
-(void)displayNextArticle
{
if(articleEnd)
nextArticle.enabled=NO;
else
nextArticle.enabled=YES;
}
-(void)dealloc
{
[previousArticle release];
[nextArticle release];
}
Right now I'm using this in my -viewDidLoad method:
UIToolbar *toolbar = [[UIToolbar alloc] init];
UIBarButtonItem *flexibleSpace = [UIBarButtonItem alloc];
flexibleSpace = [flexibleSpace initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
// Add a back button to allow user to close the modal view
NSString *back = NSLocalizedString(#"Back", nil);
UIBarButtonItem *backButton = [UIBarButtonItem alloc];
backButton = [backButton initWithTitle:back
style:UIBarButtonItemStyleDone
target:self
action:#selector(dismissModalViewControllerAnimated:)];
// Add a centered title to the toolbar
// I doubt this is the "correct" way to do this, but it seems to work.
// The "width" property of a UIBarButtonItem doesn't seem to correspond to
// the actual width if the button is flexible (i.e. the width isn't explicitly
// set), so I'm using this hack instead.
// This is obviously NOT an optimal solution. For one thing, if the button padding
// ever changes, it has to be changed manually here as well. For another, it is
// a pain to do this for every button I add to the toolbar, and furthermore the title
// is centered only according to its own width, not the toolbar's.
const CGRect toolbarFrame = [toolbar frame];
const CGFloat backWidth = [back sizeWithFont:[UIFont boldSystemFontOfSize:[UIFont buttonFontSize]]
constrainedToSize:toolbarFrame.size].width;
const CGRect titleFrame = {{0.0f, 0.0f},
{toolbarFrame.size.width - (backWidth * 2.0f), 50.0f}};
UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleFrame];
[titleLabel setText:[self title]];
[titleLabel setBackgroundColor:[UIColor clearColor]];
[titleLabel setTextAlignment:UITextAlignmentCenter];
[titleLabel setFont:[UIFont boldSystemFontOfSize:20.0f]];
[titleLabel setTextColor:[UIColor whiteColor]];
[titleLabel setShadowColor:[UIColor colorWithWhite:0.0f alpha:0.5f]];
[titleLabel setShadowOffset:CGSizeMake(0.0f, -1.0f)];
UIBarButtonItem *titleItem = [[UIBarButtonItem alloc] initWithCustomView:titleLabel];
[titleLabel release];
NSArray *items = [[NSArray alloc] initWithObjects:flexibleSpace, titleItem, backButton, nil];
[flexibleSpace release];
[titleItem release];
[backButton release];
[toolbar setItems:items];
[items release];
[view addSubview:toolbar];
[toolbar release];
Does anyone have a better method for doing this? What I'm using feels like a major hack :(.
Edit:
Thanks for the suggestion Darren!
Here's what I'm using now, if anyone's interested:
First, in accordance with Darren's suggestion, I'm wrapping my modal view controller in a generic UINavigationController (which contains it's own UIToolbar, UINavigationBar, that comes with a title):
MyCustomViewController *myModalViewController = [[MyModalViewController alloc] init];
[myModalViewController setTitle:#"Foo"];
UINavigationController *modalNavController = [[UINavigationController alloc] initWithRootView:myModalViewController];
[myModalViewController release];
// This is intended to be presented in another view controller class
[self presentModalViewController:modalNavController animated:YES];
[modalNavController release];
Then in my -init method for the MyModalViewController class, I have this:
- (id)init
{
if (self = [super init]) {
UIBarButtonItem *backButtonItem = [UIBarButtonItem alloc];
backButtonItem = [backButtonItem initWithTitle:back
style:UIBarButtonItemStyleDone
target:[self navigationController]
action:#selector(dismissModalViewControllerAnimated:)];
[[self navigationItem] setRightBarButtonItem:backButtonItem];
[backButtonItem release];
}
return self;
}
This is a much cleaner solution than before. Thanks.
You should wrap your view controller inside a generic UINavigationController when you present the modal view:
MyCustomController* myController = [[MyCustomController alloc] init];
editor.title = #"My Title";
UINavigationController* modalController = [[UINavigationController alloc] initWithRootViewController:myController];
[self.navigationController presentModalViewController:modalController animated:YES];
[modalController release];
[myController release];
Your custom controller can specify its toolbar buttons in its init method:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(doCancel:)] autorelease];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(doSave:)] autorelease];
}
return self;
}
You should be adding the title in the items property
i.e.
#property(nonatomic, copy) NSArray *items
where items is init with a title
initWithTitle:style:target:action:
See http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIToolbar_Class/Reference/Reference.html#//apple_ref/occ/instp/UIToolbar/items
and
http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html#//apple_ref/occ/instm/UIBarButtonItem/initWithTitle:style:target:action:
for the details.
Hope that helps
[Edit]
P.S. The UIBarButtonItem is also where you would be adding your buttons ;-)