Lothario
Lothario

Reputation: 300

Disable/Freeze view controller

I need to know how to disable all of the action from view controller. In another words, all of the objects in the view controller will be frozen. As a practice, I declare a button and some objects like textfield, slider, etc. Then all of the objects cannot be used when the button pressed.

I need to disable all activities from this view controller.

Upvotes: 5

Views: 4034

Answers (5)

Nilanshu Jaiswal
Nilanshu Jaiswal

Reputation: 1693

Great answer by Bartu. Here is the Swift version.

self.view.isUserInteractionEnabled = false

Upvotes: 0

Rahul Katariya
Rahul Katariya

Reputation: 3628

If you want to disable the navigation bar and tab bar also you can present a transparent view controller. This if freeze your window and won't let user interact with the screen.

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
self.presentedVC = [storyboard instantiateViewControllerWithIdentifier:@"BlankViewController"];
self.presentedVC.view.backgroundColor = [UIColor clearColor];
self.presentedVC.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:self.presentedVC animated:YES completion:nil];

To Enable screen again just dismiss the presentedVC

[self.presentedVC dismissViewControllerAnimated:YES completion:^{

}];

Upvotes: 0

bobnoble
bobnoble

Reputation: 5824

Set up you view structure such that all visible views are contained within another UIView. When you want to disable all "activities" (assuming this means all user interaction), disable user interaction on the outer view. Note that this will also disable the use of the 'Disable UI' button. To allow this button to remain enabled, do not include this in your 'outer view' UIView.

Structure and view controller in storyboard editor: enter image description here

View controller header:

#import <UIKit/UIKit.h>

@interface TestViewController : UIViewController

@property (weak) IBOutlet UIView *outerView;

-(IBAction)disableButtonTapped:(id)sender;

@end

View controller implementation for "disable" button:

#import "TestViewController.h"

@interface TestViewController ()

@end

@implementation TestViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

-(IBAction)disableButtonTapped:(id)sender{

    self.outerView.userInteractionEnabled = NO;
}

@end

Upvotes: 0

Bartu
Bartu

Reputation: 2210

You can simply disable every subview on your view by;

[self.view setUserInteractionEnabled:NO];

Upvotes: 11

Steven
Steven

Reputation: 964

Inside your ViewController.h have some function for the button pressed event:

- (IBAction)signInUser:(id)sender;

Inside your ViewController.m just reference all the controller and disable them:

- (void)signInUser:(id)sender {
    someControl.enabled = NO;
}

Assuming you wired up all your IBOutlet as properties and synthesized them.

Upvotes: 0

Related Questions