-2

I am trying to send data back from one view controller to another. I am doing this based off another Q & A in stack overflow but I still have not go the hang of it.

LocationViewController.h

#import "ViewController.h"
@class LocationViewController;

@protocol LocationViewControllerDelegate <NSObject>
- (void)addItemViewController:(LocationViewController *)controller didFinishEnteringItem:(NSString *)item;
@end


@interface LocationViewController : UIViewController


@property (nonatomic, strong) UILabel *locationLabel;
@property (nonatomic, strong) UITextField *locationField;
@property (nonatomic, strong) NSString *fieldText;
@property (nonatomic, weak) id <LocationViewControllerDelegate> delegate;

- (instancetype) init;

-(void)saveButtonPressed;


@end

LocationViewController.m

#import "LocationViewController.h"
#import "SetScoringTableViewController.h"
#import "GameDetailsTableViewController.h"

@interface LocationViewController ()

@end

@implementation LocationViewController

@synthesize locationField;
@synthesize locationLabel;
@synthesize fieldText;

- (void)viewDidLoad {
     [super viewDidLoad];

    [self.view setBackgroundColor:[UIColor whiteColor]];

     //label

     locationLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 100, 1000, 20)];
    [locationLabel setText: @"Location:"];
    [self.view addSubview:locationLabel];

    //text field

   locationField = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, 300, 35)];

     [locationField setBorderStyle: UITextBorderStyleRoundedRect];
     locationField.text = @"Enter Location Here";

    [self.view addSubview:locationField];

     //save button

    UIBarButtonItem *saveButton = [[UIBarButtonItem alloc] initWithTitle: @"Save" style: UIBarButtonItemStylePlain target: self action: selector(saveButtonPressed)];
  self.navigationItem.rightBarButtonItem = saveButton;
}

-(void)saveButtonPressed {

    locationField.text = locationField.text;
    [self.delegate addItemViewController:self didFinishEnteringItem:locationField.text];
    NSLog(@"%@", locationField.text);

    [self.navigationController popViewControllerAnimated:YES ];
  [self performSelector:@selector(saveButtonPressed) withObject:nil afterDelay:0.25];
 GameDetailsTabelViewController *gameDetails = [[GameDetailsTableViewController alloc] init];
 [gameDetails.tableView reloadData]
}

GameDetailsTableViewController.m:

   -(void)addItemViewController:(LocationViewController *)controller didFinishEnteringItem:(NSString *)item {

    LocationViewController *locationView = [[LocationViewController alloc]initWithNibName:@"LocationViewController" bundle:nil];
    locationView.delegate = self;
    [[self navigationController]pushViewController:locationView animated:YES];

    item = fieldText;
}

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {



        static NSString *CellIdentifer1 = @"GameDetailsLocationCell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifer1];

         label = (UILabel *)[cell viewWithTag:0];

         label.text = [NSString stringWithFormat: @"%@", fieldText];

         return cell;

    }
}

When I run this I get null for the item. I would like to know why I am getting null on the item which is preventing my from further usage of this program.

av993
  • 73
  • 1
  • 12
  • Hmm, why do you have the `addItemViewController:didFinishEnteringItem:` method implemented twice in `GameDetailsTableViewController.m`? – nikolovski Sep 01 '15 at 20:13
  • Further to what Marko said, that code shouldn't compile, Xcode will notice you have didFinishEnteringItem twice. That begs the question, how can you be having problems when your code shouldn't even compile? What have you posted versus what is your actual code? – Gruntcakes Sep 01 '15 at 20:25
  • I have posted the exact same code as have. It does compile though. – av993 Sep 01 '15 at 20:39
  • I'm assuming your second implementation of `addItemViewController:didFinishEnteringItem:` is supposed to be `tableView:cellForRowAtIndexPath:`? There is no way what you have typed up there would compile. – dan Sep 01 '15 at 21:05
  • oh yes your right i meant to post that. I will change it right away – av993 Sep 01 '15 at 21:11
  • It still won't compile. `cellForRowAtIndexPath` isn't returning anything when the section's not 2 or the row isn't 0. – NRitH Sep 01 '15 at 21:25
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) – Hot Licks Sep 01 '15 at 22:09

1 Answers1

0

I am guessing that you wanna send some info from LocationVC to GameDetailVC.

In that case

LocationViewController.m

 -(void)saveButtonPressed {
        if([self.delegate respondsToSelector:@selector(addItemViewController:didFinishEnteringItem:)]{
            [self.delegate addItemViewController:self didFinishEnteringItem:locationField.text]; 
        }

        [self.navigationController popViewControllerAnimated:YES ];
}

GameDetailsTableViewController.m:

-(void)addItemViewController:(LocationViewController *)controller didFinishEnteringItem:(NSString *)item {

   //do something with item?

   //reload table data?
   [self.tableview reloadData];

}

Hope it helps. check for errors.

Lucho
  • 1,024
  • 1
  • 15
  • 24
  • I believe there is something else missing for this answer to work, where is `locationView.delegate = self` "When I run this I get null" stated in the question description – ErickES7 Sep 02 '15 at 17:19
  • `locationView.delegate = self` should be setted when you create and push the LocationViewController. – Lucho Sep 02 '15 at 19:23
  • Right, okay just something to mention bc I've noticed `locationView.delegate` is set in the definition of the delegate method, which isn't correct, am I right?? – ErickES7 Sep 02 '15 at 19:33
  • yes, these two lines `LocationViewController *locationView = [[LocationViewController alloc]initWithNibName:@"LocationViewController" bundle:nil]; locationView.delegate = self;` don't have to be on the delegate callback method. Should be somewhere else. For example on the `- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender` before pushing **LocationViewController** – Lucho Sep 02 '15 at 20:57
  • Exactly :) @av993 I hope this helps :) – ErickES7 Sep 03 '15 at 00:17
  • Where exactly should I put the prepare for segue method? – av993 Sep 03 '15 at 00:30
  • The first view that presents the second, LocationVC – ErickES7 Sep 03 '15 at 00:40