Objective-c:使用prepareForSegue将数据从UITable传递到ViewController



这是我的第一个应用程序,基本上,这部分包括将数据从UItableView传递到第二个View Controll。我设法学会了如何从简单的 NSarray(也在 UITable 中)传递数据,但我的目标是从 NSDictionary 传递值。一切都设置好了,但是我不知道如何正确编写PrepareForSegue方法。应用将运行,但"详细信息视图"上的标签保持为空。到目前为止我得到了什么:

@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:)];
    }

准备方法:

- (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 setReceiver:[citySpots objectAtIndex:indexPath.row]];
[destViewController setSpot:[citySpots objectAtIndex:indexPath.row]];

最新更新