iOS - 无法设置UILabel的文本

时间:2011-02-19 14:11:57

标签: ipad ios text 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是否为null,它是,我不明白为什么。也许null值与iVar aTransfer的分配有关。顺便说一下,不可能设置标签的文字。

2 个答案:

答案 0 :(得分:26)

问题在于初始化,我在加载视图之前设置了标签。在viewDidLoad中初始化标签解决了问题。

答案 1 :(得分:2)

埃利奥

简单测试 - 在您设置self.fileNameLabel.text的行设置断点。当应用停在那里时,使用调试器查看指针是否为空。

最有可能的原因: - 插座没有正确连接 - 文件所有者不是正确的类,请确保将其设置为DbTransfer类

ħ

相关问题