Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS - Unable to set text of UILabel

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

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.

like image 380
oiledCode Avatar asked Feb 19 '11 14:02

oiledCode


2 Answers

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

like image 190
oiledCode Avatar answered Nov 10 '22 19:11

oiledCode


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

like image 25
Hiltmon Avatar answered Nov 10 '22 21:11

Hiltmon