UIBarButtonItem not changing its state from 'Edit' to 'Done' , when i click on 'Edit' button - uinavigationbar

I am implementing commitEditingStyle:. When I click on Edit button on navigation bar swipe to delete button will arrive and everything works fine.
Problem is that when i click on the 'Edit' i want to change that button to 'Done' , and when i click on 'Done' those delete buttons on table view cell should disappear , how can i do this i am newbie please guide me where i did the mistake.
Code snippets are shown below:-
- (void)viewDidLoad
{
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(ComposeBtn)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:#selector(setEditing:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
[tools setItems:buttons animated:NO];
[buttons release];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
[super setEditing:editing animated:animated];
[self.listOfFilesTblView setEditing:editing animated:animated];
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
return UITableViewCellEditingStyleDelete;
}
If I use below line of code evrything works fine.
self.navigationItem.rightBarButtonItem=self.editButtonItem;
But I want 2 buttons on navigation bar, 1)Edit 2)Compose.
Please guide me what i am missing here.Thanks in advance.

Related

IOS objective C selector using the wrong scope

I'm using a custom class to hold the initiation code for a particular set of UITextField settings and inputAccessoryView
#interface CustomCountKeyboard : NSObject
It has the below method for initialisation with a UITextField only
-(id) initWithField:(UITextField *)field {
self = [super init];
self.titleField = field;
self.titleLabel = nil;
self.titleField.inputAccessoryView = [self makeToolBarWithWidth:self.titleField.superview.frame.size.width];
return self;
}
And the below method, used by the init method above, to add the accessory views. The doneBtn in the below code specifies a selector of DoneSelectingValue, which is defined elsewhere on the class.
-(UIToolbar *) makeToolBarWithWidth:(CGFloat) width {
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, width, 44)];
[toolBar setTintColor:[UIColor blueColor]];
NSArray *segItemsArray = [NSArray arrayWithObjects: #"", #"c", #"+", nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segItemsArray];
segmentedControl.frame = CGRectMake(0, 0, 200, 30);
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentedControlButtonItem = [[UIBarButtonItem alloc] initWithCustomView:(UIView *)segmentedControl];
self.segmentedControl = segmentedControl;
UIBarButtonItem *doneBtn=[[UIBarButtonItem alloc]initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(DoneSelectingValue)];
UIBarButtonItem *space=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[toolBar setItems:[NSArray arrayWithObjects:segmentedControlButtonItem,space,doneBtn, nil]];
return toolBar;
}
XCode correctly warns me when this DoneSelectingValue method isn't defined on this class. This class has been used elsewhere and it correctly calls the DoneSelectingValue callback in this class.
However, in this instance, it was failing to call this method in the class. Confused, I added the same method definition in the surrounding ViewController, and it called that successfully. This suggests the scope of the selector is wrong.
The only difference between the usage of this code when it works, and this instance when it doesn't, is that the UITextField this time is in a UITableViewCell and the instantiation of the CustomCountKeyboard happens in the didSelectRowAtIndexPath method. If this code is needed, I can edit to provide.
If I put in breakpoints, XCode inspector shows the correct value (The CustomCountKeyboard instance) for 'self' at instantiation time, so the target should be being set correctly.
Any ideas?
Edit: On request, below is the initialisation code for the CustomCountKeyboard
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if( self.textField.text.length > 0 ) {
NSDictionary *species =[self.filteredTableData objectAtIndex:indexPath.row];
[self.obs addObject:species];
[self.textField setText:#""];
[self.textField resignFirstResponder];
self.filteredTableData = [self calculateFilteredResults:self.textField.text];
[self.speciesTable reloadData];
}
else {
TestSpeciesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CustomCountKeyboard *ff = [[CustomCountKeyboard alloc] initWithField:cell.countField];
[ff StartSelecting];
}
}
StartSelecting content added
- (void)StartSelecting {
if( self.titleLabel != nil ) {
self.titleLabel.hidden = true;
}
if( self.titleField != nil ) {
self.titleField.hidden = false;
[self.titleField becomeFirstResponder];
}
else {
[self.titleView becomeFirstResponder];
}
}
So it turns out to be quite simple actually. On selecting of a table row I am instantiating a custom class of CustomCountKeyboard and using it to attach an inputAccessoryView to the field I pass to it. Unfortunately the target property on the UIBarButtonItem is weak, so doesn't stop the class being deallocated.
So by the time the Done button is clicked, the method has completed and the instance has been garbage collected, and consequently is actually not there. The system then instead bubbles the action selector up and does it on the view controller instead.
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TestSpeciesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
CustomCountKeyboard *ff = [[CustomCountKeyboard alloc] initWithField:cell.countField];
[ff makeToolBarWithWidth:self.view.frame.size.width withTarget:ff];
[ff StartSelecting];
}
Changed to:
#property CustomCountKeyboard *countPicker;
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
TestSpeciesCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.countPicker = [[CustomCountKeyboard alloc] initWithField:cell.countField];
[self.countPicker StartSelecting];
}
Thanks to Kampai and trojanfoe for helping me to get there. I'd give you rep if I could.

