【发布时间】:2013-11-28 05:19:49
【问题描述】:
我知道已经有很多关于此的问题,但没有一个对我有帮助。我有两个 UITableViewControllers。我想将 QuicklistViewController(使用它像一个快速菜单来列出不同的节目)推送到 CurrentShowViewController。
我现在有一个仅包含 name 属性的 Show 模型。我无法将数据从我在 QuicklistViewController 中的选择移动到 CurrentShowViewController,以便我可以在那里显示 currentShow 信息。
我完全不知道该怎么做。请帮忙!
这是我的 CurrentShowViewController:
#import "CurrentShowViewController.h"
#import "QuicklistViewController.h"
@interface CurrentShowViewController ()
@end
@implementation CurrentShowViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.tabBarItem.image = [UIImage imageNamed:@"tab_icon_episodes.png"];
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//self.navigationItem.rightBarButtonItem = self.editButtonItem;
// Add a UIBarButton button that will display a Quicklist Modal View
UIBarButtonItem *quicklistButton = [[UIBarButtonItem alloc] initWithTitle:@"Quicklist"
style:UIBarButtonItemStylePlain
target:self
action:@selector(quicklistButtonPressed)];
self.navigationItem.leftBarButtonItem = quicklistButton;
self.currentShow = [[Show alloc] init];
NSLog(@"Current show name is: %@", self.currentShow.name);
self.title = self.currentShow.name;
}
- (void) viewWillAppear:(BOOL)animated {
self.title = self.currentShow.name;
}
- (void) quicklistButtonPressed {
QuicklistViewController *quicklistVC = [[QuicklistViewController alloc] init];
[self.navigationController pushViewController:quicklistVC animated:YES];
}
这是我的 QuicklistViewController:
#import "QuicklistViewController.h"
#import "CurrentShowViewController.h"
@interface QuicklistViewController ()
@end
@implementation QuicklistViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
self.title = @"Quicklist";
self.tabBarItem.image = [UIImage imageNamed:@"tab_icon_quicklist.png"];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain
target:self
action:@selector(backButtonPressed)];
self.navigationItem.leftBarButtonItem = backButton;
// Create a temp array of Show objects filled with test data
NSMutableArray *temp= [[NSMutableArray alloc] init];
[temp addObject:[[Show alloc] initWithName:@"The Walking Dead"]];
[temp addObject:[[Show alloc] initWithName:@"How I Met Your Mother"]];
[temp addObject:[[Show alloc] initWithName:@"Grey's Anatomy"]];
[temp addObject:[[Show alloc] initWithName:@"The Mentalist"]];
[temp addObject:[[Show alloc] initWithName:@"Stargate SG1"]];
NSArray *testShows = [[NSArray alloc] initWithArray:temp];
self.shows = testShows;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
self.currentShow = [[Show alloc] init];
}
- (void) backButtonPressed {
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [self.shows count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"Cell"];
}
cell.textLabel.text = [self.shows[indexPath.row] name];
return cell;
}
- (void) tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:YES];
// Change the current show to the selected show
self.currentShow = self.shows[indexPath.row];
//Archive changes to currentShow object
[Show saveShow:self.currentShow];
}
【问题讨论】:
标签: ios objective-c uitableview xcode5