好的,所以我是一个不熟悉Objective-C / iOS程序的菜鸟,所以希望这里有更多知识的人可以帮助我。
我有一个使用SplitViewController模板(带有Core Data)的iPad应用程序。我创建了另一个名为PlayerViewController的UIViewController(带有xib文件)。该视图上有几个UILabel组件。

我有一个显示在RootViewController(UITableView)中的播放器列表,当您选择一个播放器时,我以编程方式创建了一个PlayerViewController(在DetailViewController中),将传递给DetailViewController的NSManagedObject传递给它,尝试设置一个文本标签上的PlayerViewController的视图,然后将其作为子视图添加到DetailViewController。

除了在PlayerViewController的视图上设置标签的文本外,所有这些都很好用。我不确定自己在做什么错。我已经使用NSLog来确认NSManagedObject不为nil,并且我要使用的NSManagedObject属性具有正确的文本。

我在这里茫然。任何帮助将不胜感激。 (代码如下):

此方法在DetailViewController.m文件中:

- (void)configureView {
    // Update the user interface for the detail item.
    PlayerViewController *player = [[PlayerViewController alloc] init];
    player.player = detailItem;
    [self.view addSubview:player.view];
}

当用户从RootViewController中选择一个项目时,将调用此方法(此功能,称为configureView,是由模板设置的,而我尚未更改)。

在该类的setPlayer方法中处理将PlayerViewController的player属性设置为对象detailItem的操作。
- (void)setPlayer:(NSManagedObject *)managedObject {

    if (player != managedObject) {
        [player release];
        player = [managedObject retain];

        // Update the view.
        [self configureView];
    }
}

然后,我在PlayerViewController中也有一个configureView方法,该方法设置标签的文本:
- (void)configureView {
    nickName.text = [[player valueForKey:@"Nickname"] description];
    NSLog(@"Nickname %@", [[player valueForKey:@"Nickname"] description]);
    NSLog(@"Nickname %@", nickName.text);
}

好的,因此第一个NSLog语句将输出所需的值,但是UILabel的文本(称为nickName)返回nil。

以下是完整的PlayerViewController.h和.m文件:

PlayerViewController.h:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>


@interface PlayerViewController : UIViewController {

    NSManagedObject *player;

    IBOutlet UILabel *nickName;
    IBOutlet UILabel *goalCount;
    IBOutlet UILabel *assistCount;
    IBOutlet UILabel *timeInGame;
}

@property (nonatomic, retain) IBOutlet UILabel *nickName;
@property (nonatomic, retain) IBOutlet UILabel *goalCount;
@property (nonatomic, retain) IBOutlet UILabel *assistCount;
@property (nonatomic, retain) IBOutlet UILabel *timeInGame;

@property (nonatomic, retain) NSManagedObject *player;

@end

PlayerViewController.m:
#import "PlayerViewController.h"


@implementation PlayerViewController

@synthesize nickName, goalCount, assistCount, timeInGame, player;

#pragma mark -
#pragma mark Managing the detail item

/*
 When setting the player item, update the view
 */
- (void)setPlayer:(NSManagedObject *)managedObject {

    if (player != managedObject) {
        [player release];
        player = [managedObject retain];

        // Update the view.
        [self configureView];
    }
}

- (void)configureView {
    nickName.text = [[player valueForKey:@"Nickname"] description];
    NSLog(@"Nickname %@", [[player valueForKey:@"Nickname"] description]);
    NSLog(@"Nickname %@", nickName.text);
}

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
}
*/



- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end

我确定我只是缺少一些琐碎的东西,但我无法弄清楚,也找不到在网上搜索的任何答案。

谢谢你的帮助!

最佳答案

好吧,所以在玩了一段时间并四处搜寻之后,我已经找到了解决我问题的答案。事实证明,除了一条语句的位置以外,我所有的代码都很好。我在PlayerViewController.m中对configureView的调用需要在viewDidLoad()中,而不是在setPlayer()方法中。现在一切都很好。

10-04 19:42