调用方法来查看控制器委托,不会忽略模态视图

时间:2012-05-14 01:11:31

标签: objective-c ios5 delegates xcode4.3

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

@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 ...',因此视图不会恢复到主屏幕。有什么想法吗?

1 个答案:

答案 0 :(得分: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;

希望这有帮助。