簡體   English   中英

(iOS)自定義初始化UIViewController時出現黑屏

[英](iOS) Black screen when custom initializing UIViewController

這是我的第一個問題。 我正在嘗試制作一款適用於Core Audio的應用。 我發現這個框架http://theamazingaudioengine.com/我正在嘗試使用,到目前為止,我設法在文檔中做了第一件事,即播放文件。 但是,通過在app的委托中自定義初始化UIViewController,我丟失了所有內容,視圖控制器變黑,沒有其他元素。

我的UIViewController只有一個按鈕,我想用它來開始播放文件,但由於我無權訪問它,此刻,文件在項目構建時開始播放。

知道我做錯了什么嗎?

這是我的appDelegate:

@implementation SoundCheckAppDelegate

@synthesize window = _window;
@synthesize audioController = _audioController;
@synthesize viewController = _viewController;


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{


    // Create an instance of the audio controller, set it up and start it running
    self.audioController = [[[AEAudioController alloc] initWithAudioDescription:[AEAudioController nonInterleaved16BitStereoAudioDescription] inputEnabled:YES] autorelease];
    _audioController.preferredBufferDuration = 0.005;
    [_audioController start:NULL];

    // Create and display view controller
    self.viewController = [[SoundCheckViewController alloc] initWithAudioController:_audioController];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];

    return YES;
}

@end

而我的UIViewController:

@interface SoundCheckViewController ()

@property (nonatomic, strong) AEAudioController *audioController;
@property (nonatomic, strong) AEAudioFilePlayer *loop;

@end

@implementation SoundCheckViewController

- (id)initWithAudioController:(AEAudioController*)audioController {
    self.audioController = audioController;

    NSError *error;

    NSURL *file = [[NSBundle mainBundle] URLForResource:@"Southern Rock Drums" withExtension:@"m4a"];
    self.loop = [AEAudioFilePlayer audioFilePlayerWithURL:file
                                          audioController:_audioController
                                                    error:&error];
    if(error)
        NSLog(@"couldn't start loop");

    _loop.removeUponFinish = YES;
    _loop.loop = YES;
    _loop.completionBlock = ^{
        self.loop = nil;
    };

    [_audioController addChannels:[NSArray arrayWithObject:_loop]];

    return self;
}



@end

由於您正在使用故事板,因此您應該從應用程序委托中獲取所有代碼。 故事板自動實例化您的初始控制器並將其放在屏幕上。 通過alloc init'ing,你只需要創建另一個沒有任何自定義視圖。

要添加音頻控制器,您應該在SoundCheckViewController的viewDidLoad方法中添加代碼,而不是在init方法中。 這是通常的方法,但我不確定你正在使用的框架有什么可能。

我認為你應該首先初始化你的視圖控制器。

- (id)initWithAudioController:(AEAudioController*)audioController {
    // THIS LINE IS MISSING IN YOUR CODE
    self = [super initWithNibName:@"SoundCheckViewController" bundle:nil];
    if ( self ) {
       self.audioController = audioController;
       ...
    }

    return self;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM