2014-12-29 17 views
0

我正在開發文件管理器應用程序,我在列表視圖中列出所有目錄。根據用戶選擇的內容進行選擇我正在重新加載表視圖的內容。 (例如,用戶將顯示目錄列表當他點擊可以說音樂目錄我會加載所有目錄在音樂目錄如何基於表視圖中重新加載的數據啓用/禁用UIBarButton?

現在我真的需要做的是我有返回按鈕上但我希望它當用戶不在根目錄中啓用我應該在哪裏把我的代碼做

編輯:

我的代碼下

#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; 
} 
} 
+0

最好的方法可以讓你將與子目錄的路徑推動同一類的其他的viewController(使用一個UINavigationController)。後退按鈕將自動被觸發。 – ZeMoon

+1

在您的cellForRowAtIndexPath委託方法中檢查它是否是根目錄並相應地執行操作。 –

+0

Dheeraj我已經試過,沒有運氣:( –

回答

1

所以這裏是解決你的問題。 假設您有一個視圖控制器,並且您的根目錄下有可視導航控制器&。在你的課堂上創建一個名爲BOOL isRoot的iVar。最初將其設置爲True。

@property(nonatomic) BOOL isRoot; 

現在編寫自定義設置器。

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

現在根據你挖掘到子目錄或在層次上來的邏輯,只需更新該isRoot值。

您也可以按照NSNotificationCenter方法來發送通知調用此方法在每次重裝玩具您的tableView。

希望有所幫助。

+0

更新好吧,我沒有嘗試過的NSNotificationCenter但似乎說服力給我。我一定會嘗試一下 –

0

在你的根只有一條線路解決方案class(在viewDidLoad中)寫下這一行:

self.navigationItem.leftBarButtonItem.enabled = NO; 

,並參考這個問題 same question here

+0

我只有已嵌入式導航控制器的單一視圖控制器和我在表視圖重載數據我不認爲viewDidLoad中會在這裏起什麼作用 –

+0

@AadilKeshwani:你將怎麼做?與「嵌入式導航控制器」的意思是,你的意思是說,你已經被推到導航控制器 –

+0

@Balram蒂瓦里只是一個視圖控制器:我會添加其他視圖控制器添加新目錄中創建新的文件和其他文件 –

0

你可以做這樣的事情:

  1. 設置一個TableViewController爲根視圖導航控制器的控制
  2. 設置是NavigationController爲根視圖你的應用程序控制器。

然後你只要按任何你在導航控制器中查看,後退按鈕會自動隱藏,當你在根。

編程:

如果你知道你是在根目錄,然後做到這一點;

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

編輯:

乘坐NSMutableArray。在你的ViewDidLoad push @「Root」中,就像你在root中一樣。並補充一點:

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

在你didSelectRowAtIndexPath

推,你在NSMutableArray會。並檢查是否MutableArray.count> 1,則self.navigationItem.leftBarButtonItem.enabled = YES;

在GoBack方法

做到這一點:

- (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 
} 

希望這有助於.. :)

+0

我只有一個視圖控制器,它具有嵌入的導航控制器,我正在重新加載表視圖上的數據,我不認爲viewWillAppear會在這裏扮演任何角色 –

+1

@aadilKeshwani>是的你是對的。 。 – Rashad

+0

@aadilKeshwani>查看我的編輯 – Rashad

相關問題