无法将NNString标签名称从MasterView传递给DetailView

时间:2012-12-14 02:53:05

标签: ios xcode master-detail

我正在尝试创建一个Master-Detail应用程序,该应用程序在主视图上显示Sounds列表。当您点击声音时,您将转到详细信息屏幕,它会显示您选择作为标签的声音,然后显示声音的短片。我的问题是,所有声音都显示为标签列表中第一个声音的名称。当我点击声音B时,详细信息屏幕显示“声音B”。当我点击tSound F时,详细信息屏幕显示“Sound B。”

没有出现任何错误或警告。有什么想法吗?

提前致谢。

slfViewController.h

#import <UIKit/UIKit.h>

@interface slfViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *tableView;

@end

slfViewController.m

#import "slfViewController.h"
#import "slfSoundDetailViewController.h"

@interface slfViewController ()

@end

@implementation slfViewController 
{
    NSArray *tableData;
    NSArray *thumbnails;
}

@synthesize tableView = _tableView;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    tableData = [NSArray arrayWithObjects:
             @"B Sound", 
             @"Ch Sound", 
             @"D Sound", 
             @"F Sound", 
                ...
             @"Zh Sound",
             nil];

thumbnails = [NSArray arrayWithObjects:
              @"b.jpg", 
              @"ch.jpg", 
              @"d.jpg", 
              @"f.jpg", 
                ...
              @"zh.jpg",
              nil];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showSoundDetail"]) {
        NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
        slfSoundDetailViewController *destViewController = segue.destinationViewController;
        destViewController.soundName = [tableData objectAtIndex:indexPath.row];
    }
}

#pragma mark - TableView Data Source methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableData count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"soundCell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }

    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
    cell.imageView.image = [UIImage imageNamed:[thumbnails objectAtIndex:indexPath.row]];

    return cell;
}


@end

slfSoundDetailViewController.h

#import <UIKit/UIKit.h>

@interface slfSoundDetailViewController : UIViewController

@property (nonatomic, strong) IBOutlet UILabel *soundLabel;
@property (nonatomic, strong) NSString *soundName;

@end

slfSoundDetailViewController.m

#import "slfSoundDetailViewController.h"

@interface slfSoundDetailViewController ()

@end

@implementation slfSoundDetailViewController

@synthesize soundLabel;
@synthesize soundName;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    soundLabel.text = soundName;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

1 个答案:

答案 0 :(得分:0)

您只在soundLabel.text = soundName中设置viewDidLoad ...所以它只被调用一次(首次加载视图控制器的视图时)。此应用最有可能重复使用slfSoundDetailViewController的相同实例,因此不会重新加载其视图。

您可能希望覆盖-[slfSoundDetailViewController setSoundName:]-[slfSoundDetailViewController viewWillAppea:]并在那里设置soundLabel.text