UIBarButtonItem target/action not working - objective-c

I am implementing a view with a date picker and a toolbar.
I have the following code:
-(id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame), 44)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(didSelectCancelButton)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(didSelectDoneButton)];
NSArray *buttons = #[cancelButton, flexible, doneButton];
[self.toolbar setItems:buttons
animated:NO];
self.datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 50, CGRectGetWidth(frame), 216)];
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];
}
return self;
}
- (IBAction)didSelectCancelButton {
if ([self.delegate respondsToSelector:#selector(datePickerDidCancelDateSelection:)]) {
[self.delegate datePickerDidCancelDateSelection:self];
}
}
- (IBAction)didSelectDoneButton {
NSLog(#"");
}
But when I click the buttons, no action is performed. The methods are not invoked.
Can you tell what am I doing wrong?
Thanks
EDIT:
Turned out there was a gesture recognizer that was capturing the touch events.
Fixing that resolved this problem.

I suspect your date picker (which is as wide as the toolbar is) is somehow inhibiting your buttons from getting user touches.
Try reversing these two lines to this:
[self addSubview:self.datePicker];
[self addSubview:self.toolbar];

Do you have any UIGestureRecognizer objects, if you do try to disable them and test.

Related

Resizing UIToolbar when UISearchBar becomes active

I have an UIToolbarwith 5 items:
UIBarButtonItem with image
UIBarButtonItem flex width
UIBarButtonItem with custom view (UISearchBar)
UIBarButtonItem flex width
UIBarButtonItem with image
When I select the UISearchBar, I get it to keep its correct size and become active. However, I would like the image to the left and to the right to disappear and give the UISearchBar the full width of the UIToolbar. How do I do that?
This is what I have:
https://dl.dropbox.com/u/3077127/demo_1.mov
And this is what I want:
https://dl.dropbox.com/u/3077127/demo_2.mov
This is my code:
#pragma mark View lifecycle
- (void)viewDidLoad {
[...]
SearchBar *mySearchBar = [[SearchBar alloc] init];
mySearchBar.delegate = self;
[mySearchBar sizeToFit];
[mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[mySearchBar setTintColor:[UIHelpers getSlColor]];
CGRect searchBarFrame = mySearchBar.frame;
searchBarFrame.size.width = 218;
[mySearchBar setFrame:searchBarFrame];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:mySearchBar.frame];
[searchBarContainer addSubview:mySearchBar];
[searchBarContainer setTag:99];
UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"location-arrow"] style:UIBarButtonItemStyleBordered target:self action:nil];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
UIBarButtonItem *search = [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
NSArray *items = [[NSArray alloc] initWithObjects:left, flex, search, flex, right, nil];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[toolbar setItems:items];
[toolbar setTintColor:[UIHelpers getSlColor]];
self.tableView.tableHeaderView = toolbar;
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectNull];
[...]
[super viewDidLoad];
}
#pragma mark -
Here is my answer to a similar question posted here
The key to a correct animation is to specify UIViewAnimationOptionLayoutSubviews as an option for the animation.
Not sure if you have tried this, but maybe you can use the UISearchBarDelegate methods to get notified when the user clicks on the search bar.
Example:
#pragma mark - UISearchBarDelegate Methods
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
// Remove the right and left buttons from the ToolBar
[self updateToolBarWithImages:NO];
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
// Add back the right and left buttons to the ToolBar
[self updateToolBarWithImages:YES];
}
#pragma mark - Private Methods
// Custom method used to Update the ToolBar
- (void)updateToolBarWithImages:(BOOL)iShowImages {
// Logic to update ToolBar based on the BOOL value in showImages
// use "setItems:animated:" method of UIToolBar to set the ToolBar Items
[toolBar setItem:items animated:YES];
}
You shouldn't use a ToolBar for that.
Simply use a NavigationBar, with your 2 right and left ButtonItems, plus add to the NavigationBar's titleView a SearchBar with the appropriate delegates.
So you don't have to resize your NavigationBar. It's just about adding/removing the NavigationBar's right and left ButtonItems animated like so:
if (self.searchBar.isActive) {
[self.navigationItem setLeftBarButtonItem:nil animated:YES];
[self.navigationItem setRightBarButtonItem:nil animated:YES];
}
else {
[self.navigationItem setLeftBarButtonItem:self.leftButton animated:YES];
[self.navigationItem setRightBarButtonItem:self.rightButton animated:YES];
}
Hope this helps!

hidden navigation bar causing trouble with action sheet

