iOS - 无法设置 UILabel 的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5051210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 18:57:28  来源:igfitidea点击:

iOS - Unable to set text of UILabel

ipadiostextuilabel

提问by elio.d

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

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

  • FirstViewController inherit from UITableViewController
  • SecondViewController inherit From UIViewController
  • FirstViewController 继承自 UITableViewController
  • SecondViewController 继承自 UIViewController

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).

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

a little bit of code, In FirstViewController :

一点点代码,在 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

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

}

this is the code of SecondViewController.h

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

这是 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.

正如您在 startTransfer 中看到的那样,我检查 fileNameLabel 是否为空,并且确实如此,但我不明白为什么。也许空值与 iVar aTransfer 的分配有关。顺便说一句,不可能设置标签的文本。

回答by elio.d

The problem was in the initialization, i was setting the label before the view was loaded. Initialize the label in viewDidLoad solved the problem.

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

回答by Hiltmon

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.

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

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

最可能的原因: - 插座未正确链接 - 文件所有者不属于正确的类别,请确保将其设置为您的 DbTransfer 类别

H

H