iOS - 无法设置 UILabel 的文本
好吧,让我解释一下情况,我有两个视图控制器,让我们先调用它们,然后调用它们。
- FirstViewController 继承自 UITableViewController
- SecondViewController 继承自 UIViewController
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 dataSource 方法,
- (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 是否为空,确实如此,我不明白为什么。也许空值与 iVar aTransfer 的分配有关。顺便说一句,不可能设置标签的文本。
Ok let me explain the situation, I have two view controller let's call them first and second.
- FirstViewController inherit from UITableViewController
- SecondViewController inherit From UIViewController
The interface for the SecondViewController is made with Interface Builder and contains a label and an UIProgressView. Both label and UIProgressView outlet are connected with the right files owner (SecondViewController).
a little bit of code, In FirstViewController :
the following method is triggered by a notification
- (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];
}
those are the tableView dataSource methods
- (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;
}
this is the code of 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
this is a method inside 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];
}
as you can see inside the startTransfer I check if fileNameLabel is null and it is, and I don't understand why. Maybe the null value is related to the allocation of the iVar aTransfer. btw it's impossible to set the text of the label.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题出在初始化中,我在加载视图之前设置标签。在viewDidLoad中初始化标签解决了这个问题。
The problem was in the initialization, i was setting the label before the view was loaded. Initialize the label in viewDidLoad solved the problem.
Elio
简单测试 - 在设置 self.fileNameLabel.text 的行设置断点。当应用程序停在那里时,使用调试器查看指针是否为空。
最可能的原因:
- 插座未正确连接
- 文件所有者的类别不正确,请确保将其设置为您的 DbTransfer 类别
H
Elio
Simple test - set a breakpoint at the line where you set self.fileNameLabel.text. When the app stops there, use the debugger to see if the pointer is null.
Most likely causes:
- The outlet is not linked properly
- The file owner is not of the right class, make sure to set it to your DbTransfer class
H