[英]iOS - Unable to set text of UILabel
好的,让我解释一下情况,我有两个视图控制器,让我们先调用它们,然后调用它们。
SecondViewController 的界面由 Interface Builder 制作,包含一个标签和一个 UIProgressView。 标签和 UIProgressView 出口都与正确的文件所有者(SecondViewController)连接。
一点代码,在 FirstViewController :
以下方法由通知触发
- (void) addTransfer:(NSNotification *)notification{
NSLog(@"notification received");
NSDictionary *transferInfo = [notification userInfo];
// I think the problem is here
aTransfer = [[DbTransfer alloc] initWithNibName:@"DbTransfer" bundle:nil];
//
aTransfer.srcPath = [transferInfo objectForKey:@"srcPath"];
aTransfer.dstPath = [transferInfo objectForKey:@"dstPath"];
[aTransfer startTransfer];
[transfer addObject:aTransfer];
[self.tableView reloadData];
}
这些是 tableView 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
NSLog(@"%d numberOfRowsInSection",[transfer count]);
return [transfer count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
[cell.contentView addSubview:[[transfer objectAtIndex:indexPath.row] view]];
return cell;
}
这是 SecondViewController.h 的代码
@interface DbTransfer : UIViewController <DBRestClientDelegate> {
IBOutlet UILabel *fileNameLabel;
IBOutlet UIProgressView *transferProgress;
NSString *srcPath;
NSString *dstPath;
DBRestClient *restClient;
}
@property (nonatomic,retain) IBOutlet UILabel *fileNameLabel;
@property (nonatomic,retain) IBOutlet UIProgressView *transferProgress;
@property (nonatomic,retain) NSString *srcPath;
@property (nonatomic,retain) NSString *dstPath;
- (void) startTransfer;
@end
这是 SecondViewcontroller.m 中的一个方法
- (void) startTransfer{
//NSLog(@"%@\n%@",srcPath,dstPath);
if (!fileNameLabel) {
NSLog(@"null");
}
[self.fileNameLabel setText:[srcPath lastPathComponent]];
//self.fileNameLabel.text=@"test";
NSLog(@"%@",fileNameLabel.text);
restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
restClient.delegate=self;
[restClient loadFile:srcPath intoPath:dstPath];
}
正如您在 startTransfer 中看到的那样,我检查 fileNameLabel 是否为空,但我不明白为什么。 可能 null 值与 iVar aTransfer 的分配有关。 顺便说一句,不可能设置标签的文本。
问题出在初始化中,我在加载视图之前设置了标签。 在 viewDidLoad 中初始化标签解决了这个问题。
埃利奥
简单测试 - 在设置 self.fileNameLabel.text 的行设置断点。 当应用程序停在那里时,使用调试器查看指针是否为空。
最可能的原因: - 出口未正确链接 - 文件所有者不是正确的类,请确保将其设置为您的 DbTransfer 类
H
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.