using split view how to load web view in detail view - detailsview

how to initialise my detail view with webview when a table row get selected in master view...
any example or any method to solve this problem....
Thanks in advance..

initialize one UIWebview in Detailview('s loadview) and Keep it as hidden. If a row is tapped on master controler then pass that URL to UIWebView and make it as Visible.
All the best!!

in TableView method didSelectRowAtIndexPath:
if (indexPath.row == 0) {
NSURL *urlStrg= [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlStrg];
[self.detailViewController.detailItem loadRequest:urlRequest];
}else if(indexPath.row == 1) {
//do something else here
}

Related

Not able to get the data in a webview in detailview of splitviewcontroller

In my ipad app i'm not able to get the data in a web view. That web view is in a detail view of splitviewcontroller. That data is coming from a link.
I'm using this code.
In split view table did select row at index path
post=[self.postListMutableArray objectAtIndex:indexPath.row];
PostDetailsViewController *postDetails=[[PostDetailsViewController alloc]initWithNibName:#"PostDetailViewController_Ipad" bundle:nil];
postDetails.detailItem=post.postLink;
In split view detail view
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem)
{
_detailItem = newDetailItem;
// Update the view.
NSLog(#"detail Item String is %#",_detailItem);
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
NSString* str =_detailItem;
NSURL* url = [NSURL URLWithString:str];
NSLog(#"URL is ----> %#",url);
self.webView.scalesPageToFit=YES;
[self.webView loadRequest:[NSURLRequest requestWithURL:url]];
//[self.view addSubview:webView];
}
create new Variable at PostDetailsViewController .h class:-
#property(strong,nonatomic)NSString *strGetPostDetail;
and synthesize PostDetailsViewController .m
#synthesize strGetPostDetail
Now you can call splitViewTable Delegate DidSelect method:-
post=[self.postListMutableArray objectAtIndex:indexPath.row];
PostDetailsViewController *postDetails=[[PostDetailsViewController alloc]init];
postDetails.strGetPostDetail=post;

Press a Button and open a URL in another ViewController

I am trying to learn Xcode by making a simple app.
But I been looking on the net for hours (days) and I cant figure it out how I make a button that open a UIWebView in another ViewController :S
first let me show you some code that I have ready:
I have a few Buttons om my main Storyboard that each are title some country codes like UK, CA and DK.
When I press one of those Buttons I have an IBAction like this:
- (IBAction)ButtonPressed:(UIButton *)sender {
// Google button pressed
NSURL* allURLS;
if([sender.titleLabel.text isEqualToString:#"DK"]) {
// Create URL obj
allURLS = [NSURL URLWithString:#"http://google.dk"];
}else if([sender.titleLabel.text isEqualToString:#"US"])
{
allURLS = [NSURL URLWithString:#"http://google.com"];
}else if([sender.titleLabel.text isEqualToString:#"CA"])
{
allURLS = [NSURL URLWithString:#"http://google.ca"];
}
NSURLRequest* req = [NSURLRequest requestWithURL:allURLS];
[myWebView loadRequest:req];
}
How do I make this open UIWebview on my other Viewcontroller named myWebView?
please help a lost man :D
Well, you've firstViewController already designed and coded, now you've to create secondViewController with a UIWebView binded in IB it self, also it have a setter NSString variable like strUrl that you need to pass at the time of pushing or presenting secondViewController, and assign it to UIWebView in viewDidLoad of secondViewController. See my answer about how to pass a NSString? Also UIWebView has its delegates method (What's delegate & datasource methods? - Apple Doc) Which you can use to handle URL request.
These are the delegate of UIWebView, if you'll going to use it, you need to give UIWebView delegate to self.
- (void)webViewDidStartLoad:(UIWebView *)webView;
- (void)webViewDidFinishLoad:(UIWebView *)webView;
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
firstViewController.m
- (IBAction)ButtonPressed:(UIButton *)sender {
NSURL* allURLS;
//get your URL
secondViewControlelr *secondView=[[secondViewControlelr alloc]init];
second.urlToLoad=allURLS;
[self.navigationController pushViewController:secondView animated:YES];
}
secondViewControlelr.h
//declare url and set property
NSURL *urlToLoad;
secondViewControlelr.m
- (void)viewDidLoad
{
myWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 460)];
[self.view addSubview:myWebView];
NSURLRequest *urlReq=[NSURLRequest requestWithURL:self.urlToLoad cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
[myWebView loadRequest:urlReq];
}

iOS: Webview causes flicking while navigating to another controller

I use webview in my UIVIewController and load the local HTML file in it using following method.
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];
If I put this method in ViewDidLoad, I can see white flicking while navigating to my controller that doesn't look good.
I tried putting this method in ViewWillAppear like below. I am using webViewLoaded flag to make sure that webview has been loaded then only show the current view else it waits but it is going in infinite loop of waiting!
- (void)viewWillAppear:(BOOL)animated {
webViewLoaded = NO;
webView.scalesPageToFit = allowZoom;
webView.dataDetectorTypes = UIDataDetectorTypeNone;
webView.delegate = self;
[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:filePath]]];
int count = 0;
while(webViewLoaded == NO)
{
NSLog(#"Waiting...");
[NSThread sleepForTimeInterval:0.1];
if(count++ > 10) break;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)localwebView {
webViewLoaded = YES;
}
I have also tried the same thing in ViewDidLoad but still its going in infinite loop.
Please note that webViewLoaded is "volatile BOOL" but the webview delegate method is not getting called. Not sure what's going on!
Could anyone please help me to fix this. Thanks.
First : You're blocking your main thread and not giving WebView any chance to finish loading. Never block your main thread.
Second : There are two UIWebView delegate methods : webViewDidFinishLoad and webView:didFailLoadWithError: - make sure to listen to both.
If you're trying to wait until WebView completes loading and only show your controller afterwards - use delegation or blocks. Please note that this is not a proper way of doing things - i'm just modifying your example to make it work :
child controller (with WebView) :
-(void)startLoadingWithParent:(id)_parent {
self.parent = _parent;
NSURL * url = [[NSBundle mainBundle] URLForResource:#"resource" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:url]];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView {
[parent showMe:self];
}
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
[parent showError:error];
}
master controller :
-(void)doSomething {
SecondController * ctrl; /// secondController should be created AND has its view loaded at this point
/// ctrl = [SecondController new];
/// ctrl.view;
[ctrl startLoadingWithParent:self];
/// show user that we're doing something - display activity indicator or something
}
-(void)showMe:(UIViewController*)me {
[self.navigationController pushViewControllerAnimated:me];
}
Try using [NSURL fileURLWithPath:filePath]
use Following code for
[self performSelectorInBackground:#selector(webViewLoaded) withObject:nil];
so that it will not affect your UI

iPhone - UIWebView not loading first request

I have a TableViewController with some items, each item when pressed pushes a ViewController with an UIWebView, the ViewController is the same in all cases, but each item asks the webView to load a diferent URL. Now the problem is that the first time I select an item, the webView doesn't load the URL, but if I go back in my navigation controller and then press again any of the items (even the same that the first time), it loads correctly. Why could this be?
This is my code. In my TableViewController I have this inside the didSelectRowAtIndexPath:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *urlAddress;
NSInteger row = [indexPath row];
// SELECT THE URL BASED ON THE SELECTED ROW. SECTION OMMITED FOR CLARITY PURPOSES.
urlAddress = #"http://www.google.com";
[browserViewController refreshUrl:urlAddress];
[urlAddress release];
browserViewController.title = [NSString stringWithFormat:#"%#", [itemsArray objectAtIndex:row]];
MyAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
[delegate.mainNavController pushViewController:browserViewController animated:YES]; }
The browserViewController is initialized in the viewDidLoad method of the TableViewController. In the BrowserViewController implementation I have this method:
- (void) refreshUrl: (NSString *)theUrlAddress{
NSURL *url = [NSURL URLWithString: theUrlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];}
But this is the method that seems to be working only from the second time is invoked. If I set a breakpoint on it, I can see it is actually being invoked the first time I press the item on the table, but the loadRequest does nothing.
BrowserViewController is also an UIWebViewDelegate. The webViewDidStartLoad and webViewDidFinishLoad methods aren't being invoked the first time either.
Any ideas of why and how to fix it? Thanks!
you are using refreshUrl on the viewController before it has created its UIWebView.
Use a breakpoint and check the value of your webview. I think it is nil for the first time.
Simply move [browserViewController refreshUrl:urlAddress]; to the line after the push.

Cannot load alternating urls with UIWebview

I have one view holding a list of videos(buttons which can be clicked, each hold a url) which currently bring up a new view which holds the UIWebview, the only problem is I cant pass the url to the webview depending on which button was clicked.
At the moment I just get an empty UIWebview.
How can the url string be passed to the webview so it can load the correct url for each button?
Regards
NSURL *videoURL = [NSURL URLWithString:#"http://..."];
[webView loadRequest:[NSURLRequest requestWithURL:videoURL]];
UPDATE:
In response to comment #3, here's what you can do:
This goes without saying, but keep a reference to the UIWebView instance in Browser
Add an NSString or NSURL #property to Browser
Pass the URL to the Browser instance right after init
In viewDidLoad:, call loadRequest:
So your code might look like this:
- (void)buttonClicked {
Browser *browser = [[Browser alloc] initWithNibName:nil bundle:nil];
browser.URL = [NSURL URLWithString:#"http..."];
[self presentModalViewController:browser animated:YES];
[browser release];
}
and in Browser's viewDidLoad:
- (void)viewDidLoad {
[super viewDidLoad];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
}