OStack程序员社区-中国程序员成长平台

标题: ios - 无法从呈现的 View Controller 访问委托(delegate)方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 17:10
标题: ios - 无法从呈现的 View Controller 访问委托(delegate)方法

委托(delegate)声明

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

@protocol FolderSelectionDelegate <NSObject>

@required
- (void)setFolderFolder *)folder;

@end

@interface FolderListViewController : UITableViewController

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

- (IBAction)showDashboardid)sender;

@end

从 didSelectRowAtIndexPath 调用委托(delegate):

// FolderListViewController.m
- (void)tableViewUITableView *)tableView didSelectRowAtIndexPathNSIndexPath *)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)loadProjectsFolder*)folder;

@end

呈现 VC 的 Action

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

委托(delegate)方法实现

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

我已经阅读了这里的多个线程并且没有任何运气。起初,在展示 FolderListVC(即 vc.delegate = self)时,我没有提到 ProjectListVC 设置。不过,这似乎不是问题所在。我正在开发一个为 iPad 构建的应用程序,并将其扩展到所有设备上。此处的实现有效(它设置为 Split View Controller )。任何帮助将不胜感激



Best Answer-推荐答案


我认为 UINavigationcontroller 正在制造问题,因为它没有设置 FolderListViewController 委托(delegate)。你可以这样试试吗?

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

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

关于ios - 无法从呈现的 View Controller 访问委托(delegate)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38769673/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4