试图弄清楚如何将数据从一个视图控制器发送到另一个视图控制器(使用委托)

时间:2015-09-01 20:09:01

标签: ios objective-c

我正在尝试将数据从一个视图控制器发送回另一个视图控制器。我这是基于另一个Q&堆栈溢出但我仍然没有掌握它。

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;

    }
}

当我运行此项时,我的项目为null。我想知道为什么我的项目无效,这阻止了我进一步使用这个程序。

1 个答案:

答案 0 :(得分: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.