[英]Why is my delegate not passing data back after selecting row and dismissing view?
我在MainViewController上有一個文本字段,我想從我的TableViewController傳遞一個字符串。 特別是當我選擇一個單元格(didSelectRowatIndexPath)時,我想獲取該indexpath.row的文本並解除TableViewController,將該字符串傳遞給MainViewController上的文本字段。 我試圖創建一個委托以使其工作,但它在調試窗口中說的是正確的字符串正在傳遞但從未出現在文本字段中...這是我的代碼顯示委托所需的一切。
我的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從不顯示任何內容,因此它不會連接到委托或其他東西。
所以有些事情顯然是錯的,但我不確定是什么。 我使用此stackoverflow答案作為參考,但即使這樣也無法使其工作(請參閱傳遞數據返回部分) 在視圖控制器之間傳遞數據
另外作為旁注,我正在以編程方式編寫所有內容。 感謝任何幫助,謝謝。
這就是我創建文本域的方式......
//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];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.