2017-02-02 7 views
0

これは私の最初のアプリです。基本的に、この部分はUItableViewから2番目のView Controllにデータを渡します。単純なNSArray(UITableでも可)からデータを渡す方法を学びましたが、私の目標はNSDictionaryの値を渡すことです。すべてがセットアップされていますが、PrepareForSegueメソッドを正しく記述する方法がわかりません。アプリケーションは実行されますが、 "DetailView"のラベルは空のままです。私がこれまでに得たもの:Objective-c:prepareForSegueでUITableからViewControllerにデータを渡します。

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

誰かが私を助けることができますでる?おかげ

+0

'destViewController.receiver = [citySpots objectAtIndex:indexPath.row];' => 'destViewController.spot = [citySpots objectAtIndex:indexPath.row];'? – Larme

+0

いいえ、動作しません、Larme、ラベルはまだ空です... – FuManchu

+0

'viewDidLoad'では' _receiver'' nil'ですか? '_spot'は正しい値を持っていますか? – Larme

答えて

0

セッターを使用してみてください:

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

アレックスのおかげで感謝した。 ..ルーキーミス:)問題は解決されている(と私は昨日、それはコメントwasnt保存されたようだと思った...それについて申し訳ありません – FuManchu

関連する問題