gpt4 book ai didi

ios - 无法从呈现的 View Controller 访问委托(delegate)方法

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:58:36 25 4
gpt4 key购买 nike

委托(delegate)声明

// 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 调用委托(delegate):

// 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的 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)setFolder:(Folder *)folder {
_folder = folder;
[self loadProjects:folder];
}

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

最佳答案

我认为 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com