简体   繁体   中英

UISwitch set to ON and it doesn't show the Modal View Controller transition

I created a custom UISwitch for my iPhone app with NSUserDefaults and added a modal view controller (PincodeSetupViewController) for Pincode to setup. Here is the info the user will see:

  1. When the user clicks to launch the app first time on the mainViewController shows UISwitch is already set to OFF because the user doesn't setup the Pincode. That is correct.

  2. Next step is when the user decides to turn the switch to ON and it must display the modal view controller (PincodeSetupViewController) immediately. It slides from bottom to top. That is correct.

  3. Now the user closed the app and opens it again. It shows the switch set to ON already because the user did this to setup Pincode. That is correct.

  4. The user decided to turn the switch OFF and back to ON. When the switch is set to ON again, it displays the modal view controller (PincodeSetupViewController) immediately. The problem I have here is that the modal view controller doesn't transition slide from bottom to top, it just appears abruptly. This is not correct. I want that modal view controller to transition slide from bottom to top instead of abruptly.

Does anybody know what is wrong with it? Any suggestion appreciated.

mainViewController.m:

- (void)viewDidLoad
{
    [super viewDidLoad];
    customSwitch = [[RCSwitchOnOff alloc] initWithFrame:CGRectMake(1.0, 0.0, 74, 40)];  
    [customSwitchView addSubview:customSwitch];
    [customSwitch addTarget:self action:@selector(togglePincode:) forControlEvents:UIControlEventValueChanged];

}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"pincode"]) {
         NSLog(@"SWITCH MESSAGE: ON");
         [customSwitch setOn:YES];

    } else {
         NSLog(@"SWITCH MESSAGE: OFF");
         [customSwitch setOn:NO];
    }
}

- (void)togglePincode:(UISwitch *)sender {

    if(sender.on){
         NSLog(@"Switch is ON");

         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
         [userDefaults setBool:YES forKey:@"pincode"];
         [userDefaults synchronize];

         //Check to see if the user already setup the Pincode or not.
         if ([[NSUserDefaults standardUserDefaults] boolForKey:@"setupdone_status"]) {
               NSLog(@"Display Pincode Setup");

               [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"setupdone_status"];

               UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
               UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PincodeSetupViewController"];
               [vc setModalPresentationStyle:UIModalPresentationFullScreen];

               [self presentModalViewController:vc animated:YES]; 

         } else {
               NSLog(@"No Pincode Setup");
         }

    } else {
         NSLog(@"Switch is OFF");
         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
         [userDefaults setBool:NO forKey:@"pincode"];
         [userDefaults setBool:YES forKey:@"setupdone_status"];
         [userDefaults synchronize];
    }
}

AppDelegate.m I added some code in applicationDidEnterBackground and applicationWillTerminate methods

    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"pincode"]) {
         [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"setupdone_status"]; 

    } else {
         [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"setupdone_status"];
    }

I recreated your setup as best as I could and didn't get an error. Here is the code I used:

//  ViewController.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
- (IBAction)switchChange:(id)sender;
@property (nonatomic, retain) NSNumber *valueOfSwitchON;
@property (weak, nonatomic) IBOutlet UISwitch *theSwitch;

@end

//  ViewController.m

#import "ViewController.h"

@implementation ViewController
@synthesize valueOfSwitchON;
@synthesize theSwitch;

- (void)viewDidLoad
{
    [super viewDidLoad];
    valueOfSwitchON = [NSNumber numberWithBool:NO];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void) viewDidAppear:(BOOL)animated
{
    if ([valueOfSwitchON boolValue] == YES) [theSwitch setOn:YES];
    else [theSwitch setOn:NO];
}

- (void)viewDidUnload
{
    [self setTheSwitch:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}

- (IBAction)switchChange:(id)sender 
{
    if ([self.valueOfSwitchON boolValue] == NO)
    {      
        [self performSegueWithIdentifier:@"modalSegue" sender:self];
        valueOfSwitchON = [NSNumber numberWithBool:YES];
    }
    else 
    {
        valueOfSwitchON = [NSNumber numberWithBool:NO];
    }
}
@end

//  ViewController2.h

#import <UIKit/UIKit.h>

@interface ViewController2 : UIViewController
- (IBAction)closething:(id)sender;

@end

//  ViewController2.m

#import "ViewController2.h"

@interface ViewController2 ()

@end

@implementation ViewController2

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)closething:(id)sender 
{
    [self dismissModalViewControllerAnimated:YES];
}
@end

Storyboard: 在此输入图像描述

I hope this helps (note that I just set up a lot of things purely in IB that you were creating programmatically)

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