简体   繁体   English

(iOS)自定义初始化UIViewController时出现黑屏

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

This is my first question here. 这是我的第一个问题。 I'm trying to make an app that will work with Core Audio. 我正在尝试制作一款适用于Core Audio的应用。 I found this framework http://theamazingaudioengine.com/ that I'm trying to use and so far I managed to do the first thing in the documentation, which is to play a file. 我发现这个框架http://theamazingaudioengine.com/我正在尝试使用,到目前为止,我设法在文档中做了第一件事,即播放文件。 However, by custom initializing the UIViewController in app's delegate, I lose all its content and the view controller comes black with no other elements. 但是,通过在app的委托中自定义初始化UIViewController,我丢失了所有内容,视图控制器变黑,没有其他元素。

My UIViewController only has one button, which I wanted to use to start the playback of the file, but since I have no access to it, at the moment, the file starts playing when the project builds. 我的UIViewController只有一个按钮,我想用它来开始播放文件,但由于我无权访问它,此刻,文件在项目构建时开始播放。

Any idea what am I doing wrong? 知道我做错了什么吗?

This is my appDelegate: 这是我的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

And my UIViewController: 而我的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

Since you're using a storyboard, you should take all that code out of the app delegate. 由于您正在使用故事板,因此您应该从应用程序委托中获取所有代码。 The storyboard automatically instantiates your initial controller and puts it on screen. 故事板自动实例化您的初始控制器并将其放在屏幕上。 By alloc init'ing one, you're just creating another one that doesn't have any custom view. 通过alloc init'ing,你只需要创建另一个没有任何自定义视图。

To add your audio controller, you should add code in the viewDidLoad method of SoundCheckViewController that does that, rather than in an init method. 要添加音频控制器,您应该在SoundCheckViewController的viewDidLoad方法中添加代码,而不是在init方法中。 This would be the usual way to do this, but I'm not sure what is possible with the framework you're using. 这是通常的方法,但我不确定你正在使用的框架有什么可能。

I think you should initialize your view controller first. 我认为你应该首先初始化你的视图控制器。

- (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