0

私のプロジェクトでは、次のように右上隅に閉じるボタンを追加しました。

int closeBtnOffset = 10;
UIImage* closeBtnImg = [UIImage imageNamed:@"popupCloseBtn.png"];
UIButton* closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[closeBtn setImage:closeBtnImg forState:UIControlStateNormal];
[closeBtn setFrame:CGRectMake( background.frame.origin.x + background.frame.size.width - closeBtnImg.size.width - closeBtnOffset, 
                               background.frame.origin.y ,
                               closeBtnImg.size.width + closeBtnOffset, 
                               closeBtnImg.size.height + closeBtnOffset)];
[closeBtn addTarget:self action:@selector(closePopupWindow) forControlEvents:UIControlEventTouchUpInside];
[bigPanelView addSubview: closeBtn];

closePopupWindw メソッドは次のようになります。

-(void)closePopupWindow
{
    //remove the shade
    [[bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];
    [self performSelector:@selector(closePopupWindowAnimate) withObject:nil afterDelay:0.1];

}

ビルドは成功しますが、closeBtn ボタンをクリックすると、次のメッセージが表示されてプログラムがシャットダウンします: http://i45.tinypic.com/ddndsl.png

別のプロジェクトからコピーしたコードなので問題はないと思いますが、別のプロジェクトでは ARC を使用していませんでした。それが問題かどうかはわかりません。

編集:

-(void)closePopupWindowAnimate
{

    //faux view
    __block UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
    [bgView addSubview: fauxView];

    //run the animation
    UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromLeft |
    UIViewAnimationOptionAllowUserInteraction    |
    UIViewAnimationOptionBeginFromCurrentState;

    //hold to the bigPanelView, because it'll be removed during the animation

    [UIView transitionFromView:bigPanelView toView:fauxView duration:0.5 options:options completion:^(BOOL finished) {

        //when popup is closed, remove all the views
        for (UIView* child in bigPanelView.subviews) {
            [child removeFromSuperview];
        }
        for (UIView* child in bgView.subviews) {
            [child removeFromSuperview];
        }

        [bgView removeFromSuperview];

    }];
}
4

2 に答える 2

3

strong既にリリースされているオブジェクトにアクセスしています。プロパティを使用し、 ( を使用して)プロパティ タイプを設定しARCて、ビューがアクティブである限りメモリ内の場所を保持できるようにすることをお勧めします。

UIButtonあなたの問題を解決するクラス Property としてあなたを宣言してください。ボタンが追加されbigPanelView、メソッドを呼び出す前にこのビューを削除していることも確認する必要がありますclosePopupWindowAnimate

于 2013-03-11T10:26:29.393 に答える
0

ざっと見てみると、closePopupWindow での performSelector 呼び出しに問題があるのではないかと思います。ボタンは bigPanelView によって保持されますが、これは最初の行で解放されます。これは、performSelector が呼び出される前に「self」の割り当てが解除されることを意味する場合があります。

スタイルの問題として、ビュー タグの使用は避けてください。ある種の親オブジェクトの関連するビューにプロパティを定義する方がはるかに優れています。これにより、ここにある問題のような保持サイクルや早期リリースを回避することも容易になります

于 2014-04-08T08:22:33.373 に答える