0

伙计们,这已经让我头疼了好几个小时了.... :(

我有一组控制器,它们被加载到滚动视图中

我想将值分配给标签“类别”,但我无法执行此操作 - 值显示为 NULL ??

但是,我可以在 viewDidLoad 方法中对其进行硬编码?

我尝试了几种方法,但都没有奏效?...

我的 .h 文件看起来像这样......

@interface ReviewCategoryViewController : UIViewController {
    @public
    UILabel *CategoryTitle;
}

- (id)initWithCategory:(NSString *)category;
- (id)setCategory:(NSString *)category;

@property (strong, nonatomic) IBOutlet UILabel *CategoryTitle;

@end

我的实现看起来像这样......

- (id)setCategory:(NSString *)category
{
    [self.CategoryTitle setText:category];
    NSLog(@">>2. Category %@ %@", category, CategoryTitle.text);

    return self;
}

// load the view nib and initialize the pageNumber ivar
- (id)initWithCategory:(NSString *)category
{
    self = [super initWithNibName:@"ReviewCategoryViewController" bundle:nil];

    [self.CategoryTitle setText:category];
    NSLog(@">>1. Category %@ %@", category, CategoryTitle.text);

    return self;
}

我对该方法的调用如下所示...

    ReviewCategoryViewController *controller = [viewControllers objectAtIndex:cardNo];

    if ((NSNull *)controller == [NSNull null])
    {
        NSString *temp = [categories_array objectAtIndex:cardNo];

        controller = [[ReviewCategoryViewController alloc] initWithCategory:temp];

        [controller setCategory:temp];

        [viewControllers replaceObjectAtIndex:cardNo withObject:controller];
    }

我的日志显示以下内容????

2013-04-12 16:54:27.507 Review Writer[14455:11603] >>1. Category Adapability **(null)**
2013-04-12 16:54:27.509 Review Writer[14455:11603] >>2. Category Adapability **(null)**
4

2 回答 2

0
- (id)initWithCategory:(NSString *)category
{
    seidlf = [super initWithNibName:@"ReviewCategoryViewController" bundle:nil];

    [self.CategoryTitle setText:category];
    NSLog(@">>1. Category %@ %@", category, CategoryTitle.text);

    return self;
}

调用self = [super initWithNibName:@"ReviewCategoryViewController" bundle:nil];链接到您的插座后尚未建立。您只能在viewDidLoad方法中加载视图后使用您的插座;

于 2013-04-12T16:28:08.783 回答
0

您可以在全局变量中设置类别并在 didLoad 方法中设置标签文本:

- (id)initWithCategory:(NSString *)category
{
    self = [super initWithNibName:@"ReviewCategoryViewController" bundle:nil];

    // categoryStr is a global NSString var
    categoryStr = category;

    return self;
}

- (void)viewDidLoad
{
    [self.CategoryTitle setText:categoryStr];
}
于 2013-04-12T17:07:39.703 回答