2013-04-11 59 views
0

我從我的pickerviewcontroller傳遞一個NSURL到我的mainviewcontroller。 url取決於pickerviewcontroller中的用戶選擇。它工作正常,但用戶必須在每次重新啓動應用程序進行選擇時重新進入pickerviewcontroller。通過NSURL並保持它

也許代碼會解釋這個問題更好:這是pickerviewcontrollers相關部分...

//The save button on the pickerviewcontroller 
- (IBAction)selectedRow:(id)sender { 

MainViewController *vc1 = [self.storyboard 
instantiateViewControllerWithIdentifier:@"webview"]; 
vc1.destinationweb = selectedRow; 
[self presentViewController:vc1 animated:YES completion:nil]; 

一旦MainViewController的destinationweb日誌顯示用戶選擇在pickerview的地址。但是如果我在應用程序關閉應用程序或向前去(我還有一個視圖控制器),然後回到MainViewController - 它顯示destinationweb = NULL

這是MainViewController相關部分:

- (void)viewDidLoad { 

{ 
    //webView.hidden = YES; 
    self.webView.delegate = self; 
    NSURL *url = destinationweb; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request]; 
    NSLog(@"destinationweb shows= %@", destinationweb); 
} 
[super viewDidLoad]; 
} 

我不知道我應該怎麼做才能使MainViewController記住url直到選擇器視圖中的另一行被選中?

+0

存儲URL在用戶默認 – DharaParekh 2013-04-11 06:24:34

回答

0

只需保存在用戶默認的URL字符串和檢索URL字符串在視圖控制器

- (IBAction)selectedRow:(id)sender 
{ 
    MainViewController *vc1 = [self.storyboard 
    instantiateViewControllerWithIdentifier:@"webview"]; 
    //Instead of passing it just save the url string in user defaults 
    NSString *urlString = [selectedRow absoluteString]; 
    [[NSUserDefaults standardUserDefaults] setObject:urlString forKey:@"UserURL"]; 
    //vc1.destinationweb = selectedRow; 
    [self presentViewController:vc1 animated:YES completion:nil]; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //webView.hidden = YES; 
    self.webView.delegate = self; 
    NSString *urlString = [[NSUserDefaults standardUserDefaults] stringForKey:@"UserURL"]; 
    NSURL *url = [NSURL URLWithString:urlString ]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:request]; 
    NSLog(@"destinationweb shows= %@", destinationweb); 
} 
+0

完美!這就像一個魅力!非常感謝你! – Pierre 2013-04-11 06:53:08

0

如果您希望在應用程序被延遲後保留該值,則應將值(url)存儲在文件中,因此當您重新啓動它時,可以從此處讀取該值。