Adam G
Adam G

Reputation: 1188

Objective-C : Keep a View Controller active when dismissed?

In my chatting application, I have a ChatViewController.m that allows users to message with the QuickBlox framework.

When a user sends an image, a background upload begins and a UIProgressView displays the progress of the upload.

But what if the user backs out of that view during the upload, and returns in, say, 10 seconds while the upload is still happening. I want the UIProgressView to still be active and accurate based on that upload. But dismissing the ViewController doesn't allow me to do that.

Can someone suggest what I should be doing in this situation?

EDIT: This is how I present the ChatViewController.m, depending on the chat selected from the CollectionView:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if([segue.destinationViewController isKindOfClass:ChatViewController.class]){

        ChatViewController *destinationViewController = (ChatViewController *)segue.destinationViewController;

        if(self.createdDialog != nil){
            destinationViewController.dialog = self.createdDialog;
            self.createdDialog = nil;
        }else{

            QBChatDialog *dialog = [ChatService shared].dialogs[((UICollectionViewCell *)sender).tag];

            AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
            appDelegate.dialog = dialog;

        }
    }
}

EDIT 2: I have implemented the ViewController as a singleton in my didSelectItemAtIndexPath. But now, the app presents only a black screen.

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {

        QBChatDialog *dialog = [ChatService shared].dialogs[indexPath.row];

        ChatViewController *chatView = [[ChatViewController alloc] init];
        chatView.dialog = dialog;
        [self presentViewController:chatView animated:YES completion:nil];

}

Upvotes: 1

Views: 195

Answers (2)

Duncan C
Duncan C

Reputation: 131426

You should change the way you display your chat view controller. Don't use a segue.

Instead, set up your chat view controller as a singleton. Set up an IBAction or other code triggered by the user selecting an item in your collection view.

In that IBAction or didSelectItem code, fetch a reference to your singleton, configure it as needed, and present it modally yourself using presentViewController:animated:completion:

That way your view controller will preserve it's contents between views.

As the other poster said in a comment, you might pull the download logic out of your view controller and into a separate download manager class. It depends on whether you need the ability to do asynchronous downloads in places other than your chat view controller.

Upvotes: 1

Fennelouski
Fennelouski

Reputation: 2431

I'm assuming based on your question that you are creating the view controller each time that you are presenting it. Instead of re-allocating and creating a new view controller each time you present the messaging view, make only one view controller that you can call and present from any other view controller.

A couple possible ways to do this are:

  1. Create a singleton that has the messaging view controller as a property

  2. Add the messaging view controller as a property on your route view controller

  3. Make the messaging view a singleton itself so only one gets created in the entire life of the application

Doing any of these will make sure that the view persists each time that it's dismissed.

If that still doesn't fix the problem, you may be resetting the view in viewDidLoad or viewDidAppear which I don't think is actually what you want to do.

Upvotes: 0

Related Questions