开发者

auto dismiss alert view and switch view (Xcode)

开发者 https://www.devze.com 2023-02-20 09:36 出处:网络
i\'m working on an app, but i have some problems with auto dismiss alert view and switch to another views.

i'm working on an app, but i have some problems with auto dismiss alert view and switch to another views. Here is my code:

This is file "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  

Here is file "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:del开发者_Go百科egate 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  

This is file "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

When i run this app, it's also dismiss but it turns off the program. How can i solve it?


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

Try this sample code it is working fine i have tested and after diss miss of alert you can create another view object and and show over it no issue.

But in your case i think "prog" is a view .so you have to remove that view from superview means it depends on how you are showing that view if you are adding that view using add sub view you have to remove from super view .if you are using present modal view then you have to dismiss that view .

0

精彩评论

暂无评论...
验证码 换一张
取 消