0

ViewController:

  • initial view controller
  • contains button to switch to SettingsViewController
  • contains UIWebView with an initial homepage URL (value: homepageURL = NSString)

SettingsViewController:

  • acts as the Settings section of the app
  • contains a UITextField: homepageField (default value: http://google.com/)
  • UITextField value: homepageURL

ViewController.h

#import SettingsViewController.h

@interface ViewController : UIViewController <UIWebViewDelegate> {
    UIWebView *webView;
}

@property (nonatomic, retain) IBOutlet UIWebView *webView;

@end

ViewController.m

@synthesize webView;

- (void)viewDidLoad {
    [super viewDidLoad];
    [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:homepageURL]]];
}

SettingsViewController.h

#import "ViewController.h"

@interface SettingsViewController : UITableViewController <UITextFieldDelegate> {
    UITextField *homepageField;
    NSString *homepageURL;
}

@property (nonatomic, retain) IBOutlet UITextField *homepageField;
@property (nonatomic, retain) NSString *homepageURL;

SettingsViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString *homepageURL = homepageField.text;
}

Errors

ViewController.m: [webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:homepageURL]]]; - Use of undeclared identifier 'homepageURL'

Question How do I retrieve the set NSString value homepageURL for loading in the UIWebView when the app loads up? - In other words, act as a homepage. I am not able to get this code working.

iPatel
  • 46,010
  • 16
  • 115
  • 137
JDev
  • 5,168
  • 6
  • 40
  • 61

2 Answers2

1

You need to create an instance of SettingsViewController in order to get at the string. It is not loaded automatically from the xib/storyboard as it is not the initial/root view controller. If loading from a storyboard, you should give it's scene a storyboard ID (say "settingsVC") so that you can access it, then

SettingsViewController* settingsVC =  
      [self.storyboard instantiateViewControllerWithIdentifier:@"settingsVC"];

Then you can:

      [webView loadRequest:[NSURLRequest requestWithURL:
                [NSURL URLWithString:settingsVC.homepageURL]]];

However this is a very poor way to store a default string, as you have to load an entire storyboard scene just to get at the value. There are many better ways to store data. For example this is just the kind of thing that NSUserDefaults is good for:

    NSString* homePageURL;
    NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];

      //reading
    homepageURL = [defaults stringForKey:@"homepageURLKey"];

      //writing
    [defaults setString:homepageURL forKey:@"homepageURLKey"];

NSUserDefaults manages persistent data, so any changes will be retained between launches of your app.

As this is a default string, you could set it in your initial viewController's viewDidLoad if there isn't a saved one in userDefaults. Your settings viewController can be configured to read in it's data from NSUserDefaults on viewDidLoad/viewWillAppear, and write out changes as they are made and/or on viewWillDisappear.

foundry
  • 31,615
  • 9
  • 90
  • 125
1

You can use NSUserDefault for saving homepageURL in SettingsViewController, and then get it in your ViewController any time, when you want

to set use

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:homepageField.text forKey:@"homepageURL"];

and to get

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *homepageURL = [defaults objectForKey:@"homepageURL"];
Ruben
  • 290
  • 3
  • 14