iOS - 无法设置UILabel的文本

eli*_*o.d 13 text uilabel ipad ios

好吧让我解释一下情况,我有两个视图控制器让我们先调用它们.

  • 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];

}
Run Code Online (Sandbox Code Playgroud)

那些是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;

}
Run Code Online (Sandbox Code Playgroud)

这是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
Run Code Online (Sandbox Code Playgroud)

这是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];

}
Run Code Online (Sandbox Code Playgroud)

正如你在startTransfer中看到的那样,我检查fileNameLabel是否为null,它是,并且我不明白为什么.也许null值与iVar aTransfer的分配有关.顺便说一下,不可能设置标签的文字.

eli*_*o.d 26

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