这个代码[self configureView]在setSighting中是否必要,以后会重新出现?

时间:2013-02-28 04:46:21

标签: objective-c

#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)。 这个代码[self configureView]在setSighting中是必要的吗?后来再次出现?在我删除这一行后,一切似乎都运行良好。 非常感谢你。

1 个答案:

答案 0 :(得分:1)

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

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