2011-03-28 66 views
1

我正在應用程序,但我有一些問題與自動解僱警報視圖和切換到另一個視圖。 這裏是我的代碼:自動關閉警報視圖和切換視圖(Xcode)

這是文件 「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 

當我運行這個應用程序時,它也關閉,但它關閉程序。我如何解決它?

回答

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」是一個視圖。所以你必須從超級視圖中刪除該視圖意味着它取決於你如何顯示該視圖,如果你添加視圖使用添加子視圖你必須刪除從超級視圖。如果你使用現在的模態視圖,那麼你必須消除該視圖。