2011-09-14 58 views
0

首先,我是一個完整的noob,所以請在解釋時溫和。我有一個程序在第一個視圖控制器上加載一個網頁(UIWebview)。第一個視圖控制器檢查是否NSUserDefaults包含用戶名的「jdoe1234」。如果是,則加載第一個網頁,如果它有不同的內容,則加載第二個網頁。我也有一個信息按鈕可以翻到第二個視圖控制器。更新兩個視圖控制器之間的NSUserDefaults

在第二個視圖控制器上,用戶可以輸入用戶名和密碼,然後單擊完成按鈕,將其翻轉回第一個控制器視圖。在完成按鈕代碼中,我正在更新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 

回答

0

這可能是因爲視圖已經被加載並沒有被重新加載不加載到第一頁。您需要在第一個視圖控制器的-viewWillAppear中添加一些內容,以重新檢查NSUserDefaults並正確加載它們。

相關問題