为什么我的代表在选择行并解除视图后没有传回数据?

时间:2017-04-05 03:24:12

标签: ios objective-c uitableview delegates xcode8

我的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文件,这是我的textfield所在的位置,以及我如何将委托实现到此文件中...

@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答案作为参考,但即使这样也无法使它工作(请参阅传递数据返回部分) 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];

1 个答案:

答案 0 :(得分:0)

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

}