I am using UIBarButtonItem in navigation bar for open side menu in my project but it is don't work. Here is the code which I implemented:

- (void)leftSideMenuButtonPressed:(id)sender {
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{
[self setupMenuBarButtonItems];
}];
}
Please follow this code.You will get the result.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
if(!self.title) self.title = #"Demo!";
[self setupMenuBarButtonItems];
}
#pragma mark -
#pragma mark - UIBarButtonItems
- (void)setupMenuBarButtonItems {
self.navigationItem.rightBarButtonItem = [self rightMenuBarButtonItem];
if(self.menuContainerViewController.menuState == MFSideMenuStateClosed &&
![[self.navigationController.viewControllers objectAtIndex:0] isEqual:self]) {
self.navigationItem.leftBarButtonItem = [self backBarButtonItem];
} else {
self.navigationItem.leftBarButtonItem = [self leftMenuBarButtonItem];
}
}
- (UIBarButtonItem *)leftMenuBarButtonItem {
return [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"menu-icon.png"] style:UIBarButtonItemStyleBordered
target:self
action:#selector(leftSideMenuButtonPressed:)];
}
- (UIBarButtonItem *)rightMenuBarButtonItem {
return [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"menu-icon.png"] style:UIBarButtonItemStyleBordered
target:self
action:#selector(rightSideMenuButtonPressed:)];
}
- (UIBarButtonItem *)backBarButtonItem {
return [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back-arrow"]
style:UIBarButtonItemStyleBordered
target:self
action:#selector(backButtonPressed:)];
}
#pragma mark -
#pragma mark - UIBarButtonItem Callbacks
- (void)backButtonPressed:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
- (void)leftSideMenuButtonPressed:(id)sender {
[self.menuContainerViewController toggleLeftSideMenuCompletion:^{
[self setupMenuBarButtonItems];
}];
}
- (void)rightSideMenuButtonPressed:(id)sender {
[self.menuContainerViewController toggleRightSideMenuCompletion:^{
[self setupMenuBarButtonItems];
}];
}
#pragma mark -
#pragma mark - IBActions
- (IBAction)pushAnotherPressed:(id)sender {
DemoViewController *demoController = [[DemoViewController alloc]
initWithNibName:#"DemoViewController"
bundle:nil];
[self.navigationController pushViewController:demoController animated:YES];
}
Please make sure you have done basic setup properly as mentioned here :
https://github.com/mikefrederick/MFSideMenu

Back Button image not showing up after showing/hiding SearchBar

I have a SearchBar in my NavigationBar. When the SearchBar is clicked, I am having it show over the "Back" button on the Navigation bar. However, when I re-show the "Back" button, the arrow next to it is no longer visible. Here is what I mean:
And here is my code:
#import "SearchViewController.h"
#interface SearchViewController ()
#end
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.titleView = searchBar;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.hidesBackButton = NO;
}
#end
Any ideas on why this might be happening? Thanks!
just add this line to method searchBarCancelButtonClicked:searchBar:
self.navigationItem.leftBarButtonItem = nil;
*- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.hidesBackButton = YES;
self.navigationItem.titleView = searchBar;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.hidesBackButton = NO;
}*

Not able to populate PickerView with my string

