Ios 为什么我的委托在选择行并取消视图后不返回数据?

Ios 为什么我的委托在选择行并取消视图后不返回数据?,ios,objective-c,uitableview,delegates,xcode8,Ios,Objective C,Uitableview,Delegates,Xcode8,我的MainViewController上有一个文本字段,我想将一个字符串从TableViewController传入该字段。特别是当我选择一个单元格(didSelectRowatIndexPath)时,我希望获取该indexath.row的文本,并关闭TableViewController,将字符串传递到我的MainViewController上的文本字段中。我试图创建一个委托来让它工作,但它在调试窗口中所说的只是正确的字符串正在传递,但从未出现在文本字段中。。。这是我的代码,显示了代表团所需

我的MainViewController上有一个文本字段,我想将一个字符串从TableViewController传入该字段。特别是当我选择一个单元格(didSelectRowatIndexPath)时,我希望获取该indexath.row的文本,并关闭TableViewController,将字符串传递到我的MainViewController上的文本字段中。我试图创建一个委托来让它工作,但它在调试窗口中所说的只是正确的字符串正在传递,但从未出现在文本字段中。。。这是我的代码,显示了代表团所需的一切

My 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
现在这是我的MainViewController.h文件,这是我的文本字段所在的位置,以及我如何将委托实现到这个文件中

@interface MainViewController : UIViewController<UITextFieldDelegate, CAAnimationDelegate, sendDataProtocol> //include protocol here

@property(strong,nonatomic)UITextField *morseTextfield;

- (void)sendDataToMain:(NSString*)text; //conform to protocol

@end
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];

}

您可能将委托设置为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];

}

调用tableView didSelect时,内存中是否有MainViewController对象?能否说明如何在主VC上创建和添加UITextField?当然,我是通过编程方式创建的,我更新代码调用tableView didSelect时内存中是否有MainViewController对象?能否说明如何在主VC上创建和添加UITextField?当然,我是以编程方式创建的,我更新了代码我也发现了这一点,我在代理上设置了一个断点,它说一切都为零。删除它并尝试再次传递数据后,仍会显示消息,但不会向文本字段传递任何内容。还有其他想法吗?@MatthewGuest你能展示完整的MainViewController代码吗?也许是git的链接。在这种情况下,更容易理解哪里出了问题。不管怎样,你都应该检查委托方法是否在MainViewController中被调用,文本是否为非零,然后检查morseTextfield是否指向正确的对象确保有一个链接,哇,太棒了,它确实有效,谢谢!你能给我解释一下为什么会这样吗?我也发现了这一点,我在代理上设置了一个断点,它说一切都是零。删除它并尝试再次传递数据后,仍会显示消息,但不会向文本字段传递任何内容。还有其他想法吗?@MatthewGuest你能展示完整的MainViewController代码吗?也许是git的链接。在这种情况下,更容易理解哪里出了问题。不管怎样,你都应该检查委托方法是否在MainViewController中被调用,文本是否为非零,然后检查morseTextfield是否指向正确的对象确保有一个链接,哇,太棒了,它确实有效,谢谢!你能给我解释一下为什么这样做,以便我将来参考吗?