从主视图更改子视图上的 UILabel 文本

发布于 2024-10-03 06:44:26 字数 4375 浏览 4 评论 0原文

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

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

除了在 PlayerViewController 视图上设置标签文本之外,所有这些都非常有效。我不确定我做错了什么。我已使用 NSLog 来确认 NSManagedObject 不为零,并且我尝试使用的 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,是由模板设置的,我没有更改它)。

将 PlayerViewController 的player属性设置为对象detailItem是在该类的setPlayer方法中处理的。

- (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

我确信我只是错过了一些微不足道的东西,但我无法弄清楚,并且无法在网络上搜索找到任何答案。

感谢您的帮助!

Ok, so I'm a relative noob with Objective-C/iOS programming, so hopefully someone with more knowledge here can help me out.
I have an iPad application using the SplitViewController template (with Core Data). I created another UIViewController (with xib file) called PlayerViewController. This View has several UILabel components on it.

I have a list of players that show up in the RootViewController (UITableView) and when you select a player, I programmatically create a PlayerViewController (in DetailViewController), pass it the NSManagedObject that was passed to the DetailViewController, try to set the text of one of the labels on the PlayerViewController's view, and then add it as a subview to the DetailViewController.

All of this works great except for the setting the text of the label on the PlayerViewController's view. I'm not sure what I'm doing wrong. I have used NSLog to confirm that the NSManagedObject is not nil and that the NSManagedObject property I'm trying to use has the correct text.

I'm at a loss here. Any help would be greatly appreciated. (Code follows):

This method is in the DetailViewController.m file:

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

This method is called when the user selects an item from the RootViewController (This functionality, calling of configureView, is setup by the template and I haven't changed it).

Setting the player property of the PlayerViewController to object detailItem is handled in the setPlayer method of that class.

- (void)setPlayer:(NSManagedObject *)managedObject {

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

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

I then have a configureView method as well in PlayerViewController that sets the text of the label:

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

Ok, so the first NSLog statement prints the desired value, but the text of the UILabel (called nickName) returns nil.

The following is the full PlayerViewController.h & .m files:

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

I'm sure I'm just missing something trivial, but I can't figure it out, and haven't been able to find any answers searching the web.

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼藏柔 2024-10-10 06:44:26

好吧,在玩了一会儿并四处搜索之后,我得到了问题的答案。事实证明,除了一条语句的位置之外,我拥有的所有代码都很好。我在 PlayerViewController.m 中对 configureView 的调用需要在 viewDidLoad() 中,而不是在 setPlayer() 方法中。现在一切都很好。

Ok, so after playing with this for a bit and searching and searching around, I have gotten the answer to my problem. It turns out all the code I had was fine except the location of one statement. My call to configureView in PlayerViewController.m needed to be in viewDidLoad() not in the setPlayer() method. It all works great now.

最好是你 2024-10-10 06:44:26

将 configureView 方法更改为:

- (void)configureView {
    nickName.text = (NSString*)[player valueForKey:@"Nickname"];
} 

Change the configureView method to that :

- (void)configureView {
    nickName.text = (NSString*)[player valueForKey:@"Nickname"];
} 
<逆流佳人身旁 2024-10-10 06:44:26

是的,更好的调用方法的地方是

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configureView]; 
}
  • (void)setPlayer:(NSManagedObject *)managementObject 在加载 nib 文件之前调用。

Yes, better place to call method is

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configureView]; 
}
  • (void)setPlayer:(NSManagedObject *)managedObject called before your nib files loaded.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文