为什么我的代表在选择行并关闭视图后不传回数据?
Why is my delegate not passing data back after selecting row and dismissing view?
我的 MainViewController 上有一个文本字段,我想将一个字符串从我的 TableViewController 传递给它。具体来说,当我 select 一个单元格 (didSelectRowatIndexPath) 时,我想获取该 indexpath.row 的文本并关闭 TableViewController 将字符串传递到我的 MainViewController 上的文本字段。我试图创建一个委托来让它工作,但它在调试 window 中所说的只是正确的字符串正在传递但从未出现在文本字段中......这是我的代码,显示了委托所需的一切.
我的 TableViewController.h 声明委托的地方...
@protocol sendDataProtocol <NSObject>
- (void)sendDataToMain:(NSString*)text;
@end
@interface TableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource> {
__weak id selectDataDelegate;
}
@property(nonatomic,weak)id<sendDataProtocol> selectedDataDelegate;
@property(strong,nonatomic)NSArray *presetList; //Holds the strings I want to pass
@end
然后我的 TableViewController.m 文件...
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize selectedDataDelegate;
-(void)viewDidLoad {
//http://morsecode.scphillips.com/morse.html
self.presetList = [NSArray arrayWithObjects:@"AS",
@"BCNU",
@"CL",
@"CT",
@"CUL",
@"K",
@"QSL",
@"QSL?",
@"QRX?",
@"QRV",
@"QRV?",
@"QTH",
@"QTH?",
@"R",
@"SN",
@"SOS",
@"73",
@"88",
nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.selectedDataDelegate sendDataToMain:self.presetList[indexPath.row]];
NSLog(@"Delegate says: %@", self.presetList[indexPath.row]);
//The NSLog does display the correct cell I pressed, but no data passes back
[self dismissViewControllerAnimated:YES completion:nil];
}
现在这是我的 MainViewController.h 文件,这是我的文本字段所在的位置,以及我如何在该文件中实现委托...
@interface MainViewController : UIViewController<UITextFieldDelegate, CAAnimationDelegate, sendDataProtocol> //include protocol here
@property(strong,nonatomic)UITextField *morseTextfield;
- (void)sendDataToMain:(NSString*)text; //conform to protocol
@end
现在 MainViewController.m 文件...
- (void)viewDidLoad {
[super viewDidLoad];
TableViewController *tvc = [TableViewController new];
tvc.selectedDataDelegate = self;
}
//Protocol method declared here
- (void)sendDataToMain:(NSString*)text {
NSString *str = text;
self.morseTextfield.text = str;
NSLog(@"text: %@",text);
}
textField NSLog 从不显示任何内容,因此它没有连接到委托或其他东西。
很明显有些地方出了问题,但我不确定是什么。我使用这个 Whosebug 答案作为参考,但即使那样也无法让它工作(参考传递数据部分)
Passing Data between View Controllers
另请注意,我正在以编程方式对所有内容进行编码。感谢任何帮助,谢谢。
这就是我创建文本字段的方式...
//CONFORMING TO DELEGATES
self.morseTextfield.delegate = self;
//CREATING AND ADDING TEXTFIELD TO VIEW
self.morseTextfield = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-300)/2,
(self.view.frame.size.height)/7, 300, 30.0)];
self.morseTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.morseTextfield.font = [UIFont fontWithName:@"Avenir Next" size:20];
self.morseTextfield.textAlignment = NSTextAlignmentCenter;
self.morseTextfield.placeholder = @"Translate text into morse code";
[self.morseTextfield addTarget:self action:@selector(dismissKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
self.morseTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
self.morseTextfield.spellCheckingType = UITextSpellCheckingTypeNo;
self.morseTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self.morseTextfield setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:self.morseTextfield];
可能您将委托设置为 TableViewController 的一个实例并显示另一个实例。
- (void)viewDidLoad {
[super viewDidLoad];
TableViewController *tvc = [TableViewController new];
tvc.selectedDataDelegate = self;
}
在您的代码中,tvc 将刚刚从内存中释放,您的委托将不起作用。
同样在你的 .h 文件中这一行是无用的。
- (void)sendDataToMain:(NSString*)text; //conform to protocol
在您的 MainViewController 中更新下一个方法。您必须在其中设置委托
- (void) tableViewBtnPressed:(UIBarButtonItem *)sender {
MCTableViewController *tableVC = [[MCTableViewController alloc] init];
tableVC.selectedDataDelegate = self;
UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:tableVC];
[self.navigationController presentViewController:navBar animated:YES completion:nil];
}
我的 MainViewController 上有一个文本字段,我想将一个字符串从我的 TableViewController 传递给它。具体来说,当我 select 一个单元格 (didSelectRowatIndexPath) 时,我想获取该 indexpath.row 的文本并关闭 TableViewController 将字符串传递到我的 MainViewController 上的文本字段。我试图创建一个委托来让它工作,但它在调试 window 中所说的只是正确的字符串正在传递但从未出现在文本字段中......这是我的代码,显示了委托所需的一切.
我的 TableViewController.h 声明委托的地方...
@protocol sendDataProtocol <NSObject>
- (void)sendDataToMain:(NSString*)text;
@end
@interface TableViewController : UITableViewController<UITableViewDelegate, UITableViewDataSource> {
__weak id selectDataDelegate;
}
@property(nonatomic,weak)id<sendDataProtocol> selectedDataDelegate;
@property(strong,nonatomic)NSArray *presetList; //Holds the strings I want to pass
@end
然后我的 TableViewController.m 文件...
@interface TableViewController ()
@end
@implementation TableViewController
@synthesize selectedDataDelegate;
-(void)viewDidLoad {
//http://morsecode.scphillips.com/morse.html
self.presetList = [NSArray arrayWithObjects:@"AS",
@"BCNU",
@"CL",
@"CT",
@"CUL",
@"K",
@"QSL",
@"QSL?",
@"QRX?",
@"QRV",
@"QRV?",
@"QTH",
@"QTH?",
@"R",
@"SN",
@"SOS",
@"73",
@"88",
nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.selectedDataDelegate sendDataToMain:self.presetList[indexPath.row]];
NSLog(@"Delegate says: %@", self.presetList[indexPath.row]);
//The NSLog does display the correct cell I pressed, but no data passes back
[self dismissViewControllerAnimated:YES completion:nil];
}
现在这是我的 MainViewController.h 文件,这是我的文本字段所在的位置,以及我如何在该文件中实现委托...
@interface MainViewController : UIViewController<UITextFieldDelegate, CAAnimationDelegate, sendDataProtocol> //include protocol here
@property(strong,nonatomic)UITextField *morseTextfield;
- (void)sendDataToMain:(NSString*)text; //conform to protocol
@end
现在 MainViewController.m 文件...
- (void)viewDidLoad {
[super viewDidLoad];
TableViewController *tvc = [TableViewController new];
tvc.selectedDataDelegate = self;
}
//Protocol method declared here
- (void)sendDataToMain:(NSString*)text {
NSString *str = text;
self.morseTextfield.text = str;
NSLog(@"text: %@",text);
}
textField NSLog 从不显示任何内容,因此它没有连接到委托或其他东西。
很明显有些地方出了问题,但我不确定是什么。我使用这个 Whosebug 答案作为参考,但即使那样也无法让它工作(参考传递数据部分) Passing Data between View Controllers
另请注意,我正在以编程方式对所有内容进行编码。感谢任何帮助,谢谢。
这就是我创建文本字段的方式...
//CONFORMING TO DELEGATES
self.morseTextfield.delegate = self;
//CREATING AND ADDING TEXTFIELD TO VIEW
self.morseTextfield = [[UITextField alloc]initWithFrame:CGRectMake((self.view.frame.size.width-300)/2,
(self.view.frame.size.height)/7, 300, 30.0)];
self.morseTextfield.borderStyle = UITextBorderStyleRoundedRect;
self.morseTextfield.font = [UIFont fontWithName:@"Avenir Next" size:20];
self.morseTextfield.textAlignment = NSTextAlignmentCenter;
self.morseTextfield.placeholder = @"Translate text into morse code";
[self.morseTextfield addTarget:self action:@selector(dismissKeyboard) forControlEvents:UIControlEventEditingDidEndOnExit];
self.morseTextfield.autocorrectionType = UITextAutocorrectionTypeNo;
self.morseTextfield.spellCheckingType = UITextSpellCheckingTypeNo;
self.morseTextfield.autocapitalizationType = UITextAutocapitalizationTypeNone;
[self.morseTextfield setReturnKeyType:UIReturnKeyDone];
[self.view addSubview:self.morseTextfield];
可能您将委托设置为 TableViewController 的一个实例并显示另一个实例。
- (void)viewDidLoad {
[super viewDidLoad];
TableViewController *tvc = [TableViewController new];
tvc.selectedDataDelegate = self;
}
在您的代码中,tvc 将刚刚从内存中释放,您的委托将不起作用。
同样在你的 .h 文件中这一行是无用的。
- (void)sendDataToMain:(NSString*)text; //conform to protocol
在您的 MainViewController 中更新下一个方法。您必须在其中设置委托
- (void) tableViewBtnPressed:(UIBarButtonItem *)sender {
MCTableViewController *tableVC = [[MCTableViewController alloc] init];
tableVC.selectedDataDelegate = self;
UINavigationController *navBar = [[UINavigationController alloc]initWithRootViewController:tableVC];
[self.navigationController presentViewController:navBar animated:YES completion:nil];
}