iOS - 无法设置UILabel的文本 [英] iOS - Unable to set text of UILabel

查看:106
本文介绍了iOS - 无法设置UILabel的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的让我解释一下情况,我有两个视图控制器让我们先调用它们。第二个。

Ok let me explain the situation, I have two view controller let's call them first ans second.


  • FirstViewController继承自UITableViewController

  • SecondViewController继承自UIViewController

SecondViewController的接口是使用Interface Builder制作的,只包含一个标签和UIProgressView。标签和UIProgressView插座都与正确的文件所有者(SecondViewController)连接。

The interface for the SecondViewController is made with Interface Builder and contains simply a label and an UIProgressView. both label and UIProgressView outlet are connected with the right files owner (SecondViewController).

一点代码,在FirstViewController中:

a little bit of code, In 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方法

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;

}

这是SecondViewController.h的代码

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

这是SecondViewcontroller.m中的一个方法

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

}

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

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.

推荐答案

问题在于初始化,我在设置标签之前视图已加载。在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.

这篇关于iOS - 无法设置UILabel的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