0

我有一些标签的问题。我创建了一个公共方法,它从表格视图中获取信息,并在 3 个标签、一个文本视图中显示此信息,并从网络加载图片。我在表视图 didSelectRowAtIndexPath 方法中设置了所有变量,但是在显示该信息时遇到了一些问题。我这样做了:在表视图控制器中,我调用了另一种方法:

- (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"]];
}

我实现了这个方法:

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

如果我查看日志,我会看到正确的东西,但如果我查看 iPhone 或 iPhone 模拟器上的 GUI,它将保持空白。这段代码有什么问题?感谢您的建议。

@亚历克斯布伦德尔

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

3 回答 3

2

问题是DetailCharacterViewController当您调用时您的视图尚未加载setupDetailViewControllerWithName:...,因此没有连接任何插座(还)。通过 alloc-init 实例化视图控制器不会自动加载其视图。

[self view]您可以通过在 setup 方法的开头调用来强制加载视图。view这是有效的,因为如果之前没有加载该属性,访问该属性将自动加载视图。

或者,您可以制作视图控制器的namesurnameNSString属性,并在 中设置相应的标签viewDidLoad

于 2013-04-13T13:08:55.690 回答
1

您是否使用 Interface Builder/Storyboards 来设计您的界面?您是否确保为每个 UITextField/View 连接 IBOutlets?

您应该在视图控制器类的头文件中识别对象,并确保它们以 IBOutlet 为前缀,以便它们出现在 Interface Builders 连接窗格中。例如

IBOutlet UITextField labelName;
[...]

然后您需要转到 IB 并转到视图控制器的连接窗格以连接每个文本字段/视图。

于 2013-04-13T12:26:31.250 回答
0

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)

于 2013-04-13T17:10:59.753 回答