I'm new to iOS dev.My project requires me to create app with a left slide drawer,which is supposed to control the main view with a web view inside.
To make them communicated,I create a delegate of KKYViewController .
@protocol KKYViewControllerDelegate <UIWebViewDelegate>
@end
@interface KKYViewController : UIViewController <UIWebViewDelegate,UIApplicationDelegate> {
id<KKYViewControllerDelegate> _delegate;
}
@property (nonatomic, strong) id delegate;
@property (nonatomic, retain)UIWebView* webView;
- (void)handleRefresh:(UIWebView *)webView;
@end
this is my main view controller KKYViewController.m
@implementation KKYViewController
@synthesize webView = _webView;
- (void)viewWillAppear:(BOOL)animated {
self.webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
self.webView.delegate = self;
[self.view addSubview:_webView];
[self loadExamplePage:_webView url:@"http://www.google.com"];
}
- (void)loadExamplePage:(UIWebView*)webView url:(NSString*)strURL {
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
NSLog(@"LoadExamplePage!");
}
- (void)handleRefresh:(UIWebView *)webView {
NSString* strURL = @"http://www.github.com";
NSURL *url = [NSURL URLWithString:strURL];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
NSString *theTitle=[self.webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSLog(@"title:%@",theTitle);
NSLog(@"ReloadPage!");
}
So far it works well,web view looks great.Then I wanna use the delegate from another controller KKYLeftSideDrawerControoler.This is a table and I wanna touch one of the row to make the webview jump~~~
#import "KKYViewController.h"
@interface KKYLeftSideDrawerViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,KKYViewControllerDelegate>
@end
then the KKYLeftSideDrawerController.m
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
KKYViewController* webViewDelegate = [[KKYViewController alloc] init];
webViewDelegate.delegate = self;
switch (indexPath.row) {
case 0:{
UIAlertView* myAlert = [[UIAlertView alloc] initWithTitle:@"Login Successed!" message:@"~~~~" delegate:self cancelButtonTitle:nil otherButtonTitles:@"OK",nil];
[myAlert show];
break;
}
case 1:{ //Here I wanna use this to reload my webview to new url!
[webViewDelegate handleRefresh:webViewDelegate.webView];
NSLog(@"tend to reload");
break;
}
case 2:
break;
case 3:
break;
}
}
Well,the delegate works because when i touch down the case 1 row,my console will Log the info where I put them in the *- (void)handleRefresh:(UIWebView )webView.
There comes the problem:in - (void)handleRefresh,I found that the webview didn't jump and as you can see I tryna log the title of it but resulted null~
I track the log and found the webView I refered in - (void)handleRefresh is nil.I used to think it would be the same webView as above I loaded in *- (void)viewWillAppear:(BOOL)animated *.
This drives me insane for a long while,just want your help and I'll appreciate it.