2012-01-02 19 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;

這是你需要的待辦事項的東西...

相關問題