UIBarButtonItem action not working.Why? - iphone

Thanks to all. I found really working code. It looks like:
- (void)viewDidLoad {
[super viewDidLoad];
UIButton* infoButton = [UIButton buttonWithType: UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(settingsClick) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem* theSettingsButton =[[UIBarButtonItem alloc]initWithCustomView:infoButton];
[self.toolbar setItems:[NSArray arrayWithObjects:theSettingsButton,nil]];
[theSettingsButton release];
}

This works:
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)] autorelease];
}
-(void)settingsClick
{
NSLog(#"Hello");
}
You hadn't closed your viewDidLoad method.

UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSMutableArray * arr = [NSMutableArray arrayWithObjects:theSettingsButton, nil];
[self.toolbar setToolbarItems:arr animated:YES];
try this

toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 380, 320, 40)];
UIBarButtonItem *theSettingsButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered
target:self action:#selector(settingsClick)]autorelease];
NSArray * arr = [NSArray arrayWithObjects:theSettingsButton, nil];
[toolbar setItems:arr animated:YES];
[self.view addSubview:toolbar];
[toolbar release];

Related

How to add bar buttons in a UIToolBar

I have created a UIToolBar and want to add three items in that like contact, date and message. I tried but i am not able to do that.Kindly help as i am new to Objective C. Here is my "ViewController.m"
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 414, self.view.frame.size.width, 44);
UIBarButtonItem *contact = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:nil];
UIBarButtonItem *message = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
NSMutableArray *items = [[NSMutableArray alloc] initWithObjects:contact,message, nil];
[toolbar setItems:items animated:NO];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
In Following code i added two UIBarButton with flexSpace..
you can add UIBarButton as you want
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
Toolbar.barStyle = UIBarStyleBlackTranslucent;
[Toolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(Cancel)];
[barItems addObject:btnCancel];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(done)];
[barItems addObject:btnDone];
[Toolbar setItems:barItems animated:YES];
Following Method is call when tapped on bar Button
-(void)Cancel
{
// Write Code for Cancel Method
}
-(void)done
{
// Write Code for Done Method
}
try this...
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44)];
myToolbar.barStyle = UIBarStyleBlackOpaque;
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *contactBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(contact:)];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *dateBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(date:)];
UIBarButtonItem *flexibleSpaceRight = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *msgBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(message:)];
[myToolbar setItems:[NSArray arrayWithObjects: flexibleSpaceLeft, doneBtn, flexibleSpace, dateBtn, flexibleSpaceRight, msgBtn, nil]];

Objective c memory leak when using uisegmentcontrol to switch views

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?

How to add title and Done button on toolbar in a modal view

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];
}

buttons on navigation bar not loading

i want to add buttons on navigation bar but its not loading on bar.Please help.Following is my code.please tell me where i am going wrong.The view i am using is landscape
- (void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0);
navBar = [[UINavigationBar alloc] initWithFrame:rect];
navBar.items = self.navigationController.navigationBar.items;
navBar.delegate = self;
//[navBar release];
navBar.tintColor =[UIColor blackColor]
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:#"Photo" style:UIBarButtonItemStyleBordered target:self action:#selector(takePhoto)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:#"Camera List" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel:)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.leftBarButtonItem = backButton;
//navBar.topItem.rightBarButtonItem = rightButton;
// navBar.topItem.leftBarButtonItem = backButton;
[self.view addSubview:navBar];
[backButton release];
}
try this
(void)viewDidLoad {
[super viewDidLoad];
CGRect rect = CGRectMake(0.0, 0.0, 480.0, 32.0);
navBar = [[UINavigationBar alloc] initWithFrame:rect];
navBar.items = self.navigationController.navigationBar.items;
navBar.delegate = self;
UINavigationItem *navigationItem = [UINavigationItem alloc];
[navBar pushNavigationItem:navigationItem animated:NO];
[navigationItem release];
UIBarButtonItem * rightButton = [[UIBarButtonItem alloc]initWithTitle:#"Photo" style:UIBarButtonItemStyleBordered target:self action:#selector(takePhoto)];
UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:#"Camera List" style:UIBarButtonItemStyleBordered target:self action:#selector(cancel:)];
self.navigationItem.rightBarButtonItem = rightButton;
self.navigationItem.leftBarButtonItem = backButton;
[self.view addSubview:navBar];
[backButton release];
[rightButton release];
}

iphone how to disable UIBarButtonItem

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];
}