Objective-c:使用prepareForSegue

时间:2017-02-02 16:52:11

标签: objective-c uitableview nsdictionary

这是我的第一个应用程序,基本上,这部分包括将数据从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;
}

有人可以帮帮我吗?感谢

1 个答案:

答案 0 :(得分:0)

尝试使用setter:

[destViewController setReceiver:[citySpots objectAtIndex:indexPath.row]];
[destViewController setSpot:[citySpots objectAtIndex:indexPath.row]];