从UITableViewController将数据推送到UIViewController

时间:2014-05-19 21:57:12

标签: ios iphone objective-c uitableview uiviewcontroller

我在UITableView上将数据从UIViewController推送到5个标签时遇到了一个非常大的问题。我有一个应用程序,你按下"攀登"根据您选择的攀爬方式,当您被推到UIViewController时,会出现不同的规格。现在它只是一个空白的屏幕,我的标签与我的班级挂钩,所有人都不确定如何让山雀将不同的信息传递给每个标签,具体取决于选择的攀爬方式。我现在将发布我的代码:

FourthTableViewController.h

#import <UIKit/UIKit.h>

@interface FourthTableViewController : UITableViewController

@property (nonatomic, strong) NSString *sectionName;

@property (nonatomic, strong) NSMutableArray *objects;
@property (nonatomic, strong) NSMutableArray *results;

@property (nonatomic, strong) IBOutlet UISearchBar *searchBar;

@end

FourthTableViewController.m

#import "FourthTableViewController.h"
#import "DetailViewController.h"


- (void)viewDidLoad
{
[super viewDidLoad];

UIBarButtonItem *NewBackButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:NewBackButton];

self.tableView.separatorColor = [UIColor colorWithRed:183/255.0 green:207/255.0 blue:85/255.0 alpha:0.5];

//populating arrays
NSDictionary *dict = @{/*Moss Rock Sections*/@"Grass Man":@[@"Tesseract", @"Chalky Dreams", 
@"Aristocratic Nose", @"Bee Stings", @"Recovery Run Traverse", @"Heel Shock", @"Fourth of July", 
@"No Sack", @"Poop Dreams", @"Hoop Dreams", @"Grass Man Traverse", @"Mikey Likes It", @"Just 
Throw", @"Rapture", @"Caveman Hunt", @"Batman Dyno", @"Waiting Line", @"Petrified Lady 
(project)", @"Up and Over", @"Rocker", @"Heart", @"Left Ventricle", @"Poison Ivey", @"Snake 
Eyes", @"Rattle", @"Shape Shifter", @"Road to Nowhere", @"Beef It", @"Switchback", @"Pine 
Phriend", @"Hanging Out", @"Warp Hole", @"Deet", @"Mosquito Bites"], @"Lost Roof": @[@"I'll 
Tumble for Ya", @"Snoopy's House", @"Treelee", @"Lost Roof Problem"], @"Ozzy":@[@"Clark Direct", 
@"Long Shot", @"Pile", @"Revolver", @"The Bomb", @"Suntoucher", @"A Young One", 
@"Nebuchadnezzar's Dream", @"Profanity", @"Godfather", @"Re-Ignition", @"Rancid", @"Tree 
Problem", @"Scrutinzier", @"Little Big Horn", @"Cheese is Good", @"Maybe Later", @"Double 
Clutch", @"A.X Variation", @"Salute", @"Ozzy Crack", @"Adam's Slab", @"Orange Streak", @"Tommy 
H", @"Vast Understatement", @"Super Crimp", @"Big Ben", @"Fat Cut", @"It Burns!", @"The Trip", 
@"Tesseract"]/*End Moss Rock Sections*/, @"Colorado Section 1":@[@"Colorado Climb 1", @"Colorado 
Climb 2", @"Colorado Climb 3"]};

climbs = dict[self.sectionName];

[self.objects addObjectsFromArray:climbs];
}

DetailViewController.h

#import <UIKit/UIKit.h>

@interface DetailViewController : UIViewController

@property (strong, nonatomic) NSString *detailLabelContents;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;

@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *gradeLabel;
@property (weak, nonatomic) IBOutlet UILabel *coordinatesLabel;
@property (weak, nonatomic) IBOutlet UILabel *rockLabel;
@property (weak, nonatomic) IBOutlet UILabel *difficultyLabel;


@property (nonatomic, strong) NSString *climbName;

@end

DetailViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];

UIBarButtonItem *NewBackButton = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStyleBordered target:nil action:nil];
[[self navigationItem] setBackBarButtonItem:NewBackButton];

//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;
}

总而言之,我希望当选择攀爬时,攀爬规格会出现在UIViewController上的5个标签中,当选择另一个攀爬规格时,攀爬规格。 (我假设它将是一个巨大的if / else语句。)

任何建议或建设性的批评都会受到高度赞赏,我知道我的语法并不完美,我对这门语言很陌生。

感谢。

1 个答案:

答案 0 :(得分:1)

要设置标签文字,您必须将NSString传递给下一个view controller,然后在viewDidLoad中进行设置。

因此,请制作与您的标签相对应的5个属性,并像使用climbName NSString一样(在您的prepareForSegue方法中)传递数据。

要设置标签文字,请在viewDidLoad

destination view controller中执行此操作
self.titleLabel.text = self.myStringThatIPassed;