0
#import "BirdsDetailViewController.h"
#import "BirdSighting.h"
@interface BirdsDetailViewController ()
- (void)configureView;
@end

@implementation BirdsDetailViewController
-(void)setSighting:(BirdSighting *)sighting
{
    if (_sighting != sighting) {
        _sighting = sighting;
        //Update the view
        [self configureView];
// Is this code[self configureView] necessary which reappears in the later viewDidLoad? 
//After I deleted this line everything seems works well.
    }
}
- (void)configureView
{
// Update the user interface for the detail item.
    BirdSighting *theSighting = self.sighting;
    static NSDateFormatter *formatter = nil;
    if (formatter == nil) {
    formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    }
    if (theSighting) {
    self.birdNameLable.text = theSighting.name;
    self.locationLable.text = theSighting.location;
    self.dateLable.text = [formatter stringFromDate:(NSDate *) theSighting.date];
}

}
- (void)viewDidLoad
{
[super viewDidLoad];
//You see. It reappears here.
[self configureView]; 
}

@end

上面的代码引用自 Apple 的官方示例(Your Second iOS App: Storyboards)。setSighting: 中的这段代码 [self configureView] 是否需要稍后重新出现?在我删除此行后,一切似乎都运行良好。非常感谢。

4

1 回答 1

1

您可以[self configureView];在 中省略setSighting:

还有一件事为什么您要setSighting:自己实现,Cocoa 为您提供 @property/ @synthesize,这是值得信赖的,并且对于您的代码来说会更短,因为您没有做任何具体的事情。

于 2013-02-28T04:51:54.773 回答