2016-02-05 65 views
-1

這是我創建的UIButton代碼,消息來禁用它

- (void) setNavButton 
{ 
    self->_newbutton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    UIImage *myImage = [UIImage imageNamed:@"[email protected]"]; 

    UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0,0, 46,57)] autorelease]; 

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSetAllowsAntialiasing(context, true); 
    CGContextSetShouldAntialias(context, true); 
    CGContextSetInterpolationQuality(context, kCGInterpolationHigh); 

    [imageView.layer setMinificationFilter:kCAFilterTrilinear]; 
    [imageView.layer setAllowsEdgeAntialiasing:YES]; 
    CGRect imageRect = CGRectMake(0,0, imageView.bounds.size.width+10, imageView.bounds.size.height); 

    UIGraphicsBeginImageContextWithOptions(imageRect.size, NO, [UIScreen mainScreen].scale); 

    [imageView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    [myImage drawInRect:CGRectMake(0,12,myImage.size.width,myImage.size.height)]; 
    myImage = UIGraphicsGetImageFromCurrentImageContext(); 
    UIGraphicsEndImageContext(); 

    [imageView setImage:myImage]; 
    [self->_newbutton setImage:[imageView image] forState:UIControlStateNormal]; 
} 

我創建屬性這一點,我訪問它在同一禁用它。但是,當這樣做,我得到和錯誤說'消息發送到一個釋放實例'。我正在使用下面的代碼來禁用它。

[self->_newbutton setEnabled:No]; 
+5

任何原因爲'self - > _ newbutton'而不是'self._newbutton'? – zcui93

+0

In class.h UIButton * _newbutton; @property(nonatomic,retain)UIButton * newbutton;×只有一個額外的 – Nikhil

+0

閱讀關於目標C點語法,這不是C++訪問指針的方式。 – zcui93

回答

1

與​​

_newbutton = [UIButton buttonWithType:UIButtonTypeCustom]; 

同時設定圖像與​​

[_newbutton setImage:[imageView image] forState:UIControlStateNormal]; 

然後設置禁用爲

[self._newbutton setEnabled:No]; 

希望,它會幫你生成按鈕。
謝謝

+0

問題在於Autoreleasing新按鈕。在被調用之前,新的按鈕變得更加靈活,所以它變成了一個Zobie對象。所以造成了一次撞車。非常感謝你..... – Nikhil