简体   繁体   中英

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.

您只需禁用视图中的每个子视图;

[self.view setUserInteractionEnabled:NO];

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.

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: 在此输入图像描述

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

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:^{

}];

Great answer by Bartu. Here is the Swift version.

self.view.isUserInteractionEnabled = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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