Reputation: 2002
I've a problem with some label. I made a public method that takes information from a table view and shows this info in 3 label, a text view and it load a pic from the web. I setup all the variable in the table view didSelectRowAtIndexPath method, but I'm having some issue to display that info. I made so: in the table view controller I called the other method so:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
self.detailCharacterViewController = [[DetailCharacterViewController alloc] init];
[self.detailCharacterViewController setupDetailViewControllerWithName:[self.arrayCharacters[indexPath.row] objectForKey:@"name"] andSurname:[self.arrayCharacters[indexPath.row] objectForKey:@"surname"] andKind:[self.arrayCharacters[indexPath.row] objectForKey:@"kind"] andDescription:[self.arrayCharacters[indexPath.row] objectForKey:@"description"] andImage:[self.arrayCharacters[indexPath.row] objectForKey:@"URLpic"]];
}
and I implemented the method so:
- (void)setupDetailViewControllerWithName:(NSString*)name andSurname:(NSString *)surname andKind:(NSString*) kind andDescription:(NSString*)description andImage:(NSString*)url {
NSLog(@"name = %@", name);
self.labelName.text = name;
self.labelSurname.text = surname;
self.labelKind.text = kind;
self.textDescription.text = description;
NSURL *urlPic = [NSURL URLWithString:url];
NSData *dataPic = [NSData dataWithContentsOfURL:urlPic];
[self.imagePic setImage:[UIImage imageWithData:dataPic]];
}
If I look to the log i see the right things, but if I look to the GUI on iPhone or iPhone simulator it will remain blank. What's wrong with this code? Thank you for the suggestion.
@Alex Blundell
#import <UIKit/UIKit.h>
@interface DetailCharacterViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *labelName;
@property (weak, nonatomic) IBOutlet UILabel *labelSurname;
@property (weak, nonatomic) IBOutlet UILabel *labelKind;
@property (weak, nonatomic) IBOutlet UIImageView *imagePic;
@property (weak, nonatomic) IBOutlet UITextView *textDescription;
- (void)setupDetailViewControllerWithName:(NSString*)name andSurname:(NSString *)surname andKind:(NSString*) kind andDescription:(NSString*)description andImage:(NSString*)url;
@end
Upvotes: 0
Views: 420
Reputation: 379
This problem happened to me as well.
It has to do with the fact that iOS actually hooks up your outlets really really late.
If you put a breakpoint in your setupDetailViewControllerWithName:
and check your IBOutlet variables, they will be nil. That's why you are seeing a blank view. (The good old nil erroring)
The work-around I will do is to declare properties in the DetailCharacterViewController
class. (Such as @property (copy, nonatomic) NSString* name
).
And in the place where you are calling setupDetailViewControllerWithName:
, set those properties instead such as:
self.detailCharacterViewController.name = name;
Finally in your DetailCharacterViewController
's viewDidLoad:
, you set the values of the labels using the saved properties. (The outlets are for sure hooked up then).
(By the way, this problem exists for prepareForSegue:
as well. The destination view controller's outlets are all still nil in prepareForSegue:
. So you cannot set them directly there yet)
Upvotes: 0
Reputation: 53551
The problem is that your DetailCharacterViewController
's view is not loaded yet when you call setupDetailViewControllerWithName:...
, so none of the outlets are connected (yet). Instantiating a view controller via alloc-init does not load its view automatically.
You could force loading the view by calling [self view]
at the beginning of your setup method. This works because accessing the view
property will automatically load the view if it wasn't loaded before.
Alternatively, you could make the name
, surname
, etc. NSString
properties of the view controller and set the corresponding labels in viewDidLoad
.
Upvotes: 2
Reputation: 2104
Are you using Interface Builder/Storyboards to design your interface? Have you made sure to connect the IBOutlets for each UITextField/View?
You should identify the objects in the header file on your view controller class, and ensure they're prefixed by IBOutlet for them to appear in Interface Builders connections pane. E.g.
IBOutlet UITextField labelName;
[...]
Then you need to go to IB and go to the connections pane for the view controller to connect each text field/view.
Upvotes: 1