I am using the inputView to display a pickerView when a a textfield is clicked. I then want to populate the pickerview with the numbers 1-100. I have some code that I thought would work, but when i run the program and click on the textField, it just pops up an empty pickerview. Also need to know how to put another button on the toolbar. I would like to have a submit button on the right side to confirm the users selection (going to change the done to cancel) Is there something I have to do in the interface builder with the picker and buttons? because i don't actually have those in the interface builder because they are made with code...
Below are my .h and .m files.
.h----
#interface TestinputviewViewController : UIViewController <UITextFieldDelegate,UIPickerViewDataSource,UIPickerViewDelegate> {
IBOutlet UITextField *textField;
NSMutableArray *pickerViewArray;
}
-(IBAction) textFieldDidBeginEditing;
#end
.m---
#import "TestinputviewViewController.h"
#implementation TestinputviewViewController
-(IBAction)textFieldDidBeginEditing{
UIPickerView *myPickerView = [[UIPickerView alloc] init];
textField.inputView = myPickerView;
myPickerView.showsSelectionIndicator = YES;
myPickerView.delegate = self;
myPickerView.dataSource = self;
[myPickerView release];
UIToolbar *myToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(inputAccessoryViewDidFinish)];
[myToolbar setItems:[NSArray arrayWithObject:doneButton] animated:NO];
textField.inputAccessoryView = myToolbar;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)myPickerView {
return 1;
}
-(NSInteger)pickerView:(UIPickerView *) myPickerView numberOfRowsInComponent: (NSInteger)component {
return [pickerViewArray count];
}
-(NSString *)pikerView:(UIPickerView *) myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return [pickerViewArray objectAtIndex:row];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
pickerViewArray = [[NSMutableArray alloc] init];
for (int i = 1; i<=20; i++) {
NSString *myString = [NSString stringWithFormat:#"%d%",i];
[pickerViewArray addObject:myString];
}
}
You are not setting delegate of picker to the current object class.
use
`myPickerView.delegate = self;`
`myPickerView.dataSource = self;`
after allocating the picker view object.
go on..

Ios Launching Fgallery from UITableView in tabbed application

I am trying to build a small app. It's a tabbed app with 3 tabs: photos, videos, documents
Each tab displays a tableview to select wich gallery, video, document is to be shown; Videos work fine.
I'm having trouble with photo galleries. I use Fgallery which is working fine from the sample: Fgallery git
.h
#import <UIKit/UIKit.h>
#import "FGalleryViewController.h"
#interface FirstViewController : UIViewController <FGalleryViewControllerDelegate, UITableViewDelegate, UITableViewDataSource> {
NSArray *localCaptions;
NSArray *localImages;
NSArray *networkCaptions;
NSArray *networkImages;
FGalleryViewController *localGallery;
FGalleryViewController *networkGallery;
}
#property (nonatomic, strong) UITableView *myTableView;
#end
.m
#import "FirstViewController.h"
#implementation FirstViewController
#synthesize myTableView;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
CGRect tableViewRect = self.view.bounds;
UITableView *tableView = [[UITableView alloc]
initWithFrame:tableViewRect
style:UITableViewStylePlain];
self.myTableView = tableView;
self.myTableView.autoresizingMask =
UIViewAutoresizingFlexibleHeight |
UIViewAutoresizingFlexibleWidth;
[self.view addSubview:self.myTableView];
self.myTableView.dataSource = self;
self.myTableView.delegate = self;
}
and
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog( #"Choix Table");
if( indexPath.row == 0 ) {
NSLog( #"selection 1");
localGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
[self.navigationController pushViewController:localGallery animated:YES];
}
else if( indexPath.row == 1 ) {
networkGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
[self.navigationController pushViewController:networkGallery animated:YES];
...
}
I really don't know how to display the gallery. The didSelectRowAtIndexPath is the one from fgallery, I tried to modify it to show the viewm but I'm new to Objective-C and I'm stuck.
Any help or guideline will be appreciated.
Thanks
I added
if( indexPath.row == 0 ) {
NSLog( #"selection 1");
localGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
// Create the navigation controller and present it modally.
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:localGallery];
[self presentModalViewController:navigationController animated:YES];
View shows ok now but I am missing the back button
In FGalleryViewController.m, I added btn2 to the navigation controller
- (void)setUseThumbnailView:(BOOL)useThumbnailView
{
_useThumbnailView = useThumbnailView;
if( self.navigationController ) {
if (_useThumbnailView) {
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithTitle:#"See All" style:UIBarButtonItemStylePlain target:self action:#selector(handleSeeAllTouch:)] ;
[self.navigationItem setRightBarButtonItem:btn animated:YES];
UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(dismissModalViewControllerAnimated:)] ;
[self.navigationItem setLeftBarButtonItem:btn2 animated:YES];
}
else {
[self.navigationItem setRightBarButtonItem:nil animated:NO];
}
}
}
And voilĂ 

Resources