【问题标题】:Objective-c: passing data from UITable to ViewController with prepareForSegueObjective-c:使用 prepareForSegue 将数据从 UITable 传递到 ViewController
【发布时间】: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 中,是_receiver nil_spot 的值是否正确?
  • 我刚刚更改了您的建议(接收器->现货)。 DetailViewController的viewDidLoad还是这样的:_receiver.text =_spot;
  • 老实说,我不明白,一切似乎都很好,我只是无法追踪问题...有人有不同的看法吗?

标签: objective-c uitableview nsdictionary


【解决方案1】:

尝试使用设置器:

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

【讨论】:

  • 感谢 Alex 的回答,但我设法弄清楚:segue 没有标识符...新秀错误 :) 问题已解决(我以为我昨天评论过,看来评论没有保存...很抱歉
猜你喜欢
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多