1

I have a textfield on my MainViewController that I'd like to pass a string into from my TableViewController. Specifically when I select a cell (didSelectRowatIndexPath) I'd like to take the text for that indexpath.row and dismiss the TableViewController passing the string into the textfield on my MainViewController. I have attempted to create a delegate to get this to work but all it says in the debugging window is that the correct string is passing but never appears in the textfield... Here is my code showing everything necessary for the delegation.

My TableViewController.h where the delegate is declared...

@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

Then my TableViewController.m file...

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

}

Now here is my MainViewController.h file, this is where my textfield resides, and how I implement the delegate into this file...

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

@property(strong,nonatomic)UITextField *morseTextfield;

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

@end

Now the MainViewController.m file...

- (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);
}

The textField NSLog never displays anything, so its not connecting to the delegate or something.

So something is clearly wrong but I'm not sure what. I used this stackoverflow answer as a reference but even then couldn't get it to work (refer to the passing data back section) Passing Data between View Controllers

Also as a side note I'm coding everything programmatically. Any help is appreciated, thank you.

This is how i created the textfield...

//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];
Community
  • 1
  • 1
Jumpman987
  • 307
  • 1
  • 5
  • 17

1 Answers1

0

Possibly you set delegate to one instance of TableViewController and display another one.

- (void)viewDidLoad {
    [super viewDidLoad];

    TableViewController *tvc = [TableViewController new];
    tvc.selectedDataDelegate = self;

}

in your code tvc will be just released from memory and you delegate will not work.

Also in you .h file this row is useless.

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

In your MainViewController update next method. You have to set delegate in it

- (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];

}

  • I just found this out too, i set a breakpoint on the delegate and it said everything was nil. After removing it and trying to pass the data again the message still displays but nothing is getting passed to the textfield. Have any other ideas what might be going on? – Jumpman987 Apr 05 '17 at 04:57
  • @MatthewGuest could you show full MainViewController code? Maybe a link to git. In this case it will be easier to understand what's going wrong. Any way you should check that delegate method is called in MainViewController and text not nil, then check that morseTextfield points to proper object – Evgeniy Gushchin Apr 05 '17 at 05:04
  • sure thing one second – Jumpman987 Apr 05 '17 at 05:13
  • here is a link https://github.com/Guestman360/iMorse-App/blob/master/iMorse/MainViewController.m – Jumpman987 Apr 05 '17 at 05:19
  • Wow amazing, it actually worked, thank you! Could you explain to me why this works so I know for future reference? – Jumpman987 Apr 05 '17 at 05:43
  • Is is simple. In viewDidLoad you init one instance of MainViewController that was never used and set self as delegate to it. In tableViewBtnPressed method you init new instance of MainViewController that was displayed on screen. So your had to set delegate to correct instance of MainViewController. – Evgeniy Gushchin Apr 05 '17 at 05:47