Question

I am new to iOS and I'm trying to make an webview-based app with sidebar drawer and simple navigation (I'm using MFSideMenu library). I'm able to load URL with my webview:

ViewController.h

#import <UIKit/UIKit.h>

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

@property (retain, nonatomic) IBOutlet UIWebView *webView;
- (IBAction)showLeftMenuPressed:(id)sender;
- (void) loadURL;
@property (nonatomic, strong) NSURL *url;

@end

ViewController.m

#import "ViewController.h"
#import "MFSideMenu.h"

@interface ViewController ()
- (void)webViewDidFinishLoad:(UIWebView *)webView;
@end

@implementation ViewController
@synthesize webView;
@synthesize url;

- (void)viewDidLoad
{
    [super viewDidLoad];
    webView.delegate=self;

    if(self.url == Nil) {
        NSString *myURL = @"http://google.com/";
        self.url = [NSURL URLWithString:myURL];
    }

    [self loadURL];
}

-(void)loadURL{
    NSLog(@"loadURL: %@", self.url);
    [self.webView loadRequest:[NSURLRequest requestWithURL:self.url]];
}

When I'm trying to load new URL using following code, I'm receiving right url param value. Another controller is a sidebar (drawer) tableview where I'm selecting an item to change the URL showing in the main view (ViewController which contains webview).

Another controller:

ViewController *webViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
NSString *urlString = @"http://kirk.kh.ua/";
NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSURL *url = [NSURL URLWithString:encodedString];
webViewController.url = url;
//[self.navigationController pushViewController:webViewController animated:YES];
[webViewController loadURL];
[webViewController release];

You may notice that there is a commented line - I have tried this solution, but result is the same: when I'm calling code in another controller nothing happens with the ViewController's webview which is strange because in log output I see that loadURL method was called and it gives me a right URL (kirk.kh.ua).


UPD: I have noticed that on the first load webview as an object, and then it appears to be null (see log):

webview is null after first load

UPD2: I have noticed that webivew object and other synthesized objects are null when I'm calling them from another class, but this objects exist and accessible when I'm logging them from self (ViewController) controller.

Can anyone suggest how can I access ViewController class objects from another class?

Was it helpful?

Solution

Instead of trying to open the URL from the Another Controller - make a NSString property in the ViewController, then when you tap a cell just pass the URL as a string:

ViewController *webViewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
webViewController.urlString = @"http://kirk.kh.ua/";

And then add in the ViewController add something like this in the viewWillAppear:

if (![self.urlString isEqualToString:@""]) {
    NSString *encodedString=[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url = [NSURL URLWithString:encodedString];
    webViewController.url = url;
    [webViewController loadURL];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top