2011-08-26 202 views
-2

好吧,我有5次。其中一個視圖叫做RecordViewController。 (RecordViewController導致錯誤) 我可以很好地切換視圖。但是一旦我進入了recordviewcontroller,我可以切換到另一個視圖。但是,如果我想切換回recordviewcontroller。它拋出我出我的應用程序,並給了我這個錯誤:程序接收到的信號:「EXC_BAD_ACCESS」

Program received signal: 「EXC_BAD_ACCESS」. 
Data Formatters temporarily unavailable, will re-try after a 'continue'. (The program being debugged was signaled while in a function called from GDB. 
GDB has restored the context to what it was before the call. 
To change this behavior use "set unwindonsignal off" 
Evaluation of the expression containing the function (gdb_objc_startDebuggerMode) will be abandoned.) 

這裏是recordviewcontroller代碼 - 伊夫刪除切換視圖代碼,因爲它沒有必要的。

@implementation RecordViewController 
@synthesize actSpinner, btnStart, btnPlay; 



-(void)countUp { 

    mainInt += 1; 
    seconds.text = [NSString stringWithFormat:@"%02d", mainInt]; 

} 



static RecordViewController *sharedInstance = nil; 


+ (RecordViewController*)sharedInstance { 
    if (sharedInstance == nil) { 
     sharedInstance = [[super allocWithZone:NULL] init]; 
    } 

    return sharedInstance; 
} 


+ (id)allocWithZone:(NSZone*)zone { 
    return [[self sharedInstance] retain]; 
} 


- (id)copyWithZone:(NSZone *)zone { 
    return self; 
} 


- (id)retain { 
    return self; 
} 


- (NSUInteger)retainCount { 
    return NSUIntegerMax; 
} 


- (void)release { 

} 


- (id)autorelease { 
    return self; 
} 

- (void)viewDidLoad { 
    [super viewDidLoad]; 


    toggle = YES; 
    btnPlay.hidden = YES; 


    AVAudioSession * audioSession = [AVAudioSession sharedInstance]; 

    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error: &error]; 

    [audioSession setActive:YES error: &error]; 

} 





- (IBAction) start_button_pressed{ 


    if(toggle) 
    { 
     toggle = NO; 
     [actSpinner startAnimating]; 
     [btnStart setImage:[UIImage imageNamed:@"recordstop.png"] forState:UIControlStateNormal]; 
     mainInt = 0; 
     theTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(countUp) userInfo:nil repeats:YES]; 


     btnPlay.enabled = toggle; 
     btnPlay.hidden = !toggle; 


     NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init]; 
     [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey]; 
     [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey]; 
     [recordSetting setValue:[NSNumber numberWithInt: 2] forKey:AVNumberOfChannelsKey]; 

     recordedTmpFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]]; 
     NSLog(@"Using File called: %@",recordedTmpFile); 

     recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error]; 
     [recorder setDelegate:self]; 
     [recorder prepareToRecord]; 

     [recorder record]; 


    } 
    else 
    { 
     toggle = YES; 
     [actSpinner stopAnimating]; 
     [btnStart setImage:[UIImage imageNamed:@"recordrecord.png"] forState:UIControlStateNormal]; 
     btnPlay.enabled = toggle; 
     btnPlay.hidden = !toggle; 
     [theTimer invalidate]; 

     NSLog(@"Using File called: %@",recordedTmpFile); 

     [recorder stop]; 
    } 
} 

- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

-(IBAction) play_button_pressed{ 


    AVAudioPlayer * avPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error]; 
    [avPlayer prepareToPlay]; 
    [avPlayer play]; 

} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight); 
} 

- (void)viewDidUnload { 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
    //Clean up the temp file. 
    NSFileManager * fm = [NSFileManager defaultManager]; 
    [fm removeItemAtPath:[recordedTmpFile path] error:&error]; 
    //Call the dealloc on the remaining objects. 
    [recorder dealloc]; 
    recorder = nil; 
    recordedTmpFile = nil; 
} 

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

@end 

感謝您的幫助!

+1

嘗試[啓用殭屍](http://stackoverflow.com/questions/2190227/how-do-i-set-nszombieenabled-in-xcode-4)... – jtbandes

+1

你的代碼會更容易閱讀,如果你有更好的格式(空白行少,模板沒有評論) – vikingosegundo

+0

哦,對不起。我會修復 – LA12

回答

0

聽起來像是你不留住recordViewController,所以當你把另一種觀點認爲,它的釋放,當您嘗試回去,這是不存在了。

如果你不使用ARC,保留它,當你創建它。

+0

ARC沒有被使用,哪部分我需要特別保留? – LA12

+0

如果你沒有使用弧,那麼很多對象都在使用你的代碼。檢查內存管理指南。 – vikingosegundo

0

拆下實現共下列方法:

+ (id)allocWithZone:(NSZone*)zone 

- (id)copyWithZone:(NSZone *)zone 

- (id)retain 

- (NSUInteger)retainCount 

- (void)release 

並改變sharedInstance執行以下,並且還添加以下所示的alloc的實施。

+(RecordViewController *) sharedInstance { 
@synchronized([RecordViewController class]) 
{ 
    if (!sharedInstance) 
    { 
     [[self alloc]init]; 
    } 
    return sharedInstance; 
} 
return nil; 
} 

+(id) alloc 
{ 
    @synchronized([RecordViewController class]) 
    { 
     NSAssert(sharedInstance == nil, @"Attempted to allocated second singleton RecordViewController"); 
     sharedInstance = [super alloc]; 
     return sharedInstance; 
    } 
    return nil; 
} 
+0

嗨,我得到這個錯誤。 ***在+ [RecordViewController ALLOC],類/ RecordViewController.m斷言故障:111 ***終止應用程序由於未捕獲的異常「NSInternalInconsistencyException」,原因:「已嘗試分配第二單RecordViewController」 ***調用堆棧在第一次投擲: – LA12

相關問題