开发者

iOS - Unable to set text of UILabel

开发者 https://www.devze.com 2023-02-11 21:42 出处:网络
Ok let me explain the situation, I have two view controller let\'s call them first and second. FirstViewController inherit from UITableViewController

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 th开发者_C百科e 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.


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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消