我在 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/