Aadil Keshwani
Aadil Keshwani

Reputation: 1386

How to enable/disable UIBarButton based on the data reloaded in table view?

I am developing File Manager Application where I am listing all Directories in a table view. On selection based on what user has selected I am reloading content of the table view. (For eg User will be showed a list of Directories When he tap on lets say Music Directory I will load all Directories in Music Directory.

Now what i really need to do is i have back button on top But i want it to be enabled when user is not in Root Directory. Where should i place my code to do that ?

EDIT:

My code is as under

#import "ViewController.h"
#import "AddDirectory.h"
@interface ViewController ()
@property NSArray *fileList;
@property NSFileManager *manager;
@property (weak, nonatomic) IBOutlet UITableView *tableView1;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *bkbutton;
@property NSMutableString *currentDir;
@property NSString *rootpath;
@end

@implementation ViewController
- (void)viewWillAppear:(BOOL)animated{

[self.tableView1 reloadData];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.manager=[NSFileManager defaultManager];
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory=[paths objectAtIndex:0];
self.fileList=[self.manager contentsOfDirectoryAtPath:documentDirectory error:nil];
self.currentDir=[[NSMutableString alloc] init];
[self.currentDir appendString:documentDirectory];
self.rootpath= self.currentDir;
//NSLog(@"%@",self.fileList);
// self.fileList=contents;

// Do any additional setup after loading the view, typically from a nib.
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (IBAction)backDirectory:(id)sender {
self.currentDir = [NSMutableString stringWithFormat:@"%@", [self.currentDir stringByDeletingLastPathComponent]];
self.fileList = [self.manager contentsOfDirectoryAtPath:self.currentDir error:nil];
[self.tableView1 reloadData];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
// NSLog(@"%i",[self.fileList count]);
return [self.fileList count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath  {
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell1"];
NSString *cfname=[self.fileList  objectAtIndex:[indexPath row]];
//    NSString *fname=[[cfname lastPathComponent] stringByDeletingPathExtension];
    NSString *fname=[cfname lastPathComponent];
// NSLog(@"%@",fname);
cell.textLabel.text=fname;

return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if([segue.identifier isEqualToString:@"adddir"]){
    AddDirectory *ad=segue.destinationViewController;
    ad.cpath=self.currentDir;

}
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if(self.currentDir!=nil){
    [self.currentDir appendString:[NSString stringWithFormat:@"/%@",[self.fileList objectAtIndex:indexPath.row]]];
    self.fileList=[self.manager contentsOfDirectoryAtPath:self.currentDir error:nil];
    NSLog(@"%@",self.currentDir);
    [tableView reloadData];

}
if([self.currentDir isEqualToString:self.rootpath]){
    self.navigationItem.leftBarButtonItem.enabled = NO;
}
}

Upvotes: 0

Views: 127

Answers (3)

Rashad
Rashad

Reputation: 11197

You can do something like this:

  1. Set a TableViewController as root view controller of navigation controller
  2. Set that NavigationController as root view controller of you app.

Then you just push whatever you view in Navigation controller, the Back Button will automatically hide when you are in root.

Programmatically:

If you know a you are in root then do this;

-(void)viewWillAppear:(BOOL)animated {
    self.navigationItem.hidesBackButton = YES;
}

EDIT:

Take a NSMutableArray. In your ViewDidLoad push @"Root", as you are in root. and add this:

UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"Back"] style:UIBarButtonItemStylePlain target:self action:@selector(goBack:)];
self.navigationItem.leftBarButtonItem = backBarButton;
self.navigationItem.leftBarButtonItem.enabled = NO;

In you didSelectRowAtIndexPath

push where you are going in that NSMutableArray. And check if MutableArray.count > 1 then self.navigationItem.leftBarButtonItem.enabled = YES;

in your goBack method do this:

- (void) goBack:(id)sender {
    //pop last item form NSMutableArray
    //Check array count if it is still > 1 keep the leftButton enabled 
    // if count is ==1 disable the left button
}

Hope this helps.. :)

Upvotes: 0

Balram Tiwari
Balram Tiwari

Reputation: 5667

So here is the solution to your problem. Let's say you have a view controller with visible navigation controller & you are at your Root directory. Create an iVar in your class as BOOL isRoot. set it to True initially.

@property(nonatomic) BOOL isRoot;

Now write the custom setter.

-(void)setIsRoot:(BOOL)isRoot{
    _isRoot = isRoot;
    self.navigationItem.hidesBackButton = !isRoot;
}

Now based on your logic of digging into subdirectory or coming up in hierarchy, just update this isRoot value.

You can also follow the NSNotificationCenter approach to send notification to invoke this method on every time toy reload your tableVIew.

Hope that helps.

Upvotes: 1

Nitin
Nitin

Reputation: 451

just one line solution in your root class (in viewDidLoad) write this one line :

self.navigationItem.leftBarButtonItem.enabled = NO;

and refer to this question same question here

Upvotes: 0

Related Questions