gpt4 book ai didi

iphone - UISwitch 设置为 ON,它不显示模态视图 Controller 转换

转载 作者:行者123 更新时间:2023-11-28 22:53:08 25 4
gpt4 key购买 nike

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

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

  2. 下一步是当用户决定将开关打开并且它必须立即显示模态视图 Controller (PincodeSetupViewController) 时。它从底部滑到顶部。没错。

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

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

有人知道这是怎么回事吗?任何建议表示赞赏。

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 我在 applicationDidEnterBackground 和 applicationWillTerminate 方法中添加了一些代码

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

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

最佳答案

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

//  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: enter image description here

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

关于iphone - UISwitch 设置为 ON,它不显示模态视图 Controller 转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11436584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com