How to recognize a button press for programmatic UIButton Xcode - objective-c

Im using this code to create UIBarButtons programmatically on my Navigation Bar, how can I set the Buttons to send IBActions programmatically. I.O.W. how can I programmatically place a view to recognize the UITapGestureRecognizer(set the touchUpInside event) and connect it to an IBAction?
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:153/256.0 green:204/256.0 blue:51/256.0 alpha:1.0];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.title = #"Feed";
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
UIBarButtonItem *postItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:nil];
UIBarButtonItem *optionsItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"myIcon2.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
NSArray *cameraAndPostItem = #[cameraItem, postItem];
NSArray *addPlusAndOptionsItem = #[optionsItem, addPlusItem];
self.navigationItem.leftBarButtonItems = cameraAndPostItem;
self.navigationItem.rightBarButtonItems = addPlusAndOptionsItem;
EDIT: I am trying to perform a segue when the user presses the UIBarButtonItem, which I have implemented programmatically.

set the target and the action of the buttons
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
cameraItem.target = self;
cameraItem.action = #selector(cameraItemButtonInvoked);
//or short
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(cameraItemButtonInvoked)];
...
- (IBAction)cameraItemButtonInvoked {
.... // e.g.
[self performSegueWithIdentifier:#"ShowDetail" sender:sender];
}

You'll be setting the action to UIBarButton like this:
UIBarButtonItem *addPlusItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(addSomething:)];
Note the action part, there you are passing the selector to be called. Write that selector in your ViewController like this:
- (void) addSomething:(id)sender{
NSLog(#"I'm adding something..");
}
Do the same for rest of the buttons.

Related

barButtonItem in UIToolBar of NavigationController not working in ios7

I am trying to add BarButton in UIToolBar of NavigationController. I found on below code on stack over flow. But it is not working in ios7. If anything changed why it shows nothing on tool bar.
[self.navigationController setToolbarHidden:NO];
UIBarButtonItem *buttonItem = [[ UIBarButtonItem alloc ] initWithTitle: #"Select All"
style: UIBarButtonItemStyleBordered
target: self
action: #selector(selectAll:) ];
UIBarButtonItem *buttonNext = [[UIBarButtonItem alloc]initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(goNext:)];
self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];
//allocate the tool bar
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 416, 320, 44)];
// create bar btns
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind target:self action:#selector(Back)];
//set spacing
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(Forward)];
// add btns to the bar
[toolBar setItems:[NSMutableArray arrayWithObjects:back,flexibleSpace,forward, nil]];
// adds the toobar to the view
[self.view addSubview:toolBar];
I found the problem. Above code is correct. The reason why there is no button showing in the tool bar . i also have this function in my class. this is a empty function. so it overwrite the default self.toolbarItems = [ NSArray arrayWithObjects: buttonItem, buttonNext, nil ];
After Removing the below code. Its working Perfect Now.
- (void)setToolbarItems:(NSArray *)toolbarItems animated:(BOOL)animated
{
}

Set navigation bar title to UIBarButtonItem?

here's some code that I am using to add UIBarButtonItems to my navigation bar, how can I change it display custom images, or images uploaded to my Xcode project?
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:nil];
NSArray *actionButtonItems = #[shareItem, cameraItem];
self.navigationItem.rightBarButtonItems = actionButtonItems;
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *cameraItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"your_image.png"] style:UIBarButtonItemStyleBordered target:self action:nil];
self.navigationItem.rightBarButtonItems = [[NSArray alloc] initWithObjects: shareItem, cameraItem, nil];
Also you can set not only your image, but also your custom UIView with this method:
- (id)initWithCustomView:(UIView *)customView
See more info.
UIBarButtonItemStyleBordered is deprecated in iOS 8. Use UIBarButtonItemStylePlain instead.
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"ic_clear"] style:UIBarButtonItemStylePlain target:self action:nil];
NSArray *actionButtonItems = #[shareItem];
self.navigationItem.leftBarButtonItems = actionButtonItems;

Odd bug with UISplitView - Viewport scales to iPhone size?

I'm working with Objective-C for the first time, and have an odd bug in an application that was given to me. The following pictures demonstrate the problem I'm having:
This screen displays when the user rotates the iPad. This is normal. The control at the top (<) allows the user to hide the subview on the left side. Upon hiding the subview, the dark blue portion should expand to fill the screen. Prior to my working with the application, this worked. Now, this is happening:
I haven't found anything to explain why, exactly, this is happening. Any ideas?
- (void)viewDidLoad {
[super viewDidLoad];
[self addGestureRecognizers];
// initialize the "<" button...
toggleButton = [[UIBarButtonItem alloc] initWithTitle:[PanoUtils leftIndicator] style:UIBarButtonItemStylePlain target:self action:#selector(toggleScreen)];
// the annotate button...
annotateButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Pencil.png"] style:UIBarButtonItemStylePlain target:self action:#selector(annotateTapped)];
// the done button for annotations...
doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneAnnotateTapped)];
// a flexible spacer...
nameFieldSpacerButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// the accelerometer button...
alignButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gyro.png"] style:UIBarButtonItemStylePlain target:self action:#selector(align:)];
// and the search button.
searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Search.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTouched:)];
// customize the align button after the user selects it.
UIImage *buttonImage = [UIImage imageNamed:#"gyroSelected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(align:) forControlEvents:UIControlEventTouchUpInside];
alignSelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *itemsArray = [toolBar.items mutableCopy];
[itemsArray addObject:searchButton];
[itemsArray addObject:annotateButton];
[itemsArray addObject:alignButton];
[toolBar setItems:itemsArray animated:NO];
[itemsArray release];
[self adjustToggleButton];
}

How can I add multiple UIBarButtonItem's to a UINavigationBar?

I want to add many UIBarButtonItem's to a UINavigationbar, not just right and left buttons:
logoButton = [[UIBarButtonItem alloc] initWithTitle:#"A Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonAClicked:)];
logoButton2 = [[UIBarButtonItem alloc] initWithTitle:#"B Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonBClicked:)];
logoButto3 = [[UIBarButtonItem alloc] initWithTitle:#"C Button" style:UIBarButtonItemStyleBordered target:self action:#selector(logoButtonCClicked:)];
self.navigationController.navigationBarHidden = NO;
self.title = #"Title";
NSArray* items = [[NSArray alloc] initWithObjects:logoButtonA, logoButtonB, logoButtonC, nil];
self.navigationController.navigationBar.items = items;
I get a SIGBRT on self.navigationController.navigationBar.items = items;
How can I add multiple UIBarButtonItems to a UINavigationBar?
You need to add UIBarButtonItem instance to a UINavigationItem, not to a UINavigationBar. So you can do this as:
NSArray *buttonArray = [NSArray arrayWithObjects:logoButton, logoButton2, logoButton3, nil];
self.navigationItem.leftBarButtonItems = buttonArray;
If you want your buttons on the right, use rightBarButtonItems.
You should use
self.navigationItem.leftBarButtonItems = items;
use addSubView in navigationbar.

iPad app navigation bar with multiple buttons on the same side

how can I have a back button and another button both on the left side of the nav bar? since this is an ipad app there's space for multiple buttons. but I can only get it to show one button per side on the navigationitem...
For people who stumbled here and were disappointed by the broken link, check out this article:
http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
iOS now supports multiple buttons on the same side. From the UINavigationItem docs:
Customizing Views
titleView property
leftBarButtonItems property
leftBarButtonItem property
rightBarButtonItems property
rightBarButtonItem property
– setLeftBarButtonItems:animated:
– setLeftBarButtonItem:animated:
– setRightBarButtonItems:animated:
– setRightBarButtonItem:animated:
So, basically, you would write something like this to add two buttons on the left:
UIBarButtonItem *refreshBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshPlans:)];
UIBarButtonItem *selectYearBtn = [[UIBarButtonItem alloc] initWithTitle:#"Select Year" style:UIBarButtonSystemItemAction target:self action:#selector(selectYear)];
self.navigationItem.leftBarButtonItems = [[NSArray alloc] initWithObjects: refreshBtn, selectYearBtn, nil];
Unfortunately, I haven't seen a way to do this in the Storyboard. Hope this helps.
UIBarButtonItem *btn0 = [[UIBarButtonItem alloc] initWithTitle:#"1" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel0)];
UIBarButtonItem *btn1 =[[UIBarButtonItem alloc] initWithTitle:#"<2" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel1:)];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithTitle:#"<3" style:UIBarButtonItemStylePlain target:self action:#selector(tapLevel2:)];
UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space.width = 10;
UIBarButtonItem *space2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
space2.width = 10;
// UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshItem)];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:btn0, space, btn1, space2, btn2, nil];
found the answer: http://blog.blackwhale.at/2009/06/uibuttons-in-uinavigationbar/