ios - (iOS)自定义初始化UIViewController时黑屏

标签 ios uiviewcontroller screen designated-initializer

这是我的第一个问题。我正在尝试制作一个可与 Core Audio 配合使用的应用程序。我找到了这个框架 http://theamazingaudioengine.com/我正在尝试使用,到目前为止,我设法完成了文档中的第一件事,即播放文件。但是,通过在应用程序的委托(delegate)中自定义初始化 UIViewController,我丢失了它的所有内容,并且 View Controller 变成黑色,没有其他元素。

我的 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

最佳答案

由于您使用的是 Storyboard,因此您应该将所有代码从应用委托(delegate)中取出。 Storyboard会自动实例化您的初始 Controller 并将其显示在屏幕上。通过分配初始化一个,您只是在创建另一个没有任何自定义 View 的。

要添加音频 Controller ,您应该在执行该操作的 SoundCheckViewController 的 viewDidLoad 方法中添加代码,而不是在 init 方法中。这将是执行此操作的常用方法,但我不确定您使用的框架可以实现什么。

关于ios - (iOS)自定义初始化UIViewController时黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240708/

相关文章:

ios - 我的工具栏不会显示在 webView 上方

macos - Unity ScreenToWorldPoint 在 Mac 上表现不同(与 PC 相比)?

iphone - 获取iPhone/iPad屏幕上显示的文字

ios - UITableView 在使用 Swift 中的 Master Detail App 滚动之前不会重新加载

ios - 将 UIBezierPath 转换为 UIImage

iPhone 如何将 View Controller 的 View 添加到另一个 View Controller 的 View ?

ios - 容器 Controller 和 iOS 6 轮换

html - 调整到屏幕高度的图像高度

objective-c - NSFileManager moveItemAtPath :toPath:error: changes filename

ios - UIImageJPEGRepresentation 使用大量内存(Swift 3.0)