问题
I have two controllers (first,second) in a storyboard, xcode 4.2.
First controller has a tableview and embedded in navigation controller. Second controller has a tableview too and embedded in navigation controller (not same as the first)
In first.h:
#import "second.h"
...
@interface first : UIViewController <secondDelegate, UITableViewDelegate, UITableViewDataSource>
...
In 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 is a modal segue with default transition.
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];
Summary:
I don't see the "callback here" message, but I've got a "delegated" message. The "class: " debug line print "null".
Why?
(I can send any data from first to second with this, only delegate's callback not works)
回答1:
I found a solution: the destinationViewController returns a UInavigationController, so we can use: AddDrinkViewController *controller=[[[segue destinationViewController]viewControllers]objectAtIndex:0];
来源:https://stackoverflow.com/questions/8491410/ios-delegate-not-working-with-performseguewithidentifier