Objective c 实现委托以使模式视图能够将数据传递回UIViewController

Objective c 实现委托以使模式视图能够将数据传递回UIViewController,objective-c,xcode,delegates,Objective C,Xcode,Delegates,我正在尝试实现一个委托,以使模态视图能够将数据传递回UIViewController 我有两个视图控制器,我的主UIViewController和模态。使用下面的代码,[delegate translationTextEntered:@“Test”];不影响主屏幕(即“translationTextEntered”从未被调用) 我的主控制器 这包含当模态具有用户值时要调用的方法: MainViewController.h #import "SuggestionViewController.h"

我正在尝试实现一个委托,以使模态视图能够将数据传递回UIViewController

我有两个视图控制器,我的主UIViewController和模态。使用下面的代码,[delegate translationTextEntered:@“Test”];不影响主屏幕(即“translationTextEntered”从未被调用)

我的主控制器 这包含当模态具有用户值时要调用的方法:

MainViewController.h

#import "SuggestionViewController.h"

@interface MainViewController : UIViewController <SelectTranslationDelegate>

// - (void)translationTextEntered:(NSString *)txt;  <- Not required
MainViewController.m

// The method where you instantiate SuggestionViewController
{
     // .. do your work

     SuggestionViewController *suggestionViewController = [[SuggestionViewController alloc] init];

     suggestionViewController.delegate = self; // <- This is the missing line

     [self presentModalViewController:suggestionViewController animated:YES];
     // [suggestionViewController release]; suggestionViewController = nil; // I'm assuming your using ARC

}
我的模态控制器 它包含一个UITableView,其中包含委托,当选择某个项目时,应触发委托回调

SuggestionViewController.h

#import "SuggestionViewController.h"

@interface MainViewController : UIViewController <SelectTranslationDelegate>

// - (void)translationTextEntered:(NSString *)txt;  <- Not required

在模态视图中,viewDidLoad或view中的控制器将显示以下语句

  • 创建主视图控件的对象。。。鉴于我的想法

    mainViewController*mainVC=[[mainViewController alloc]initwithnobname]

  • 然后定一个句子

    self.delegate=mainVC


  • 这是您需要做的事情…

    应该是这样的:

    MainViewController.h

    #import "SuggestionViewController.h"
    
    @interface MainViewController : UIViewController <SelectTranslationDelegate>
    
    // - (void)translationTextEntered:(NSString *)txt;  <- Not required
    
    还应注意,模态视图控制器不应符合
    SelectTranslationDelegate
    ,因为这很可能不是您的意图。所以你的声明应该是这样的:

    @interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
    
    @界面建议ViewController:UIViewController
    

    您要响应的是
    MainViewController
    translationtext输入:
    not
    SuggestionViewController
    SuggestionViewController
    是在
    delegate

    上进行
    translationTextEntered:
    消息调用的控制器,您是否确实将
    MainViewController
    指定为
    SuggestionViewController
    的代理。请举例说明一下好吗?My modal不支持UIViewController:
    // The method where you instantiate SuggestionViewController
    {
         // .. do your work
    
         SuggestionViewController *suggestionViewController = [[SuggestionViewController alloc] init];
    
         suggestionViewController.delegate = self; // <- This is the missing line
    
         [self presentModalViewController:suggestionViewController animated:YES];
         // [suggestionViewController release]; suggestionViewController = nil; // I'm assuming your using ARC
    
    }
    
    @interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>