gpt4 book ai didi

objective-c - 从主视图更改 subview 上的 UILabel 文本

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:52:44 26 4
gpt4 key购买 nike

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

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

除了在 PlayerViewController 的 View 上设置标签文本外,所有这些都很好用。我不确定我做错了什么。我已使用 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,由模板设置,我没有更改它)。

将 PlayerViewController 的播放器属性设置为对象 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

我确定我只是遗漏了一些微不足道的东西,但我想不通,而且在网上搜索也找不到任何答案。

感谢您的帮助!

最佳答案

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

关于objective-c - 从主视图更改 subview 上的 UILabel 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4186233/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com