ios - 如何在不使用 prepareForSegue 的情况下为类设置委托(delegate)

标签 ios iphone delegation

我有两个类(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 中选择的内容。

http://oi62.tinypic.com/2li99w1.jpg

最佳答案

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

相关文章:

ios - 当数据为 NULL 时,访问链表的数据元素时应用程序崩溃

ios - 不会调用其他类中的方法,Objective-C

ios - 为 iPhone 聊天应用程序实现自己的用于发送和接收的笑脸图标(我有自己设计的图像)

iphone - 使用依赖子项目时 Xcode 中的链接问题

ios - 在 iPhone 上继续中断的下载

iphone - Xcode 3.2.6 适合运行 iOS 5 beta 6 的 iPhone 吗?

iphone - Objective C中的内存管理,NSString

python - 在 Python : delegating __contains__ to contained-object correctly 中模拟成员资格测试

groovy - Groovy 中 @Delegate 和 @Mixin AST 转换之间的区别

c++ - 使用 Qt 在 C++ 中委派类构造函数