簡體   English   中英

Objective-C:使用prepareForSegue將數據從UITable傳遞到ViewController

[英]Objective-c: passing data from UITable to ViewController with prepareForSegue

這是我的第一個應用程序,基本上,這部分包括將數據從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;
}

有人可以幫我嗎? 謝謝

嘗試使用setter:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM