更新两个视图控制器之间的 NSUserDefaults

发布于 2024-12-04 17:45:34 字数 5206 浏览 0 评论 0原文

首先,我是一个十足的菜鸟,所以请在解释时保持温和。我有一个程序可以在第一个视图控制器上加载网页(UIWebview)。第一个视图控制器检查 NSUserDefaults 是否包含用户名“jdoe1234”。如果是,则加载第一个网页,如果有不同,则加载第二个网页。我还有一个信息按钮,可以翻转到第二个视图控制器。

在第二个视图控制器上,用户可以输入用户名和密码,然后单击“完成”按钮,将它们翻转回第一个控制器视图。在完成按钮代码中,我正在更新 NSUserDefaults 以包含用户名和密码文本框值。

我的问题是,当用户翻回第一个视图控制器时,网页不会刷新该人刚刚输入并保存的值。当我关闭程序并再次打开它时,新值会正确显示。我搜索并尝试了不同的例子,但我无法让它工作。我在下面列出了我的代码。我很感谢对此的任何帮助。

FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface FirstViewController : UIViewController {

    IBOutlet UIWebView *loadInitialGradebook;
    IBOutlet UILabel *labelUserName;

}

- (IBAction)infoButtonPressed:(id)sender;
-(IBAction)refreshWebView;
@property (nonatomic, retain) UILabel *labelUserName;
@end

FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"


@implementation UACCHViewController
@synthesize labelUserName;


- (IBAction)infoButtonPressed:(id)sender;
{
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;    
    [self presentModalViewController:second animated:YES];
}

- (IBAction)refreshWebView {

   [loadInitialWebView reload];
}           



// Start Strip URL of spaces & funky characters
- (NSString *)urlEncodeValue:(NSString *)str
{
    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
 return [result autorelease];
}
// End Strip URL of spaces & funky characters



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

    labelUserName.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldUserName"];

    // Start Load WebView 

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"txtFieldUserName"]) {

    [loadInitialWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];        
    } 
    else {    

    NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",[self urlEncodeValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldUserName"]],[self urlEncodeValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldPassWord"]]];                    

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://www.mysite.com/page.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    [loadInitialWebView loadRequest: request];

 }
// End Load WebView



[super viewDidLoad];
}

@end

SecondViewController.h

#import <UIKit/UIKit.h>

NSUserDefaults *prefs;

@interface SecondViewController : UIViewController {

    IBOutlet UITextField *txtUserName;
    IBOutlet UITextField *txtPassWord;   

}

@property (nonatomic, retain) UITextField *txtUserName;
@property (nonatomic, retain) UITextField *txtPassWord;
@property (nonatomic, retain) NSUserDefaults *prefs;

- (IBAction)doneButtonPressed:(id)sender;   


@end

SecondViewController.m   

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@implementation SecondViewController
@synthesize txtUserName;
@synthesize txtPassWord;
@synthesize prefs;


// Start Done Button Pressed
- (IBAction)doneButtonPressed:(id)sender;
{      
    [self dismissModalViewControllerAnimated:YES];   

    [prefs setObject:txtUserName.text forKey:@"txtFieldUserName"];
    [prefs setObject:txtPassWord.text forKey:@"txtFieldPassWord"];
    [[NSUserDefaults standardUserDefaults] synchronize];  

}
// End Done Button Pressed


// Start Dismiss the keyboard when a user selects the return key
- (BOOL) textFieldShouldReturn: (UITextField *) theTextField
{
[theTextField resignFirstResponder];

return YES;
}
// End Dismiss the keyboard when a user selects the return key

- (void)viewDidLoad
{
    self.prefs = [NSUserDefaults standardUserDefaults];


//Start (Code to set a default username,password)

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"txtFieldUserName"]) {

        txtUserName.text = @"jdoe1234";
    } 
    else {
        txtUserName.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldUserName"];
    }

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"txtFieldPassWord"]) {

        txtPassWord.text = @"01012011";
    } 
    else {
        txtPassWord.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldPassWord"];
    }        

    // End (Code to set a default username,password)

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.  

}
@end

First off, I am a complete noob, so please be gentle when explaining. I have a program that loads a webpage (UIWebview) on the first view controller. The first view controller checks to see if the NSUserDefaults contains "jdoe1234" for the UserName. If it does, it loads the first webpage, if it has something different, it loads a second webpage. I also have an information button that flips to the second view controller.

