0

我使用 NSUserDefaults 为我的 iPhone 应用程序创建了一个自定义 UISwitch,并添加了一个模式视图控制器 (PincodeSetupViewController) 用于设置 Pincode。以下是用户将看到的信息:

  1. 当用户第一次在 mainViewController 上单击启动应用程序时,显示 UISwitch 已设置为 OFF,因为用户没有设置 Pincode。那是对的。

  2. 下一步是当用户决定将开关打开时,它必须立即显示模态视图控制器 (PincodeSetupViewController)。它从下到上滑动。那是对的。

  3. 现在用户关闭了应用程序并再次打开它。它显示开关已设置为 ON,因为用户这样做是为了设置 Pincode。那是对的。

  4. 用户决定关闭开关并重新打开。当开关再次设置为 ON 时,它会立即显示模态视图控制器 (PincodeSetupViewController)。我在这里遇到的问题是模态视图控制器不会从下到上转换幻灯片,它只是突然出现。这是不正确的。我希望该模态视图控制器将幻灯片从底部过渡到顶部,而不是突然过渡。

有人知道它有什么问题吗?任何建议表示赞赏。

主视图控制器.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 我在 applicationDidEnterBackground 和 applicationWillTerminate 方法中添加了一些代码

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

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

1 回答 1

2

我尽可能地重新创建了您的设置,并且没有出现错误。这是我使用的代码:

//  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

故事板: 在此处输入图像描述

我希望这会有所帮助(请注意,我只是在 IB 中设置了很多您以编程方式创建的东西)

于 2012-07-11T18:02:59.263 回答