The navigation bar in my uinavigationbarcontroller is hidden, but my actionsheet thinks its still there, hence giving me a the highlights at the top of the action page (seen above). How can I erase this/ trick my actionsheet into thinking its a normal uiimageview?
Navigation bar hidden like so in didLoad:
[self.navigationController setNavigationBarHidden:YES animated:NO];
and:
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if (![self.navigationController isNavigationBarHidden])
{
[self.navigationController setNavigationBarHidden:YES animated:animated];
}
}
actionsheet looks like this in didLoad:
actionSheet = [[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 200,320,16)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
UIPickerView *locationPicker = [[UIPickerView alloc] init];
locationPicker.frame = CGRectMake(0, 244, 320, 216);
locationPicker.delegate = self;
locationPicker.dataSource = self;
locationPicker.showsSelectionIndicator = YES;
locationPicker.tag = 1;
[actionSheet addSubview:locationPicker];
Then its animated in on textfield touch.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if (textField == locationTextField.self)
{
[actionSheet showInView:self.view];
[actionSheet setFrame:CGRectMake(0, 0, 320, 480)];
[nameField resignFirstResponder];
}
}
I believe the problem is with the view you chose to show the action sheet. try to show it in your app's main window.

UIBarButtonItem not responding to click

I have a UITableView where I want the user to be able to click two buttons, in the header of the section, one for adding a new record, and the other one for editing them, so I first tried using tableView viewForHeaderInSection and tableView heightForHeaderInSection like this:
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
return 44;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 360, 44)];
UIToolbar* tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 360, 44)];
UIBarButtonItem* spacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem* addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(onAddVehicleInterest:)];
UIBarButtonItem* editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(onEditVehiclesInterest:)];
[tb setItems:[NSArray arrayWithObjects:spacer, addBtn, editBtn, nil]];
[v addSubview:tb];
return v;
}
- (IBAction)onAddVehicleInterest: (id) sender {
NSLog(#"Add");
}
- (IBAction)onEditVehiclesInterest:(id)sender {
NSLog(#"Edit");
}
When I run the app, I can see the three UIBarButtonItems correctly, and I can click them, but the NSLog line is never called. So I added a UIToolbar directly in the nib file, added the 3 UIBarButtonItem controls to it, and tied the onAddVehicleInterestand onEditVehiclesInterest to the respective buttons. But that doesn't work either...
So, as a last test, I added a UIButton to the nib and tied its Touch Up Inside event to one of the methods, and that works as expected. So I'm confused. What am I doing wrong?
I had a UITapGestureRecognizer on the parent view of the view controller. Once I removed the tap gesture, my UIBarButtonItems began to respond properly to all selectors.
I have also faced the same problem and I get it solved by changing:-
- (IBAction)onAddVehicleInterest: (id) sender {
to
- (IBAction)onAddVehicleInterest: (UIBarButtonItem *) sender {
I also dont know why it was not working earlier, but my problem solved with it.
You can try this.
Try this:
UIBarButtonItem* addBtn;
if([self respondsToSelector:#selector(onAddVehicleInterest:)])
{
addBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(onAddVehicleInterest:)];
}
UIBarButtonItem* editBtn
if([self respondsToSelector:#selector(onEditVehiclesInterest:)])
{
editBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(onEditVehiclesInterest:)];
}
if(addBtn && editBtn)
{
[tb setItems:[NSArray arrayWithObjects:spacer, addBtn, editBtn, nil]];
}

odd behavior of UIPickerView on ActionSheet

- (void) actionsheetstart
{
actionSheet=[[UIActionSheet alloc] initWithTitle:#"" delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[actionSheet showInView:self.view];
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 480, 32)];
[pickerToolbar sizeToFit];
pickerToolbar.barStyle = UIBarStyleBlackTranslucent;
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonSystemItemCancel target:self action:#selector(cancel_clicked:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done_clicked:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[actionSheet addSubview:pickerToolbar];
UIPickerView *picker = [[UIPickerView alloc] init];
picker.frame = CGRectMake(0, 44, 320, 216);
picker.delegate = self;
picker.dataSource = self;
picker.showsSelectionIndicator = YES;
[actionSheet addSubview:picker];
}
-(void)done_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
-(void)cancel_clicked:(id)sender
{
[actionSheet dismissWithClickedButtonIndex:0 animated:YES];
}
for some reason, the actionsheet is fine, but the UIPickerView and the UIToolBar gone crazy.
I've try to set the frame to -px but without any success. what seems to be the problem?
Thanks alot.
Don't insert UIPickerView inside UIActionSheet, you won't get correct behaviour this way. My solution:
Subclass UIButton, implement method canBecomeFirstResponder:, return YES.
Implement inputView method in your subclassed UIButton, return UIPickerView.
Optionally, implement inputAccessoryView method in your subclassed button to return toolbar above your picker.
Make your button becomeFirstResponder: when tapped.
Voila, you have picker shown modally like a keyboard when button is tapped.
EDIT:
MyButton.h
#import <UIKit/UIKit.h>
#interface MyButton : UIButton
#end
MyButton.m
#import "MyButton.h"
#implementation MyButton {
UIDatePicker *_pv;
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
#pragma mark - Picker
- (UIView *)inputView {
// set up your UIPickerView here
_pv = [[UIDatePicker alloc] init];
[_pv setDatePickerMode:UIDatePickerModeTime];
[_pv setMinuteInterval:30];
return _pv;
}
- (UIView *)inputAccessoryView {
// set up your toolbar here
UIToolbar *tb = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *flex = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *ok = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pick:)];
[tb setItems:[NSArray arrayWithObjects:flex, ok, nil]];
return tb;
}
- (void)pick:(id)sender {
// store picker value here before closing inputView
[self resignFirstResponder];
}
#end

UIBarButtonItem not clickable

I have a very tricky problem and after long searching (google, stackoverflow, ...) i didn't get the solution that works for me.
Let me introduce you in my current choosen architecture:
I have a a AppDelegate, which has a UIView that contains a UINavigationController and the application didFinishLaunchingWithOptions: contains:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
UIViewController *myController = [[UIViewController alloc] init];
myController.view = myView;
FSCScrumRootView * myRootView = [[FSCScrumRootView alloc] initWithNibName:#"FSCScrumRootView" bundle:[NSBundle mainBundle]];
[myController.view addSubview:myRootView.navigation.view];
[self.window addSubview:myController.view];
[self.window makeKeyAndVisible];
return YES;
}
In my FSCScrumRootView (inherits from UIViewController) i init the view like this:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigation = [[[UINavigationController alloc] init] autorelease];
self.scrumProjectsList = [[[FSCScrumProjectListView alloc] init] initWithNibName:#"FSCScrumProjectListView" bundle:nil];
[navigation pushViewController:scrumProjectsList animated:YES];
[navigation view];
}
return self;
}
In my FSCScrumProjectListView (it is inherited from UITableViewController) i have implemented the viewDidLoad as following:
- (void)viewDidLoad
{
[super viewDidLoad];
//Set the title
self.navigationItem.title = #"Scrum Projects";
UIBarButtonItem *myRefreshButton = [[[UIBarButtonItem alloc] initWithTitle:#"Refresh" style:UIBarButtonSystemItemRefresh target:self action:#selector(refreshList)] autorelease];
self.navigationItem.leftBarButtonItem = myRefreshButton;
UIBarButtonItem *myLogoutButton = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonSystemItemCancel target:self action:#selector(logout)];
self.navigationItem.rightBarButtonItem = myLogoutButton;
//Initialize the toolbar
toolbar = [[UIToolbar alloc] init];
toolbar.barStyle = UIBarStyleDefault;
//Set the toolbar to fit the width of the app.
[toolbar sizeToFit];
//Caclulate the height of the toolbar
CGFloat toolbarHeight = [toolbar frame].size.height;
//Get the bounds of the parent view
CGRect rootViewBounds = self.parentViewController.view.bounds;
//Get the height of the parent view.
CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
//Get the width of the parent view,
CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
//Create a rectangle for the toolbar
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
//Reposition and resize the receiver
[toolbar setFrame:rectArea];
//Create a button
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc]
initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(info_clicked:)];
[toolbar setItems:[NSArray arrayWithObjects:infoButton,nil]];
//Add the toolbar as a subview to the navigation controller.
[self.navigationController.view addSubview:toolbar];
//Reload the table view
[self.tableView reloadData];
}
This results finally in the following screen (as i'd like to have it):
View iOS Mockup of current result
The Problem:
My Problem now is, that i can click ONLY on the Refresh Button. The other two buttons (Info and Logout) cannot be clicked. And i don't understand why? What I am doing wrong here?
Your help is kindly appreceated!
Try autoreleasing the second two buttons like your first one (the refresh).
UIBarButtonItem *myLogoutButton = [[[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonSystemItemCancel target:self action:#selector(logout)]autorelease];
UIBarButtonItem *infoButton = [[[UIBarButtonItem alloc]
initWithTitle:#"Info" style:UIBarButtonItemStyleBordered target:self action:#selector(info_clicked:)]autorelease];
People, i have found the reason for my problem and i have to say, it was a very very silly one.
The first line of this project was responsible for all the troubels:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 400)];
Since the View i created there is only sized 200x400, it is to small to recognize any events that appear in the outside of this view (although everything is visible).
If i change the size of this view, all works as expected:
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 500)];
But the more dynamic solution would probably be:
CGRect cgRect =[[UIScreen mainScreen] bounds];
CGSize cgSize = cgRect.size;
UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cgSize.width, cgSize.height)];
Maybe there is an even better solution for getting the dynamic screen size?