我正在尝试从UITableViewController将数据推送到UIViewController。当用户按下爬升时,它将推送到UIViewController,并在我放置在UIViewController上的3个标签中显示3个不同的规格。它正在推动细节视图,但没有显示任何信息。我将发布我的代码:
FourthTableViewController.h
#import <UIKit/UIKit.h>
@interface FourthTableViewController : UITableViewController
@property (nonatomic, strong) NSString *sectionName;
@end
FourthTableViewController.m
#import "FourthTableViewController.h"
#import "DetailViewController.h"
@interface FourthTableViewController ()
@end
@implementation FourthTableViewController
{
NSArray *climbs;
}
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//populating arrays
NSDictionary *dict = @{@"Alabama Section 1":@[@"Alabama Climb 1", @"Alabama Climb 2", @"Alabama
Climb 3"], @"Georgia Section 1": @[@"Georgia Climb 1", @"Georgia Climb 2", @"Georgia Climb 3"],
@"Tennessee Section 1":@[@"Tennessee Climb 1", @"Tennessee Climb 2", @"Tennessee Climb 3"],
@"Colorado Section 1":@[@"Colorado Climb 1", @"Colorado Climb 2", @"Colorado Climb 3"]};
climbs = dict[self.sectionName];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return climbs.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = @"ClimbCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
//if cell doesn't have anything in it, creates a new one
if(cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = climbs[indexPath.row];
cell.textLabel.font = [UIFont fontWithName:@"Chalkduster" size:17];
return cell;
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"ShowDetails"])
{
DetailViewController *detailViewController = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
int row = [indexPath row];
}
}
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
{
IBOutlet UILabel *Type;
IBOutlet UILabel *Coordinates;
IBOutlet UILabel *Grade;
}
@property (strong, nonatomic) NSString *detailLabelContents;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (nonatomic, strong) NSString *climbName;
@end
DetailViewController.m
#import "DetailViewController.h"
#import "FourthTableViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
{
NSArray *specs;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
//populating arrays
NSDictionary *dict = @{@"Alabama Climb 1":@[@"Alabama Spec 1", @"Alabama Spec 2", @"Alabama Spec
3"], @"Georgia Climb 1": @[@"Georgia Spec 1", @"Georgia Spec 2", @"Georgia Spec 3"], @"Tennessee
Climb 1":@[@"Tennessee Spec 1", @"Tennessee Spec 2", @"Tennessee Spec 3"], @"Colorado Climb
1":@[@"Colorado Spec 1", @"Colorado Spec 2", @"Colorado Spec 3"]};
specs = dict[self.climbName];
self.detailLabel.text = self.detailLabelContents;
if ([_climbName isEqualToString:@"Alabama Climb 1"])
{
Type.text = @"Really Hard";
Coordinates.text = @"10.32145, -83.498621";
Grade.text = @"V10";
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我知道我的问题在于我的问题,但我不确定如何从if语句中获取信息以推送到3个标签的详细视图。
我不是在找人牵着我的手告诉我答案,只是一些适当的指导和建设性的批评。
由于
答案 0 :(得分:0)
如果要在目标视图控制器(您正在推动的控制器)中设置数据,请使用prepareForSegue
方法,如下所示:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"ShowDetails"]){
UIViewController *vc = [segue destinationViewController];
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
int row = [indexPath row];
vc.climbName = ...;
vc. detailLabelContents = ...;
}
}
您很可能必须将destinationViewController
强制转换为您正在使用的ViewController类,例如DetailViewController *vc = (DetailViewController*)[segue destinationViewController];
然后只需设置您想要存储数据的属性。使用NSIndexPath,您可以像在cellForRowAtIndexPath
中一样访问本地数据