客观c中不同控制器的调用方法

时间:2014-07-15 12:17:05

标签: objective-c cocoa callback modalviewcontroller

我有一个视图控制器,我将使用[runmodal]显示一个窗口控制器。我在模态窗口中有一些文本字段和按钮。当我单击按钮时,我需要使用来自窗口控制器的集合数据在视图控制器中调用方法。我怎样才能实现这一目标?是否与自定义委托方法有关?因为我是Mac dev和Objective c的新手,所以有人帮我做。

3 个答案:

答案 0 :(得分:2)

您可以使用委派模式。您可以定义这样的协议:

@protocol DataProviderDelegate <NSObject>
- (NSDictionary *) retrieveData;
@end

在视图控制器中实现此协议:

@interface MainViewController () <DataProviderDelegate>
@end

@implementation MainViewController {
...
- (NSDictionary *) retrieveData {
    ....
}

...
@end

在窗口控制器中定义委托属性

@interface ModalWindowController : NSWindowController

@property (nonatomic, weak) id <DataProviderDelegate> dataProviderDelegate;

@end

从主视图控制器中,将该属性设置为self

modalWindow.dataProviderDelegate = self

此时,模态窗口控制器能够调用DataProviderDelegate协议中定义的视图控制器的任何方法 - 例如:

if (self.dataProviderDelegate) {
    [self.dataProviderDelegate retrieveData];
}

要更多地了解委托模式,我建议谷歌为它

答案 1 :(得分:1)

您可以使用委托。在ModalViewController中,您必须实现一个每个操作都有一个方法的委托(textfields,buttons):

<强> ModalViewController.h

@class ModalViewController;

@protocol ModalViewControllerDelegate <NSObject>

- (void)modalViewControllerDelegateButtonPressed:(APPCameraOverlay *)overlay;
- (void)modalViewControllerDelegate:(APPCameraOverlay *)overlay
                    textFieldEdited:(NSString *)text;

@end

@interface ModalViewController : UIViewController

@property (nonatomic, weak) id <ModalViewControllerDelegate> delegate;

@end

然后,您将能够在ModalViewController.m中调用您的委托方法:

<强> ModalViewController.m

// The method linked to your button
- (IBAction)actionButtonPressed {
    [self.delegate modalViewControllerDelegateButtonPressed:self];
}

// Your textfield method that is called when input has changed
- (void)textFieldDidEndEditing:(UITextField *)textField {
    [self.delegate modalViewControllerDelegate:self
                               textFieldEdited:textField.text];
}

现在,您只需在显示模态控制器时在ViewController中设置ModalViewController委托对象:

<强> ViewController.m

#import "ModalViewController.h"

// We create an extension to the class to implement the delegate protocol
@interface ViewController () <ModalViewControllerDelegate>

@end

@implementation ViewController

// This method gets called by apple when a view controller is showed (modally, pushed or embedded)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // if the view that is showed is the ModalViewController
    if ([segue.destinationViewController isKindOfClass:[ModalViewController class]]) {
        ModalViewController *controller = segue.destinationViewController;
        controller.delegate = self;
    }    
}

// You have to implement the delegate methods now :

- (void)modalViewControllerDelegateButtonPressed:(APPCameraOverlay *)overlay {
    // Do whatever you want when the button is pressed on the ModalViewController
}

- (void)modalViewControllerDelegate:(APPCameraOverlay *)overlay
                    textFieldEdited:(NSString *)text {
    // Do whatever you want when the textfield is edited
}

@end

答案 2 :(得分:0)

将视图控制器作为实例变量添加到(子类)窗口控制器,然后单击该按钮时,可以使用窗口控制器中的集合数据向视图控制器发送方法。