【问题标题】:Objective C: Unable to Assign value to Labels目标 C:无法为标签赋值
【发布时间】:2011-04-20 17:13:53
【问题描述】:

我正在尝试访问存储在数组中的对象(人的名字)的属性,并将其分配给单独的视图控制器 (SplitMethodViewController) 中的标签。名称值在此处成功分配。代码 sn-p 如下:

在初始视图控制器中(在显示包含 UILabel 的模态视图控制器之前):

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    int row = [indexPath row];
    Person *thisPerson = (Person *)[self.personArray objectAtIndex:row];

    SplitMethodViewController *smvc = [[SplitMethodViewController alloc]initWithNibName:nil bundle:nil];
    smvc.nameLabel.text = [[NSString alloc] initWithFormat:@"%@", thisPerson.firstName];

    //This lines returns the value I want, showing that assignment is working till this point
    NSLog(@"The name label is %@", smvc.nameLabel.text);

    [self presentModalViewController:smvc animated:YES];

    [smvc release];

}

但是,当我签入 splitMethodViewController(签入 ViewDidLoad 方法)时,这些值变为空白

@interface SplitMethodViewController : UIViewController 
{
    UILabel *nameLabel;
}

@property (nonatomic, retain) IBOutlet UILabel *nameLabel;
@end

@implementation SplitMethodViewController

@synthesize nameLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    // Custom initialization.
    self.nameLabel = [[UILabel alloc] init];
    }
    return self;
}

- (id)init 
{
    return [self initWithNibName:nil bundle:nil];
}
- (void)viewDidLoad 
{
    //name label returning nothing here.
    NSLog(@"namelabel is %@",self.nameLabel.text);

    [super viewDidLoad];
}
@end

我确定我在某个地方犯了一些愚蠢的错误。我试过删除所有的网点和标签,然后只重新创建一个名称标签和网点。但我仍然遇到同样的问题。

任何帮助将不胜感激!

【问题讨论】:

    标签: objective-c uilabel variable-assignment


    【解决方案1】:

    在您实例化 SplitMethodViewController 后,您是否真的分配并实例化了 nameLabelevenBillAmountLabel?在 Objective-C 中,消息(方法调用)可以发送到 nil(不存在的对象)而不返回任何错误,也可以不返回任何结果。

    确保 SplitMethodViewController 上的 -init 方法看起来像这样:

    // this is the designated initializer of most view controllers, 
    // do initialization here ...
    - (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle 
    {
        self = [super initWithNibName:nibName bundle:nibBundle]; 
        if (self)
        {
            nameLabel = [[UILabel alloc] init];
            evenBillAmountLabel = [[UILabel alloc] init];
            // add other stuff you need to initialize ...
        }
        return self;
    }
    
    - (id)init 
    {
        // since we don't wanna re-implement allocation and instantiation for every 
        // initializer, we call the 'designated initializer' with some default values,
        // in this case the default nibName and bundle are nil.
        return [self initWithNibName:nil bundle:nil];
    }
    
    - (void)dealloc 
    {
        [nameLabel release];
        [evenBillAmountLabel release];
        [super dealloc];
    }
    

    如果这对您来说是新的并且与您的问题有关,请务必阅读有关指定初始化程序的信息。 Here's a link to Apple's documentation 关于这个主题。

    【讨论】:

    • 感谢您的建议。我跟着它,现在我已经为 nameLabel 分配了值。但奇怪的是标签仍然显示为空白。我重新创建了标签和商店,以确保我没有犯任何愚蠢的错误。我已经更新了原始帖子中的代码 sn-p。如果您能帮忙再看一遍,将不胜感激!再次感谢您的帮助
    • 你真的使用 nib 文件吗?通常我完全用代码创建我的 ViewControllers。如果使用 nib 文件,则可能必须更改指定的初始化程序以使用 nib。您可能还想在代码中设置标签的框架。带有 CGRectZero 框架(默认值)的标签不会显示单行文本。最后确保标签的 textColor 与背景形成对比。
    • 是的,我确实使用了 NIB 文件。我找到了解决问题的方法,我必须首先将名称的值作为字符串传递给我的 splitMethodViewController,然后将其直接设置为 splitViewController 中的标签。不知道为什么最初的方法不起作用。
    【解决方案2】:

    如果 Wolfgang 的回答没有解决问题,请确保您的 SplitMethodViewController.xib 文件中的 UILabel 引用已连接到您的 SplitMethodViewController.h 文件中的正确引用出口。

    【讨论】:

    • 是的,我怀疑我在 IB 中犯了一些错误。所以我删除了所有的出口和标签,只重新创建了 UILabel 作为名称,看看它是否有效,遗憾的是它仍然让我空白。我似乎无法找到问题....
    猜你喜欢
    • 2014-05-13
    • 1970-01-01
    • 2022-01-16
    • 2018-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多