1

我使用此示例打开我的条款和条件按钮的弹出窗口。

打开时它可以工作,但关闭它会给出exc bad access error.

在我的应用程序中,我使用storyboards的可能是没有关闭的问题

这是我在弹出窗口类中使用的代码:

  #import "MTPopupWindow.h"

 #define kShadeViewTag 1000

 @interface MTPopupWindow(Private)
 - (id)initWithSuperview:(UIView*)sview andFile:(NSString*)fName;
 @end

 @implementation MTPopupWindow


- (id)initWithSuperview:(UIView*)sview andFile:(NSString*)fName
{

self = [super init];
if (self) {
    // Initialization code here.
    bgView = [[UIView alloc] initWithFrame: sview.bounds];
    NSLog(@"%f",sview.bounds.size.width);
    NSLog(@"%f",sview.bounds.size.height);

    NSLog(@"%f",sview.bounds.origin.x);
    NSLog(@"%f",sview.bounds.origin.y);


    [sview addSubview: bgView];

    // proceed with animation after the bgView was added
    [self performSelector:@selector(doTransitionWithContentFile:) withObject:fName afterDelay:0.1];
}

return self;
}


 -(void)doTransitionWithContentFile:(NSString*)fName
{
//faux view
UIView* fauxView = [[UIView alloc] initWithFrame: CGRectMake(10, 10, 200, 200)];
[bgView addSubview: fauxView];

//the new panel
bigPanelView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, bgView.frame.size.width, bgView.frame.size.height)];
bigPanelView.center = CGPointMake( bgView.frame.size.width/2, bgView.frame.size.height/2);

//add the window background
UIImageView* background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"popupWindowBack1.png"]];
background.center = CGPointMake(bigPanelView.frame.size.width/2, bigPanelView.frame.size.height/2);
[bigPanelView addSubview: background];

//add the web view
int webOffset = 10;
UIWebView* web = [[UIWebView alloc] initWithFrame:CGRectInset(background.frame, webOffset, webOffset)];
web.backgroundColor = [UIColor clearColor];

if ([fName hasPrefix:@"http"]) {
    //load a web page
    web.scalesPageToFit = YES;
    [web loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: fName]]];
} else {
    //load a local file
    NSError* error = nil;
    NSString* fileContents = [NSString stringWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fName] encoding:NSUTF8StringEncoding error: &error];
    if (error!=NULL) {
        NSLog(@"error loading %@: %@", fName, [error localizedDescription]);
    } else {
        [web loadHTMLString: fileContents baseURL:[NSURL URLWithString:@"file://"]];
    }
}

[bigPanelView addSubview: web];

//add the close button
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];

//animation options
UIViewAnimationOptions options = UIViewAnimationOptionTransitionFlipFromRight |
UIViewAnimationOptionAllowUserInteraction    |
UIViewAnimationOptionBeginFromCurrentState;

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

    //dim the contents behind the popup window
    UIView* shadeView = [[UIView alloc] initWithFrame:bigPanelView.frame];
    shadeView.backgroundColor = [UIColor blackColor];
    shadeView.alpha = 0.3;
    shadeView.tag = kShadeViewTag;
    [bigPanelView addSubview: shadeView];
    [bigPanelView sendSubviewToBack: shadeView];
}];
}


-(void)closePopupWindow
{
//remove the shade
[[bigPanelView viewWithTag: kShadeViewTag] removeFromSuperview];   

//[self performSelector:@selector(closePopupWindowAnimate) withObject:nil afterDelay:0.1];

}


-(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];


}];
 }

 @end
4

0 回答 0