【问题标题】:Can't set a label in a view无法在视图中设置标签
【发布时间】:2012-10-17 18:02:21
【问题描述】:

我有以下情况。我有一个视图控制器(ContentController),顶部有 3 个按钮。下面我添加了第二个视图(contentView),这个视图应该显示这 3 个按钮后面的内容。因此,每次我按下按钮时,contentView 都会发生变化。所以我所做的是创建了三个加法控制器。

FirstController.m、FirstController.h、FirstController.xib

SecondController.m、SecondController.h、SecondController.xib、

ThirdController.m、ThirdController.h、ThirdController.xib、

在我的 ContentController.m 中,我添加了 3 个处理更改的 IBAction。

@interface ViewController ()

@property (nonatomic,strong) UIViewController *nextController;
@end

@implementation ViewController
@synthesize nextController = _nextController;

-(IBAction)chooseFirstController:(id)sender {
    _nextController = [[FirstController alloc] initWithNibName:@"FirstController" bundle:nil];
    [self.contentView addSubview:_nextController.view];

}
-(IBAction)chooseSecondController:(id)sender{
    _nextController = [[SecondController alloc] initWithNibName:@"SecondController" bundle:nil];
    [self.contentView addSubview:_nextController.view];
}
-(IBAction)chooseThirdController:(id)sender{
    _nextController = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
    [self.contentView addSubview:_nextController.view];
}

我的第一个和第三个控制器只是在 contentController 的 contentView 中显示 tableviews。但在我的 secondController 中,我使用了 gridView 来显示项目。现在,当我按下一个单元格时,它应该转到一个 detailViewController,它会显示有关该单元格的更多信息。所以在我的 cellForRowAtIndex 中,我做了以下操作。

- (void)gridView:(NRGridView *)gridView didSelectCellAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"cell clicked");
    PlayerDetailController *playerController = [[PlayerDetailController alloc]initWithNibName:@"PlayerDetailController" bundle:nil];
    [playerController setLblTest:@"hooray this works"];
    [self.view addSubview:playerController.view];

}

它正确执行 setLblTest 并在我的 ContentView 中显示 detailViewcontroller。但它不会改变标签。对我的@“hooray”来说,这是可行的。但是,尽管您可以在我的日志中看到它正确地传递了它。

@synthesize testLabel = _testLabel;
@synthesize lblTest = _lblTest;

-(void)setLblTest:(NSString *)lblTest{
    if(![_lblTest isEqual:lblTest]){
        _lblTest = lblTest;
    }
    NSLog(@"log komt %@",lblTest);
    [_testLabel setText:lblTest];
}

日志

RacingGenk[567:c07] log komt hooray this works

希望这能进一步澄清我的问题

谢谢

【问题讨论】:

  • 在 IB 中是否连接了 testLabel?
  • 你的问题是什么? IE。你会期待什么?
  • 你确定这是准确的代码吗?我的意思是在您编写“ViewController”的界面中。如果你没有继承 UIViewController 这甚至不会编译。
  • testLabel 是用 lblTest 字符串设置的。
  • 另一个观察 isEqual: 与 isEqualToString: 不同,我想你可能已经想到了。

标签: objective-c ios ipad view uilabel


【解决方案1】:

要更新标签,您需要将代码更改为如下所示。

self.testLabel.text = lblTest;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多