iOS 委托(delegate)不使用 performSegueWithIdentifier?

标签 ios xcode delegates segue

我在 Storyboard中有两个 Controller (第一个,第二个),xcode 4.2。

第一个 Controller 有一个 TableView 并嵌入在导航 Controller 中。 第二个 Controller 也有一个 tableview 并嵌入在导航 Controller 中(与第一个不同)

在 first.h 中:

#import "second.h"
...
@interface first : UIViewController <secondDelegate, UITableViewDelegate, UITableViewDataSource>
...

在 first.m 中:

- (IBAction)add:(id)sender // action when tapped a button on topbar
{
    [self performSegueWithIdentifier:@"addSegue" sender:sender];
}

....

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if([[segue identifier] isEqualToString:@"addSegue"])
    {
        NSLog(@"delegated");
        second *controller=[segue destinationViewController];
        controller.delegate=self;
    }
}

- (void)callback
{
     NSLog(@"Callback here");
}

Segue 是一种带有默认转换的模态 segue。

second.h:

@protocol secondDelegate
   -(void)callback;
@end

....

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

second.m:

... (button of topbar tapped action) ...
[self dismissModalViewControllerAnimated:YES];
NSLog(@"class: %@",[self delegate]);
[[self delegate]entryGroupDoneButtonTapped];

总结:

我没有看到“此处回调”消息,但我收到了“委托(delegate)”消息。 “class:”调试行打印“null”。

为什么?

(我可以用这个从第一到第二发送任何数据,只有委托(delegate)的回调不起作用)

最佳答案

我找到了一个解决方案:destinationViewController 返回一个 UInavigationController,所以我们可以使用:AddDrinkViewController *controller=[[[segue destinationViewController]viewControllers]objectAtIndex: 0];

关于iOS 委托(delegate)不使用 performSegueWithIdentifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8491410/

相关文章:

ios - 从 UIViewController 中删除我的自定义 MKMapView

ios - addArrangedSubview 是重叠 View

ios - 如果我的应用程序在其各种 View Controller 中使用位置管理器,我应该在哪里实例化它?

c# - 在事件声明中添加匿名空委托(delegate)有什么缺点吗?

ios - 读取 sqlite 数据库中的第二行

xcode - 更改 Xcode 中的模板

xcode - 场景编辑器不显示网格

ios - 用于签署可执行文件的身份不再有效 (iOS 8+/Xcode 7)

ios - SWRevealViewController 委托(delegate)未触发

C#:创建具有 bool 返回类型的多播委托(delegate)