2012-01-02 17 views
1

我想实现一个委托来启用一个模式视图来将数据传回给UIViewController。实现一个委托来启用模式视图将数据传回UIViewController

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

我的主控制器

它包含一个方法被调用时,模式具有用户的价值:

MainViewController。^h

- (void)translationTextEntered:(NSString *)txt; 

MainViewController.m

- (void)translationTextEntered:(NSString *)text 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
    _text.text = [NSString stringWithFormat:@"%@" , text]; 
} 

我的模态控制器

这包含其中包含委托和,选择了一个项目时一个UITableView,应当触发委托回调。

SuggestionViewController.h

@protocol SelectTranslationDelegate <NSObject> 
- (void)translationTextEntered:(NSString *)text; 
@end 

@interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, SelectTranslationDelegate> 
{ 
    id<SelectTranslationDelegate> delegate; 
} 

@property (nonatomic, weak)id delegate; 

SuggestionViewController.h

@synthesize delegate = _delegate; 

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    ...  
    [delegate translationTextEntered:@"f"]; 

} 
+1

你真的把'MainViewController'作为委托给'SuggestionViewViewController'吗? – 2012-01-02 04:02:38

+0

嗨保罗。你能举一个例子来展开这个吗?我的模态UIViewController: Nick 2012-01-02 13:02:15

回答

2

应该是这样的:

MainViewController.h

#import "SuggestionViewController.h" 

@interface MainViewController : UIViewController <SelectTranslationDelegate> 

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

- (void)translationTextEntered:(NSString *)txt;的声明不是必需的,因为你说你符合SelectTranslationDelegate协议(位于之间的位/>

MainViewController。米

// 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 

} 

还应当指出的是,你的模式视图控制器不应当符合SelectTranslationDelegate,因为这是最有可能不是你的意图。所以,你应该申报如:

@interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> 

这是要到translationTextEntered:SuggestionViewController回应MainViewControllerSuggestionViewController是那个让translationTextEntered:的消息调用delegate

0

在viewDidLoad中或查看WillAppear您的模态视图控制器包含了句...

  1. 创建主视图controoller的对象......鉴于DidLoad ...

    mainViewController * mainVC = [[mainViewController页头] initwithnobname] ...

  2. 然后设置句子

    self.delegate = mainVC;

这是你需要的待办事项的东西...

相关问题