开发者

update NSUserDefaults between two view controllers

开发者 https://www.devze.com 2023-04-05 02:32 出处:网络
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 NSUse

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)doneButton开发者_Python百科Pressed:(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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号