我在一个UIViewController和函数创建一个UILabel更改一个UILabel文本更改标签,它们被初始化为.h文件如下:从另一个类
@interface StoreDetailsController : UIViewController {
UILabel *storeNameLabel;
}
@property (nonatomic, retain) IBOutlet UILabel *storeNameLabel;
- (IBAction)LabelTheStore:(int)storeNumber;
然后在.m文件:
@synthesize storeNameLabel;
...
-(void)LabelTheStore:(int)storeNumber
{
NSLog(@"CHECK NUMBER: %d", storeNumber);
storeNameLabel = [[UILabel alloc] init];
storeNameLabel.text = @"TEST";
}
一个int变量被传递给将被使用的函数。日志根据我传递的内容显示正确的数字,所以我知道函数被正确调用,但是当我从另一个类调用函数时,标签永远不会更新。如果我在storeNameLabel.text上调用NSLog,则显示为(null)。
storeNameLabel在界面生成器中正确链接,程序生成OK。 更新:
的方法来加载StoreDetailsController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
StoreDetailsController *storeDetailsController = [StoreDetailsController alloc];
storeDetailsController = [storeDetailsController initWithNibName:@"StoreDetailsController" bundle:[NSBundle mainBundle]];
NSInteger row = indexPath.row;
[storeDetailsController LabelTheStore:row];
[self.navigationController pushViewController:storeDetailsController animated:YES];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
每次选择表格行时分配和初始化storeDetailsController是非常不寻常的;这可能是问题所在。正常的做法是1)你有一个视图,用于存储细节和一个控制器的视图,2)当你选择一行时,你改变了该视图的内容。在你的tableViewController中,你需要访问storeDetailsController。该访问通常在Xcode Interface Builder中设置 - 但有很多方法可以实现。更现代的方式是从表格视图中的单元格到storeDetailsController。 – GoZoner
一个不太现代的方式,它为您的tableViewController在头文件中放置'IBOutlet storeDetailsController storeDetailsController',然后将它链接到Xcode IB中。然后你不分配它。您的didSelectRowAtIndexPath变为:{[self.storeDetailsController LabelTheStore:indexPath.row];/*推,取消选择* /} – GoZoner
@GoZoner我已经完全重写我的应用程序以利用故事板功能。我现在处于同一阶段,但我无法使用segue来显示storeDetails视图。你有可以给我的教程或建议吗? – robbiecutting