UIDatePicker Replaces Keyboard only on Last Uitableviewcell - objective-c

I have a tableview where a user can add a new row that contains two UITextFields in them. The user can add a date to each textview (or not add one, leaving it empty) with a UIDatePicker. Once the process is completed and everything is saved, the user should be able to go to any row and update the dates in that row. The problem is that any row previous to the very last row created only shows the regular keyboard when it is time to edit. I can't seem to get the datepicker to show up except in the last saved row. Any thoughts on why this is happening?
Here is how I set up the datepicker:
-(void)showDatePicker{
_datePicker = [[UIDatePicker alloc] init];
UIToolbar *toolbar = [[UIToolbar alloc]init];
[toolbar sizeToFit];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(done)];
doneButton.tintColor = [UIColor whiteColor];
UIBarButtonItem *flexibleSpaceLeft = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
toolbar.translucent = YES;
toolbar.barTintColor = [UIColor colorWithRed:8/255.0 green:46/255.0 blue:46/255.0 alpha:1];
[toolbar setItems:[NSArray arrayWithObjects:flexibleSpaceLeft,doneButton ,nil]];
[_readDateCell.startDateTextfield setInputAccessoryView:toolbar];
[_readDateCell.finishDateTextfield setInputAccessoryView:toolbar];
[_datePicker setDatePickerMode:UIDatePickerModeDate];
[_readDateCell.startDateTextfield setInputView:_datePicker];
[_readDateCell.finishDateTextfield setInputView:_datePicker];
}
The function is called in two places - when the textfield begins editing and when a new row is being created:
-(void)textFieldDidBeginEditing:(UITextField *)textField {
[self showDatePicker];
}
-(void)addNewRow {
[self showDatePicker];
}

If you are setting input view for your text field then there is no need to call showDatePicker on didBeginEditing delegate method of UITextField.
Then you need to add the datePicker code in your viewDidLoad method. And in cellForRowAtIndexPath you need to set the inputAccessory and inputView

Related

How to change leftBarbuttonItem title?

I want to change leftBarButton title. I've tried this
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setLeftBarButtonItem:newBackButton];
but it brings changes like
But I want it like
How it can be possible?
It may helpful for you
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = backBarButton;
From the view controller (ViewController1) that is pushing the next view controller (ViewController2), you need to set the backBarButtonItem.
#implementation ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
}
It's totally normal that you don't see any arrow in your back button. That's because you're creating a new bar button that you set its title and its style. But the style you've set for your bar button item won't give you what you wanted.
I suppose that you're using UINavigationController to navigate. If it's the case, you need to get a reference to navigationBar that UINavigationController displays on the screen. Then, you'll be able to change its color via tint color property. So,
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
would do what you want to achieve. Here is the image i obtained using the code i mentioned above.
If you're not using UINavigationController, the simplest solution is to provide an image for your back button which has already an arrow.
You should set backBarButtonItem instead of leftBarButtonItem. And remember to set it not in current view controller (where you wish it to be displayed) but in parent one (to which this button will return you).
Use this Code and
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
and for going to back use this method .
-(void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
This worked for me
UIImage *image = [[UIImage imageNamed:#"btn_back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = button;
- (IBAction)backAction:(id)sender
{
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
[self.navigationController pushViewController:mainMenuViewController animated:YES];
}

Adding UIBarButtonItem's to a UIToolbar

I need to add buttons to the UIToolbar of a UINavigationController, which is also my root. I want the UIToolbar to appear when a specific UIViewController is shown. Therefore, I placed this code in my viewDidLoad method of my UIViewController subclass:
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithTitle:#"Title" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething)];
item.width = 300;
item.tintColor = [UIColor whiteColor];
UIBarButtonItem* item2 = [[UIBarButtonItem alloc] initWithTitle:#"Title2" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething)];
NSMutableArray* theItems = [self.navigationController.toolbar.items mutableCopy];
[theItems addObject:item];
[theItems addObject:item2];
[self.navigationController.toolbar setBarStyle:UIBarStyleBlackOpaque];
[self.navigationController setToolbarHidden:NO animated:YES];
[self.navigationController setToolbarItems:theItems animated:NO];
//self.navigationController.toolbarItems = theItems; // Tried both
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
label.text = #"kjhdkjhadsda";
[self.navigationController.toolbar addSubview:label];
This only shows the UILabel in the correct position, nothing else appears. The UILabel is useless for me, it is just a test. I also tried to allocate a new array instead of copying the one from the component. Ignore the missing releases, this is only test code.
I read many questions about this but no answer seem to help making it work. Any idea what might not be ok in this code?
Seems that you are trying make mutable copy of nil in line
[self.navigationController.toolbar.items mutableCopy];
By default method navigationController.toolbar.items nas no items and returns nil
Update
Method - (void)setToolbarItems:(NSArray*)toolbarItems animated:(BOOL)animated does nothing if you send it to UINavigationController. You need to set toolbar items to that controller who is managed by a navigation controller. This line will make your buttons visible:
[self setToolbarItems:theItems animated:NO];

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!

UISearchBar in UIView in UIBarButtonItem does not work as expected

Im trying to add UIsearchBar to my navigation controller. Also i wish to use cancel button of UISearchBar.
UISearchBar* searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, 300, searchBar.frame.size.height);
searchBar.translucent = NO;
searchBar.barStyle = UIBarStyleBlack;
searchBar.delegate = self;
UIView* myView = [[UIView alloc] initWithFrame:searchBar.frame];
[myView addSubview:searchBar];
UIBarButtonItem* sbItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:chart,sbItem,nil]];
trouble that searchBar is inactive. it appears in navigation controller. But when i tap on it it does not response at any way. If i show cancel button it appears but also not clickable.
If i doesnt wrap UIsearchBar at UIView it work fine except it doesnt want to show me a cancel button.
UPD
IF i call
[searchBar becomeFirstResponder]
it goes to edit mode and shows me a cancel button and keyboard, but looks like view in which searchBar wrapped changes it frame params and goes on 10px down from its original appearence. And change its background from trnsparent to blackopaque
Looks like a some kind of Voodoo... So simple action takes so much time...
i think you have mistaken at search bar frame,you took search bar height as searchBar.frame.size.height,but there was no height at that time,just change it like:
UISearchBar* searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, 300, 40);
searchBar.translucent = NO;
searchBar.barStyle = UIBarStyleBlack;
searchBar.delegate = self;
UIView* myView = [[UIView alloc] initWithFrame:searchBar.frame];
[myView addSubview:searchBar];
UIBarButtonItem* sbItem = [[UIBarButtonItem alloc] initWithCustomView:myView];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObjects:chart,sbItem,nil]];
i am sorry,i don't know what the chart is here,hope it is what you are looking for.

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