iOS delegate not working with performSegueWithIdentifier?

孤者浪人 提交于 2019-12-10 19:22:55

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!