xcode iOS 委托不适用于 performSegueWithIdentifier?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8491410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:44:14  来源:igfitidea点击:

iOS delegate not working with performSegueWithIdentifier?

iosxcodedelegatessegue

提问by mudlee

I have two controllers (first,second) in a storyboard, xcode 4.2.

我在故事板 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)

第一个控制器有一个 tableview 并嵌入到导航控制器中。第二个控制器也有一个 tableview 并嵌入到导航控制器中(与第一个不同)

In first.h:

在第一个.h:

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

In first.m:

在第一个.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.

Segue 是一个带有默认过渡的模态转场。

second.h:

第二个.h:

@protocol secondDelegate
   -(void)callback;
@end

....

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

second.m:

第二个.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".

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

Why?

为什么?

(I can send any data from first to second with this, only delegate's callback not works)

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

回答by mudlee

I found a solution: the destinationViewControllerreturns a UInavigationController, so we can use: AddDrinkViewController *controller=[[[segue destinationViewController]viewControllers]objectAtIndex:0];

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