1

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?

Kirk Hammett
  • 656
  • 8
  • 24

1 Answers1

0

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];
}
Stas Zhukovskiy
  • 799
  • 9
  • 21
  • I have changed from passing NSURL object to passing url string and added viewWillAppear code as you advised, but no result: loadURL logs right URL and webview URL doesn't change. – Kirk Hammett Dec 23 '13 at 08:16
  • try self.webView instead of webView. – Stas Zhukovskiy Dec 23 '13 at 16:35
  • I have added an update to the question info: webview doesn't change its url because after first loadrequest webview is null and I don't know how to avoid this... – Kirk Hammett Dec 23 '13 at 17:42
  • then don't release it. remove this line: `[webViewController release];` – Stas Zhukovskiy Dec 24 '13 at 07:40
  • I tried, but it does not give a result. I have also checked other elements - I have navigation item on the same page with webview, and it also logs null when I'm calling [webViewController loadURL];. But at the same time if I'm clicking on the URL inside of webview and logging webview or navigation item value, it shows existing object. So webview is unaccessible from another controller. Can you help me with this? – Kirk Hammett Dec 24 '13 at 08:41
  • archive the project and send it to me. – Stas Zhukovskiy Dec 28 '13 at 11:57
  • problem was solved using delegates relatively side menu controller class. – Kirk Hammett Dec 29 '13 at 10:52