0

我有一个viewController调用AudioViewController。在这方面,我有这个方法:从类方法访问UINavigationController

- (IBAction)stopAction:(id)sender 
{ 
    [self.audioClass stop]; 
} 

我有一个NSObject类叫做AudioClass。在
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex

-(void) stop 
{ 
    if (!recorder.recording) 
    { 
     [player stop]; 
     player.currentTime = 0; 

     [self.recordOrPauseButton setEnabled:YES]; 
     [self.stopButton setEnabled:NO]; 

     [self alertMessage]; 
    } 
    else 
    { 
     [recorder stop]; 

     AVAudioSession *audioSession = [AVAudioSession sharedInstance]; 
     [audioSession setActive:NO error:nil]; 
     [self alertMessage]; 
    } 
} 


-(void) alertMessage 
{ 
    UIAlertView *stopAlert = [[UIAlertView alloc] initWithTitle: @"Done!" 
                 message: @"Do you want to save it?" 
                 delegate: self 
               cancelButtonTitle:@"Cancle" 
               otherButtonTitles:@"Save", nil]; 
    [stopAlert show]; 
} 


- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger) buttonIndex 
{ 
    if (buttonIndex == 0) 
    { 
     // this is the cancel button 
    } 
    else 
    { 
     [self.sqLiteDB insertDataIntoTable:soundFilePath And:outputFileURL]; 
     [self.navigationController popViewControllerAnimated:YES]; 
    } 
} 

现在,这是不承认的
[self.navigationController popViewControllerAnimated:YES];:在这方面,我有这些方法。
因为,它在NSObject类中。我如何访问navigationController并从AudioViewControlelr返回到之前的ViewController

如果任何人有任何解决方案,请与我分享。提前致谢。祝你有美好的一天。

+0

你不能只是在viewController里面移动AlertView逻辑吗? – 2014-11-04 16:04:47

+0

@GonjiDev感谢您的评论。我不能那样做。因为' - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player成功:(BOOL)flag'这个方法也使用相同的'[self alertMessage]''方法。这是'AudioClass'中。 – Tulon 2014-11-04 16:46:30

+0

以下@CleverError回答你应该通知viewController当 - (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)播放器成功:(BOOL)flag'被调用。你可以通过各种方式实现这一点,可以使用NSNotificationCenter。显示警报是viewControllers的责任。 – 2014-11-04 16:53:40

回答

1

iOS开发基于Model View Contorller design pattern。你的AudioViewControlelr显然是一个控制器,你的AudioClass实际上是一个模型。在MVC中,模型不应该直接修改视图,这包括修改UINavigationControllers并显示UIAlertViews

对于您的情况,您的AudioViewController应呼叫停止在AudioClass对象上并显示UIAlertView


在一个侧面说明,AudioRecorder会比AudioClass一个更好的名字。它更准确地描述了班级的作用,而不是仅仅告诉我们这是我们已经知道的班级。