On the second view controller, a person can type in a username and password and click the Done button which flips them back to the first controller view. Inside the done button code, I am updating the NSUserDefaults to include the username and password textbox values.

My problem is when the person flips back to the first view controller, the webpage is not refreshing with the values that the person just typed in and saved. When I close the program and open it again, the new values show correctly. I have searched and tried different examples, but I am unable to get it to work. I have listed my code below. I appreciate any help on this.

FirstViewController.h
#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@interface FirstViewController : UIViewController {

    IBOutlet UIWebView *loadInitialGradebook;
    IBOutlet UILabel *labelUserName;

}

- (IBAction)infoButtonPressed:(id)sender;
-(IBAction)refreshWebView;
@property (nonatomic, retain) UILabel *labelUserName;
@end

FirstViewController.m
#import "FirstViewController.h"
#import "SecondViewController.h"


@implementation UACCHViewController
@synthesize labelUserName;


- (IBAction)infoButtonPressed:(id)sender;
{
    SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];

    second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;    
    [self presentModalViewController:second animated:YES];
}

- (IBAction)refreshWebView {

   [loadInitialWebView reload];
}           



// Start Strip URL of spaces & funky characters
- (NSString *)urlEncodeValue:(NSString *)str
{
    NSString *result = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)str, NULL, CFSTR("?=&+"), kCFStringEncodingUTF8);
 return [result autorelease];
}
// End Strip URL of spaces & funky characters



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{

    labelUserName.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldUserName"];

    // Start Load WebView 

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"txtFieldUserName"]) {

    [loadInitialWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com"]]];        
    } 
    else {    

    NSString *post = [NSString stringWithFormat:@"username=%@&password=%@",[self urlEncodeValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldUserName"]],[self urlEncodeValue:[[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldPassWord"]]];                    

    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:@"http://www.mysite.com/page.php"]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];

    [loadInitialWebView loadRequest: request];

 }
// End Load WebView



[super viewDidLoad];
}

@end

SecondViewController.h

#import <UIKit/UIKit.h>

NSUserDefaults *prefs;

@interface SecondViewController : UIViewController {

    IBOutlet UITextField *txtUserName;
    IBOutlet UITextField *txtPassWord;   

}

@property (nonatomic, retain) UITextField *txtUserName;
@property (nonatomic, retain) UITextField *txtPassWord;
@property (nonatomic, retain) NSUserDefaults *prefs;

- (IBAction)doneButtonPressed:(id)sender;   


@end

SecondViewController.m   

#import <UIKit/UIKit.h>
#import "SecondViewController.h"


@implementation SecondViewController
@synthesize txtUserName;
@synthesize txtPassWord;
@synthesize prefs;


// Start Done Button Pressed
- (IBAction)doneButtonPressed:(id)sender;
{      
    [self dismissModalViewControllerAnimated:YES];   

    [prefs setObject:txtUserName.text forKey:@"txtFieldUserName"];
    [prefs setObject:txtPassWord.text forKey:@"txtFieldPassWord"];
    [[NSUserDefaults standardUserDefaults] synchronize];  

}
// End Done Button Pressed


// Start Dismiss the keyboard when a user selects the return key
- (BOOL) textFieldShouldReturn: (UITextField *) theTextField
{
[theTextField resignFirstResponder];

return YES;
}
// End Dismiss the keyboard when a user selects the return key

- (void)viewDidLoad
{
    self.prefs = [NSUserDefaults standardUserDefaults];


//Start (Code to set a default username,password)

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"txtFieldUserName"]) {

        txtUserName.text = @"jdoe1234";
    } 
    else {
        txtUserName.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldUserName"];
    }

    if (![[NSUserDefaults standardUserDefaults] objectForKey:@"txtFieldPassWord"]) {

        txtPassWord.text = @"01012011";
    } 
    else {
        txtPassWord.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"txtFieldPassWord"];
    }        

    // End (Code to set a default username,password)

[super viewDidLoad];
// Do any additional setup after loading the view from its nib.  

}
@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈情不如逗狗 2024-12-11 17:45:34

它可能没有加载到第一页,因为视图已经加载并且没有重新加载。您需要在第一个视图控制器中的 -viewWillAppear 中添加一些内容,以重新检查 NSUserDefaults 并适当地加载它们。

It probably isn't loading into the first page because the view has already been loaded and isn't being reloaded. You would need to add something to your -viewWillAppear in the first view controller to recheck the NSUserDefaults and load them appropriately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文