2016-08-04 208 views
2

委託報關無法從呈現視圖控制器

// FolderListViewController.h 
#import <UIKit/UIKit.h> 
#import "Folder.h" 

@protocol FolderSelectionDelegate <NSObject> 

@required 
- (void)setFolder:(Folder *)folder; 

@end 

@interface FolderListViewController : UITableViewController 

@property (nonatomic, assign) id<FolderSelectionDelegate> delegate; 

- (IBAction)showDashboard:(id)sender; 

@end 

代表從didSelectRowAtIndexPath方法稱爲訪問委託方法:

// FolderListViewController.m 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    [self.delegate setFolder:dataSource[indexPath.row]]; 
} 

的VC應接收該消息

#import <UIKit/UIKit.h> 
#import "FolderListViewController.h" 
#import "Folder.h" 

@interface ProjectListViewController : UITableViewController <FolderSelectionDelegate, UISplitViewControllerDelegate, UISearchBarDelegate, UINavigationControllerDelegate> 

@property (nonatomic,retain)UIActivityIndicatorView *activityIndicatorObject; 
@property (nonatomic, copy)Folder *folder; 


-(void)loadProjects:(Folder*)folder; 

@end 

介紹VC的行動

- (void)foldersButtonTapped { 
    UINavigationController *vc = [[UIStoryboard storyboardWithName:@"MainStoryboard_iPhone" bundle:nil] instantiateViewControllerWithIdentifier:@"FolderListNavController"]; 
    vc.delegate = self; 
    [self presentViewController:vc animated:YES completion:nil]; 
} 

委託方法實現

- (void)setFolder:(Folder *)folder { 
    _folder = folder; 
    [self loadProjects:folder]; 
} 

我已經經歷了多線程上讀到這裏,還沒有任何運氣。起初,我在展示FolderListVC(即vc.delegate = self)時沒有引用ProjectListVC設置。雖然這似乎不是問題。我正在研究爲iPad構建的應用程序,並將其擴展到所有設備上。這裏的實現工作(它被設置爲分割視圖控制器)。任何幫助將不勝感激

+0

它在我看來你是設置'UINavigationController'的委託,而不是你的委託。您需要在「FolderListViewController」實例上設置委託。您也可以將您的委託屬性重命名爲'folderSelectionDelegate',以避免混淆。 – albertamg

+1

非常感謝 - 這表明我朝着正確的方向 – user1987502

回答

0

我認爲,UINavigationcontroller創建問題,因爲它沒有設置FolderListViewController委託。你可以嘗試這樣。

FolderListViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"FolderListViewController"]; 

    vc.delegate=self; // protocol listener 
    [self.navigationController pushViewController:vc animated:YES]; 
+0

謝謝!最後以這種方式實現它,所以我仍然可以呈現視圖控制器並在頂部維護導航欄: 'FolderListViewController * vc = [self.storyboard instantiateViewControllerWithIdentifier:@「FolderListPhone」]; UINavigationController * nav = [[UINavigationController alloc] initWithRootViewController:vc]; vc.delegate = self; [self presentViewController:nav animated:YES completion:nil];' – user1987502

+0

尼斯:) ..解決了這兩個問題 – Nilesh

相關問題