0

我设置了以下简单的视图控制器类

@protocol ThermoFluidsSelectorViewControllerDelegate;

@interface ThermoFluidsSelectorViewController : UIViewController <UITextFieldDelegate>
@property (weak, nonatomic) id <ThermoFluidsSelectorViewControllerDelegate> delegate;
// user hits done button
- (IBAction)done:(id)sender;
@end

@protocol ThermoFluidsSelectorViewControllerDelegate <NSObject>
-(void) didFinishSelection:(ThermoFluidsSelectorViewController *)controller fluidID:    (NSString *)fluidID;
@end

'didFinishSeletion: fluidID:' 方法在主视图控制器中定义,并且在调用时应该关闭选择器视图控制器。当按下完成按钮时,将调用以下方法:

- (IBAction)done:(id)sender
{
    [[self delegate] didFinishSelection:self fluidID:nil];
}

'done:' 方法被调用(通过警报检查)但 'didFinishSelection...' 没有被调用,因此视图不会返回到主屏幕。有任何想法吗?

4

1 回答 1

0

听起来您还没有在主视图控制器中分配您的委托。

您应该在设置委托的主视图控制器中有类似的内容:

    ThermoFluidsSelectorViewController *view = [[ThermoFluidsSelectorViewController alloc] init];
    view.delegate = self;

在这里你可以看到我创建了视图,然后将视图的委托设置回我自己。

如果您不是以编程方式创建 Thermo... 视图控制器,而是使用了故事板,那么您可以在主视图控制器的 prepareForSegue: 方法中设置委托:

// Do some customisation of our new view when a table item has been selected
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Make sure we're referring to the correct segue
    if ([[segue identifier] isEqualToString:@"MySegueID"]) {

    // Get reference to the destination view controller
    ThermoFluidsSelectorViewController *cont = [segue destinationViewController];

    // set the delegate
    cont.delegate = self;

希望这可以帮助。

于 2012-05-14T01:18:42.523 回答