3

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

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");

    }

}

4

2 回答 2

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;

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

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

并且不要忘记您的dealloc方法应该用于self.whatever = nil;释放对象。

于 2013-10-29T17:19:19.530 回答
1

弄清楚了,每次调用该方法时都会创建我的子视图。我包括了一个 if alertview == nil 所以它没有被创建两次。也感谢@trojanfoe 的帮助。

 if (alertViewCustom == nil){
 setup view code here..
 }
于 2013-10-29T19:42:41.520 回答