简体   繁体   English

如何在iOS中拥有永久视图?

[英]How can I have a persistent view in iOS?

We are using a UInavigationController with one of the views being one that plays soundtracks. 我们正在使用UInavigationController,其中一个视图是播放音轨的视图。

We are using the AVfountation framework. 我们正在使用AVfountation框架。

We navigate to audiolist(where music is played) on button click 单击按钮,我们导航到音频列表(播放音乐的位置)

-(IBAction)audioBtnClicked
{
    audiolist *audio=[[audiolist alloc] initWithNibName:@"audiolist" bundle:nil];
    [self.navigationController pushViewContrller:audio animated:YES];
    [audiolist release];
}

When the user plays music and navigates away, the music continues playing 当用户播放音乐并导航时,音乐继续播放

Problem: when the user navigates back to the list of songs and plays another track, 2 songs are playing at the same time. 问题:当用户导航回到歌曲列表并播放另一首曲目时,正在同时播放2首歌曲。

We think that a new instance of audiolist is created everytime the user navigates back. 我们认为,每当用户向后导航时, audiolist创建一个新的audiolist实例。 We would like to only have one instance of audiolist . 我们只希望有一个audiolist实例。 How do we make the first instance of audiolist persistent and how do we refer back to it? 我们如何使audiolist的第一个实例持久化,以及如何重新引用它?

You have to do 你必须做

[audio release];

and not 并不是

[audiolist release];

audiolist seems to be your class name. 音频列表似乎是您的班级名称。 The naming conventions says that classes should be capital and camel-cased, so it should be AudioList. 命名约定说,类应使用大写字母和驼峰式,因此应使用AudioList。 It'll make your code more readable. 这将使您的代码更具可读性。

To have one single shared instance of your AudioList, you could do this: 要拥有一个AudioList的单个共享实例,可以执行以下操作:

Add a class method to your header: 在您的标头中添加一个类方法:

+ (AudioList *) sharedInstance;

then add this to your implementation file: 然后将其添加到您的实现文件中:

@implementation AudioList

static AudioList *gSharedInstance = nil;

+ (AudioList *) sharedInstance {
    if (gSharedInstance == nil) {
        gSharedInstance = [[AudioList alloc] init];
    }

    return gSharedInstance;
}

Now you can always access that instance with 现在,您随时可以使用

[AudioList sharedInstance];

Cheers. 干杯。

Create a singleton AudioManager class that handles all your music playback. 创建一个处理所有音乐播放的单例AudioManager类。 When you navigate to that view controller, grab the shared instance of that AudioManager to do whatever you do in that view. 当您导航到该视图控制器时,请抓住该AudioManager的共享实例以在该视图中执行任何操作。

You don't want the view to persist as that would be bad separation of MVC. 您不希望该视图持久存在,因为这将导致MVC分离不良。

Apple has some discussion of different design patterns and their objective-c implementations, including the singleton: 苹果公司讨论了不同的设计模式及其target-c实现,包括单例:

http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW6 http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaDesignPatterns/CocoaDesignPatterns.html#//apple_ref/doc/uid/TP40002974-CH6-SW6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM