实现委托以启用模态视图将数据传回UIViewController [英] Implementing a delegate to enable a modal view to pass data back to a 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>代表;
}
@property(nonatomic,weak)id delegate;
SuggestionViewController.h
@synthesize delegate = _delegate;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
...
[delegate translationTextEntered:@f ];
}
是这样的:
MainViewController.h
#importSuggestionViewController.h
@interface MainViewController:UIViewController< SelectTranslationDelegate>
// - (void)translationTextEntered:(NSString *)txt; < - 不需要
声明 - (void)translationTextEntered :( NSString *)txt;
不需要,因为你说你符合 SelectTranslationDelegate
协议( ;
/ >
)
MainViewController.m
//您实例化的方法SuggestionViewController
{
// ..做你的工作
SuggestionViewController * suggestionViewController = [[SuggestionViewController alloc] init];
suggestionViewController.delegate = self; //< - 这是缺失的行
[self presentModalViewController:suggestionViewController animated:YES];
// [suggestionViewController release]; suggestionViewController = nil; //我假设你使用ARC
}
它也应该请注意,您的Modal视图控制器不应符合 SelectTranslationDelegate
,因为这很可能不是您的意图。所以你的声明应该是这样的:
@interface SuggestionViewController:UIViewController< UITableViewDataSource,UITableViewDelegate>
您想要回复的是 MainViewController
到 translationTextEntered:
not SuggestionViewController
。 SuggestionViewController
是在委托中的消息调用
translationTextEntered:
code>
I am trying to implement a delegate to enable a modal view to pass data back to a UIViewController.
I have two view controllers, my main UIViewController and the modal. Using the code below, the [delegate translationTextEntered:@"Test"]; doesn't affect the main screen (i.e. 'translationTextEntered' never gets called)
My Main Controller
This contains a method to be called when the modal has the user's value:
MainViewController.h
- (void)translationTextEntered:(NSString *)txt;
MainViewController.m
- (void)translationTextEntered:(NSString *)text
{
[self dismissModalViewControllerAnimated:YES];
_text.text = [NSString stringWithFormat:@"%@" , text];
}
My Modal Controller
This contains a UITableView which contains the delegate and, when an item is selected, should trigger the delegate callback.
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"];
}
It should be something like this:
MainViewController.h
#import "SuggestionViewController.h"
@interface MainViewController : UIViewController <SelectTranslationDelegate>
// - (void)translationTextEntered:(NSString *)txt; <- Not required
The declaration of - (void)translationTextEntered:(NSString *)txt;
is not required because you say that you conform to the SelectTranslationDelegate
protocol (The bit between the <
/>
)
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
}
It should also be noted that your Modal view controller should not conform to SelectTranslationDelegate
as this is most likely not your intention. So you declaration should be like:
@interface SuggestionViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
It is MainViewController
that you want to respond to translationTextEntered:
not SuggestionViewController
. The SuggestionViewController
is that one that makes the message call of translationTextEntered:
on the delegate
这篇关于实现委托以启用模态视图将数据传回UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!