我有以下情况。我有一个 View Controller (ContentController),顶部有 3 个按钮。下面我添加了第二个 View (contentView),该 View 应显示这 3 个按钮后面的内容。所以每次我按下按钮时,contentView 都会发生变化。所以我所做的是创建了三个加法 Controller 。
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];
}
我的第一个和第三个 Controller 只是在 contentController 的 contentView 中显示表格 View 。但在我的第二个 Controller 中,我使用 gridView 来显示项目。现在,当我按下一个单元格时,它应该转到一个详细 View Controller ,它显示有关该单元格的更多信息。因此,在我的 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 中显示详细 View Controller 。但它不会改变标签。对于我的@“万岁”,这有效。但是,尽管正如您在我的日志中看到的那样,它正确地通过了。
@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
希望这能进一步澄清我的问题
谢谢
最佳答案
要更新标签,您需要将代码更改为如下所示。
self.testLabel.text = lblTest;
关于objective-c - 无法在 View 中设置标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940492/