【发布时间】:2017-02-02 16:52:10
【问题描述】:
这是我的第一个应用程序,基本上,这部分包括将数据从 UItableView 传递到第二个 View Controll。我设法学习了如何从一个简单的 NSarray(也在 UITable 中)传递数据,但我的目标是从 NSDictionary 传递值。一切都设置好了,但我不知道如何正确编写 PrepareForSegue 方法。应用程序运行,但“DetailView”上的标签保持为空。到目前为止我得到了什么:
@implementation TableViewController
- (void)viewDidLoad {
[super viewDidLoad];
_citySpots = @{@"Bars" : @[@"Hurricane", @"Black Swan", @"Texas"],
@"Clubs" : @[@"Electric Wizard", @"Offspring", @"The Tunnel"],
@"Restaurants" : @[@"Nando's", @"1/2 Burguer", @"Satellite"],
};
_sectionTitles = [[_citySpots allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
PrepareForSegue 方法:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:@"spotsDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
DetailViewController *destViewController = segue.destinationViewController;
NSString *sectionTitle = [_sectionTitles objectAtIndex:indexPath.section];
NSArray *citySpots = [_citySpots objectForKey:sectionTitle];
destViewController.receiver = [citySpots objectAtIndex:indexPath.row];
}
}
以及接收者(标头):
#import <UIKit/UIKit.h>
#import "TableViewController.h"
@interface DetailViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *receiver;
@property (nonatomic, strong) NSString *spot;
@end
主要:
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)viewDidLoad {
[super viewDidLoad];
_receiver.text =_spot;
}
有人可以帮帮我吗?谢谢
【问题讨论】:
-
destViewController.receiver = [citySpots objectAtIndex:indexPath.row];=>destViewController.spot = [citySpots objectAtIndex:indexPath.row];? -
不行,不行,拉美,标签还是空的……
-
但在
viewDidLoad中,是_receivernil?_spot的值是否正确? -
我刚刚更改了您的建议(接收器->现货)。 DetailViewController的viewDidLoad还是这样的:_receiver.text =_spot;
-
老实说,我不明白,一切似乎都很好,我只是无法追踪问题...有人有不同的看法吗?
标签: objective-c uitableview nsdictionary