1

我正在尝试制作一个在主视图上显示声音列表的 Master-Detail 应用程序。当您点击声音时,您会转到详细信息屏幕,它会显示您选择的声音作为标签,然后显示声音的短片。我的问题是,所有声音都显示为列表中第一个声音的名称的标签。当我点击声音 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
4

1 回答 1

0

您只是设置...所以它只被调用一次(当视图控制器的视图第一次加载时)soundLabel.text = soundNameviewDidLoad这个应用程序很可能重用相同的实例,slfSoundDetailViewController因此它不会重新加载其视图。

您可能想要覆盖其中一个-[slfSoundDetailViewController setSoundName:]-[slfSoundDetailViewController viewWillAppea:]并在soundLabel.text那里设置。

于 2012-12-14T02:58:23.690 回答