2015-10-01 59 views
-1

請解釋爲什麼如果您投票。 我的UIView的一個子類實現代碼如下節頭UIview調用autorelease的子類會導致崩潰

@interface ContactSectionHeaderView() 

@property (nonatomic, retain) IBOutlet UILabel *headerTitle; 

@end 

@implementation ContactSectionHeaderView 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 

    if(self){ 
     id rootView = [[self class] viewSizedForDevice]; 
     [rootView setFrame:frame]; 
     self = rootView; 
    } 

    return self; 
} 


+ (id) viewSizedForDevice { 
    UINib* nib = [self nib]; 
    NSArray * nibObjects = [nib instantiateWithOwner:self options:nil]; 
    NSAssert2(([nibObjects count] > 0) && 
       [[nibObjects objectAtIndex:0] isKindOfClass:[self class]], 
       @"Nib '%@' does not appear to contain a value %@", 
       [self nibName], NSStringFromClass([self class])); 
    id rootView = nibObjects[0]; 
    return rootView; 
} 

+(UINib*)nib { 
    NSBundle * classBundle = [NSBundle bundleForClass:[self class]]; 
    UINib * nib = [UINib nibWithNibName:[self nibName] bundle:classBundle]; 
    return nib; 
} 

+ (NSString *)nibName { 
    return [self viewIdentifier]; 
} 

+ (NSString *)viewIdentifier { 
    static NSString* _viewIdentifier = @"ContactSectionHeaderView"; 
    _viewIdentifier = NSStringFromClass([self class]); 
    return _viewIdentifier; 
} 

-(void)setTitle:(NSString *)title { 
    self.headerTitle.text = [NSString stringWithFormat:@"%@",title];  
} 
- (void)dealloc { 
    self.headerTitle = nil; 
    [super dealloc]; 
} 

@end 

我在- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

ContactSectionHeaderView *sectionHeader = [[[ContactSectionHeaderView alloc] initWithFrame:[tableView rectForHeaderInSection:section]] autorelease]; 

autorelease使用這樣導致飛機失事的原因。如果我刪除了autorelease,那麼它完美的工作。我如何解決崩潰?

+0

你的項目是ARC還是非ARC? –

+0

您不需要使用'autorelease',因爲現在所有東西都是弧形的,它會自動完成,您正在使用石器時代代碼。 – iphonic

+0

我的項目在非ARC @AshishThakkar – tausun

回答

-1

刪除autorelease。 ARC沒有必要使用保留,釋放方法。 ARC爲你做的

+0

我的項目是在非ARC – tausun