草庐IT

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

coder 2024-01-11 原文

好的,我是 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/

有关objective-c - 从主视图更改 subview 上的 UILabel 文本的更多相关文章

  1. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  2. ruby-on-rails - Ruby on Rails 迁移,将表更改为 MyISAM - 2

    如何正确创建Rails迁移,以便将表更改为MySQL中的MyISAM?目前是InnoDB。运行原始执行语句会更改表,但它不会更新db/schema.rb,因此当在测试环境中重新创建表时,它会返回到InnoDB并且我的全文搜索失败。我如何着手更改/添加迁移,以便将现有表修改为MyISAM并更新schema.rb,以便我的数据库和相应的测试数据库得到相应更新? 最佳答案 我没有找到执行此操作的好方法。您可以像有人建议的那样更改您的schema.rb,然后运行:rakedb:schema:load,但是,这将覆盖您的数据。我的做法是(假设

  3. ruby - 使用 ruby​​ 将 HTML 转换为纯文本并维护结构/格式 - 2

    我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h

  4. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  5. ruby-on-rails - 项目升级后 Pow 不会更改 ruby​​ 版本 - 2

    我在我的Rails项目中使用Pow和powifygem。现在我尝试升级我的ruby​​版本(从1.9.3到2.0.0,我使用RVM)当我切换ruby​​版本、安装所有gem依赖项时,我通过运行railss并访问localhost:3000确保该应用程序正常运行以前,我通过使用pow访问http://my_app.dev来浏览我的应用程序。升级后,由于错误Bundler::RubyVersionMismatch:YourRubyversionis1.9.3,butyourGemfilespecified2.0.0,此url不起作用我尝试过的:重新创建pow应用程序重启pow服务器更新战俘

  6. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  7. ruby - Capistrano 3 在任务中更改 ssh_options - 2

    我尝试使用不同的ssh_options在同一阶段运行capistranov.3任务。我的production.rb说:set:stage,:productionset:user,'deploy'set:ssh_options,{user:'deploy'}通过此配置,capistrano与用户deploy连接,这对于其余的任务是正确的。但是我需要将它连接到服务器中配置良好的an_other_user以完成一项特定任务。然后我的食谱说:...taskswithoriginaluser...task:my_task_with_an_other_userdoset:user,'an_othe

  8. ruby-on-rails - date_field_tag,如何设置默认日期? [ rails 上的 ruby ] - 2

    我想设置一个默认日期,例如实际日期,我该如何设置?还有如何在组合框中设置默认值顺便问一下,date_field_tag和date_field之间有什么区别? 最佳答案 试试这个:将默认日期作为第二个参数传递。youcorrectlysetthedefaultvalueofcomboboxasshowninyourquestion. 关于ruby-on-rails-date_field_tag,如何设置默认日期?[rails上的ruby],我们在StackOverflow上找到一个类似的问

  9. ruby-on-rails - openshift 上的 rails 控制台 - 2

    我将我的Rails应用程序部署到OpenShift,它运行良好,但我无法在生产服务器上运行“Rails控制台”。它给了我这个错误。我该如何解决这个问题?我尝试更新ruby​​gems,但它也给出了权限被拒绝的错误,我也无法做到。railsc错误:Warning:You'reusingRubygems1.8.24withSpring.UpgradetoatleastRubygems2.1.0andrun`gempristine--all`forbetterstartupperformance./opt/rh/ruby193/root/usr/share/rubygems/rubygems

  10. ruby-on-rails - 相关表上的范围为 "WHERE ... LIKE" - 2

    我正在尝试从Postgresql表(table1)中获取数据,该表由另一个相关表(property)的字段(table2)过滤。在纯SQL中,我会这样编写查询:SELECT*FROMtable1JOINtable2USING(table2_id)WHEREtable2.propertyLIKE'query%'这工作正常:scope:my_scope,->(query){includes(:table2).where("table2.property":query)}但我真正需要的是使用LIKE运算符进行过滤,而不是严格相等。然而,这是行不通的:scope:my_scope,->(que

随机推荐