我有两个类(class),我想让他们互相交谈。 A 类包含一个 tableView,当用户点击表格行时,我会触发 didSelectRowAtIndexPath 方法。在这种方法中,我需要通过委托(delegate)将此事通知 B 类。我知道委托(delegate)是如何工作的,但很难弄清楚如何在不使用 prepareForSegue 方法的情况下设置 A 的委托(delegate)。
通常我会在设置委托(delegate)时这样做
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"goToManipulator"]) {
ManipulatorViewController *secondVC = (ManipulatorViewController *) segue.destinationViewController;
[secondVC setDelegate:self];
}
}
但是如何在不使用 prepareForSegue 的情况下设置委托(delegate)?
提前致谢
编辑:
这就是我的 Storyboard的结构。 “接收者” View Controller 将获取数据并显示在“当前名称”标签中,具体取决于从最靠近右侧的“发送者” View Controller 在 TableView 中选择的内容。
最佳答案
- (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
ManipulatorViewController *secondVC = [[ManipulatorViewController alloc] init...];
[secondVC setDelegate:self];
//if you use push transition in UINavigationController
[self.navigationController pushViewController:secondVC animated:YES];
//if you use modal transition
[self presentViewController:secondVC animated:YES completion:nil]
}
init...
表示初始化取决于您的程序架构。
编辑
如果你想从 Storyboard中获取secondVC
,使用
UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
ManipulatorViewController* secondVC = [storyboard instantiateViewControllerWithIdentifier:@"secondVC"];
并且不要忘记在 Storyboard中为您的 viewController 添加标识符。
关于ios - 如何在不使用 prepareForSegue 的情况下为类设置委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21753596/