Я пытаюсь отправить данные с одного контроллера представления на другой. Я делаю это на основе другого Q & A в переполнении стека, но я до сих пор не понимаю его.Попытка выяснить, как отправлять данные с одного контроллера представления на другой (с помощью делегатов)
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 на предмет, который мешает моему дальнейшему использованию этой программы.
Хмм, почему у вас есть метод addItemViewController: didFinishEnteringItem: ', реализованный дважды в' GameDetailsTableViewController.m'? –
В дополнение к тому, что Марко сказал, что код не должен компилироваться, Xcode заметит, что у вас естьFinishEnteringItem дважды. Это вызывает вопрос, как у вас могут возникать проблемы, когда ваш код даже не компилируется? Что вы опубликовали в сравнении с вашим фактическим кодом? – Gruntcakes
Я опубликовал тот же самый код, что и у. Однако он компилируется. – av993