自动关闭警报视图和切换视图(Xcode)

时间:2011-03-28 07:01:38

标签: iphone xcode view

我正在开发一个应用程序,但我在自动关闭警报视图时遇到了一些问题并切换到另一个视图。 这是我的代码:

这是文件“Progress.h”:

  #import <Foundation/Foundation.h>  
  #import "Progress_ToolViewController.h"

@interface Progress : UIAlertView {  
    UIProgressView *myPro;  
    //UILabel *myPercent;  
    UILabel *myCount;  
    NSTimer *timer;  
    int count;  
    BOOL userDismissed;  
}  
@property (nonatomic, retain)UIProgressView *myPro;  
//@property (nonatomic, retain)UILabel *myPercent;  
@property (nonatomic, retain)UILabel *myCount;  

-(void)start;  
-(void)moreprogress;  
-(void)makecount;  
-(id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okButtonTitle;  
@end  

这是文件“Progress.m”:

 #import "Progress.h"  
 #import "Progress_ToolViewController.h"  
 @implementation Progress    
 @synthesize myPro;    
 @synthesize myCount;  
//@synthesize myPercent;  
- (id)initWithTitle:(NSString *)title message:(NSString *)message delegate:(id)delegate cancelButtonTitle:(NSString *)cancelButtonTitle okButtonTitle:(NSString *)okayButtonTitle
{  

    if (self = [super initWithTitle:title message:message delegate:delegate cancelButtonTitle:cancelButtonTitle otherButtonTitles:okayButtonTitle, nil])  
    {  
        UIProgressView *thePro = [[UIProgressView alloc]  initWithFrame:CGRectMake(12.0, 60.0, 220.0, 20.0)];   
        UILabel *theLabel = [[UILabel alloc] initWithFrame:CGRectMake(240.0, 55.0, 60.0, 20.0)];  

        [thePro setBackgroundColor:[UIColor clearColor]]; 
        [theLabel setBackgroundColor:[UIColor clearColor]];
        [theLabel setTextColor:[UIColor whiteColor]];
        theLabel.text = 0;
        [self addSubview:thePro];
        [self addSubview:theLabel];
        self.myPro = thePro;
        self.myCount = theLabel;
        myCount.text = @"0";
        [self start];
        [thePro release];
        [theLabel release];
        CGAffineTransform translate = CGAffineTransformMakeTranslation(0.0, 65.0); 
        [self setTransform:translate];
    }
    return self;
}


-(void)start{  
    myPro.progress = 0.0;  
    //myPro.text = @"%";  
    timer = [NSTimer scheduledTimerWithTimeInterval:0.10 target:self selector:@selector(moreprogress) userInfo:nil repeats:YES];  
    timer = [NSTimer scheduledTimerWithTimeInterval:0.20  target:self selector:@selector(makecount) userInfo:nil repeats:YES];  

}  

-(void)moreprogress{  
    myPro.progress = myPro.progress + 0.02;  
    if(myPro.progress == 1.0){  
        [timer invalidate];  
        [self dismissAlert];  
    }
}  
-(void)makecount{  
    count = count + 4;  
    myCount.text = [[NSString alloc] initWithFormat:@"%i", count];  
    if(count == 100){  
        [timer invalidate];  
    }  
}  
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
    if ([alertView tag] == 1) {  
        if (buttonIndex == 1) {  
            UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:@"Sub-Alert" message:@"You've chosen \"Sure\"." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
            [subAlert show];  
            [subAlert release];  
        }  
        else if (buttonIndex == 2) {  
            UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:@"Sub-Alert" message:@"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
            [subAlert show];  
            [subAlert release];  
        }  
    }   
    else if ([alertView tag] == 2) {  
        userDismissed = YES;  
    }  
}
-(void)dismissAlert{  
    if(userDismissed) return;  
    [self release];  
}  

@end

This is file "Progress_ToolViewController .h":  
  #import <UIKit/UIKit.h>
@class Progress;  
@interface Progress_ToolViewController : UIViewController <UIAlertViewDelegate>{    
    BOOL userDismissed;  
    Progress *prog;  
}
- (void)showAlert;  
@end  

这是文件“Progress_ToolViewController.m”:

 #import "Progress_ToolViewController.h"
 #import "Progress.h"
@implementation Progress_ToolViewController  

- (void)showAlert  
{
    prog = [[Progress alloc] initWithTitle:@"Your Value:" message:@"  " delegate:self cancelButtonTitle:nil okButtonTitle:nil];  
    prog.tag = 2;  
    userDismissed = NO;  
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(dismissAlert) userInfo:nil repeats:NO];  
    [prog show];  
}  

-(void)dismissAlert{  
    if(userDismissed) return;  
    [prog dismissWithClickedButtonIndex:0 animated:YES];  
    [prog release];  
}
- (void)didReceiveMemoryWarning {  
    [super didReceiveMemoryWarning];
}

- (void)viewDidUnload {  

}  

- (void)alertView:(Progress *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {  
    if ([alertView tag] == 1) {  
        if (buttonIndex == 1) {  
            UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:@"Sub-Alert" message:@"You've chosen \"Sure\"." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
            [subAlert show];  
            [subAlert release];  
        }
        else if (buttonIndex == 2) {  
            UIAlertView *subAlert = [[UIAlertView alloc] initWithTitle:@"Sub-Alert" message:@"You've chosen \"Not sure\"." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];  
            [subAlert show];  
            [subAlert release];  
        }
    }
    else if ([alertView tag] == 2) {  
        userDismissed = YES;  
    }  
}  

- (void)dealloc {  
    [super dealloc];  
}  
@end

当我运行这个应用程序时,它也会被解雇,但它会关闭程序。我该如何解决?

1 个答案:

答案 0 :(得分:0)

-(void)showAlert 
{
    prog = [[UIAlertView alloc]initWithTitle:@"Auto alert" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(dismissAlert) userInfo:nil repeats:NO];
    [prog show];
}

-(void)dismissAlert
{
    [prog dismissWithClickedButtonIndex:0 animated:YES];
    TrackingSetting *_obj = [[TrackingSetting alloc]init];
    [self presentViewController:_obj animated:YES completion:nil];  
}

试试这个示例代码它运行正常我已经过测试,在看到错过警报后你可以创建另一个视图对象并显示它没有问题。

但在你的情况下,我认为“prog”是一个视图。所以你必须从superview中删除该视图意味着它取决于你如何使用添加子视图添加该视图来显示该视图,你必须从中删除超级视图。如果您正在使用当前模态视图,则必须关闭该视图。