无法隐藏自定义子视图

时间:2013-10-29 17:04:21

标签: ios objective-c uiview uialertview uiactivityindicatorview

我为iOS 7创建了一个子视图(看起来很像带有微调器的alertview),并利用了wimagguc的iOS 7自定义警报视图(git上的ios-custom-alertview)。使用ARC。我遇到的问题是我在处理时显示视图,然后在处理完成时调用关闭,但仍在屏幕上显示。

Connection.m(编辑)

 UIActivityIndicatorView *aSpinnerCustom;
 CustomIOS7AlertView *alertViewCustom;

 - (void)showWaittingAlert:(BOOL)isShow {

NSString *ver = [[UIDevice currentDevice] systemVersion];
float ver_float = [ver floatValue];

// custom alert view for iOS 7 from AlertViewCustom folder
alertViewCustom = [[CustomIOS7AlertView alloc] init];

//spinner to be added to iOS 7 AlertView
aSpinnerCustom = [[UIActivityIndicatorView alloc]  initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];

//label to be used as subview to help text and spinner
alertTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 75)];

//label of app name
UILabel *subTitleApp;
subTitleApp = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 250, 40)];
subTitleApp.text = APP_NAME;
subTitleApp.font = [UIFont boldSystemFontOfSize:16.0f];
subTitleApp.textAlignment = UITextAlignmentCenter;
subTitleApp.numberOfLines = 0;

//label of processing
UILabel *subTitle;
subTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 25, 250, 25)];
subTitle.text = @"processing...";
subTitle.font = [UIFont systemFontOfSize:14.0f];
subTitle.textAlignment = UITextAlignmentCenter;

//set height of spinner in custom alertview
aSpinnerCustom.frame = CGRectMake(round((alertTitle.frame.size.width - 25) / 2), round(alertTitle.frame.size.height - 30), 25, 25);
aSpinnerCustom.tag  = 1;

// adding text and spinner to alertview
[alertTitle addSubview:aSpinnerCustom];
[alertTitle addSubview:subTitleApp];
[alertTitle addSubview:subTitle];

//adding label to custom alertview
[alertViewCustom setContainerView:alertTitle];

[alertViewCustom setButtonTitles:NULL];

    if (isShow) {
        [alertViewCustom show];
        [aSpinnerCustom startAnimating];
        NSLog(@"%@",@"showWaitingAlert Show iOS 7");

    }else {
        [alertViewCustom close];
        [aSpinnerCustom stopAnimating];

        NSLog(@"%@",@"showWaitingAlert Stop iOS 7");

    }

}

2 个答案:

答案 0 :(得分:2)

这是因为您使用的是实例变量和属性名称的错误组合。我个人从不使用自动生成的@synthesize方法,并会像这样重新编写您的类:

.h文件:

@interface ConnectionManager : NSObject {
    UIActivityIndicatorView   *_aSpinnerCustom;
    CustomIOS7AlertView *_alertViewCustom;
}
@property (nonatomic, retain) UIActivityIndicatorView *aSpinnerCustom;
@property (nonatomic, retain) CustomIOS7AlertView *alertViewCustom;

.m文件:

// Be explicit
@synthesize aSpinnerCustom = _aSpinnerCustom;
@synthesize alertViewCustom = _alertViewCustom;

然后将对aSpinnerCustomalertViewCustom的每个引用更改为self.aSpinnerCustomself.alertViewCustom。切勿直接在非ARC环境中引用实例变量。

您的代码发生了什么事情是自动生成的@synthesize方法创建了以_开头的支持变量,以及您创建的支持变量以及有时使用alertViewCustom和{{1在其他时候,正在使用错误的对象引用。

不要忘记,self.alertViewCustom方法应使用dealloc来释放对象。

答案 1 :(得分:1)

想出来,每次调用方法时都会创建我的子视图。我包含一个if alertview == nil所以它没有创建两次。感谢@trojanfoe的帮助。

 if (alertViewCustom == nil){
 setup view code here..
 }