I have a modal view controller that I am trying to present a web view in it. Tthe first time it appears, it shows up as blank. When I close it out and click it again, however, it displays fine. I'm thinking it may be an issue with the loading of the webView, but I've tried displaying it only when the webView finishes loading, but it never gets called then.
NSURL* newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webViewController.webView loadRequest: newURLRequest];
[newURL release];
[newURLRequest release];
webViewController.modalPresentationStyle=UIModalPresentationPageSheet;
[self presentModalViewController:webViewController animated:YES];
The webView control won't be accessible the first time until the first present call causes controls to be loaded and initialized. Before the first present, webViewController.webView will be nil and so calling loadRequest on it will do nothing.
You could move the loadRequest call after the presentModalViewController call.
But instead of accessing controls in view controller's views directly, it'd be better to declare the url string as a NSString property (called say urlString) in WebViewController and set it to fileString before the presentModalViewController call (and don't create the NSURL, etc there):
webViewController.urlString = fileString;
[self presentModalViewController:webViewController animated:YES];
Finally, in WebViewController, in viewWillAppear: or viewDidAppear:, create the NSURL (using urlString), create the NSURLRequest, and call loadRequest.
Related
I have a strange problem and I have no idea how to solve it. I have a root viewcontroller. Via button you can switch to the child viewcontroller. In the child view controller I open a link like this as a request to the webview:
myURL = [NSURL URLWithString:#"http://m.imdb.com/video/imdb/vi2284695065/"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[myWebView loadRequest:myRequest];
the video on the page is autoplaying and switching to fullscreen mode. In exactly this moment I get a log message which indicates that the viewdidload method on my rootviewcontroller is called. Which eventually makes my code not working anymore. But why? Its not the viewdidload of the current viewcontroller its the one of the rootviewcontroller?! I donĀ“t know what kind of code I should provide actually...
Edit
I switch to viewcontroller like this:
UIViewController *NVC = [self.storyboard instantiateViewControllerWithIdentifier:#"child"];
[self presentViewController:NVC animated:NO completion:nil];
The rootviewcontroller does not get a received memory warning.
I have a non storyboard app that has a UITableView with a bunch of cells, upon clicking a cell I push a view controller with a UIWebView, the local html file is loaded based on the cell tapped.
If this UIWebView then has other links in it, I use the UIWebView delegate method shouldStartLoadWithRequest along with this code:
if(navigationType == UIWebViewNavigationTypeLinkClicked) {
//When a html link is clicked in uiwebview
NSURL *url = request.URL;
NSString *urlString = url.absoluteString;
//Get components of url
NSArray *urlComponents = [urlString componentsSeparatedByString:#"/"];
//Get last component which should be wanted file name
NSString *wantedLink = [urlComponents objectAtIndex:[urlComponents count]-1];
//Get the url without the dot
NSArray *fileComponents = [wantedLink componentsSeparatedByString:#"."];
NSString *wantedFile = [fileComponents objectAtIndex:0];
ALSDetailViewController *superDetail = [[ALSDetailViewController alloc] initWithNibName:#"ALSProtocolDetailViewController" bundle:nil];
self.alsProtocolDetailViewController = superDetail;
//Check for each wanted file name to see if it matches another file
if ([wantedLink isEqualToString:#"VTachPulseStable.html"]) {
//Open different protocol xib with html file name as parameter, and push the view in
self.alsProtocolDetailViewController.title = #"Ventricular Tachycardia With Pulse";
self.alsProtocolDetailViewController.protocolURL = wantedFile;
self.alsProtocolDetailViewController.protocolSubTitle = #"Stable Without Decompensation";
[self.navigationController pushViewController:self.alsProtocolDetailViewController animated:YES];
return NO;
}
}
This code is from my working old non-storyboard app.
Basically its reloading the same view its currently in with a new loadRequest and pushing it on top of the stack, so the user can go back to the last loadRequest.
Now with storyboard I can not figure out how to accomplish this same function with segue's, I need to invoke these actions and push the controller onto the stack.
I tried performSegueWithIdentifier but had no luck.
If I use a NSURL load request for the webview without pushing a new instance of the view controller on the stack, it works.
But I need the user to be able to go back.
Any ideas how I can accomplish this?
You can use UIWebView's goBack method:
goBack
Loads the previous location in the back-forward list.
You could add some buttons to accomplish this, in the nav bar or an extra toolbar. This is certainly better design than pushing a new view controller instance.
BTW, you can push a view controller onto itself via segues by dragging the segue to the very same view controller in storyboard.
I fixed the issue by using:
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
And getting the storyboard item by its ID, and then using the old pushViewController.
Achieved the same effect.
I have subclass TTphotoviewcontroller and added a button for saving the images to the camera roll. All the other buttons on the toolbar are working fine (previous, next and play) except for the one I've created by overriding in the subclass file. I am getting the dreaded 'unrecognized selector sent to instance' error when I click the button.
My subclass TTphotoviewcontroller is inside a navigation controller which in turn is inside a navigation controller of a tabbarcontroller.
The top nav controller has a view controller with an in-app purchase button. After purchase the user goes through to the wallpaper section containing the subclass TTphotoviewcontroller inside a navigation controller (hope that makes sense).
I have checked all the linker settings. I have used some of the code that can be found here
tabbar - navigation controller - in-app purchase view controller - navigation controller - subclass photoviewcontroller - clickActionItem selector method for button
I create the button like so:
_clickActionItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self action:#selector(clickActionItem)];
and fire with this:
- (void) clickActionItem: (id)sender
{
NSURL *aUrl = [NSURL URLWithString:[_centerPhoto URLForVersion:TTPhotoVersionLarge]];
NSData *data = [NSData dataWithContentsOfURL:aUrl];
UIImage *img = [[UIImage alloc] initWithData:data];
NSLog(#"photo:class %#", [img class]);
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
}
I think there is simple mistake by you that you are forgotten to give ':' after your #selector method.
_clickActionItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemAction
target:self action:#selector(clickActionItem:)];
My suggestion will be that if you didn't create the selector method before calling it then always do copy and paste of the complete name of the method so this type of error can get reduced.
This is probably a simple question...
I have a UIView with a button that corresponds to a URL address http:// etc. I want to be able to click this web address button and have it load a UIWebView on a separate UIViewController and XIB that shows the website, allowing me to then hit a back button and go back to my first view that has the original button. Basically I want to be able to load the webpage but have it operate within the App rather than start up Safari. Is this possible?
So far I have build a dedicated XIB and ViewController for my webpage for which the viewDidLoad method looks like this:
- (void)viewDidLoad{
[super viewDidLoad];
NSString *urlAddress = pushURL; //pushURL is passed as parameter
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObject = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObject];
[addressBar setText:urlAddress];
}
Question is... how do I call the UIWebView from my button (which is in the first class/parent viewController)? All my other viewControllers in my app are using UITableView so it's a simple DidSelectRowAtIndex.
Thanks.
It sounds like you want to be using a UINavigationController. Then when the button is tapped just push the UIViewController with the web view to the UINavigationController.
This is an iPad app. You press one of two buttons and it presents a modal view controller containing a webview which will display a PDF. I created the UIViewController class containing the webview, and I want to use 1 of 2 initializers depending on which button the user pushes. When I initialize the object I want to set which PDF is displayed. The modal view is coming up but it is blank(No PDF displayed). However, when I use this same code within -(void)viewDidLoad{} it does work. Why is this? Thanks!
- (id)initAsPi{
NSLog(#"initAsPi Called");
[super init];
NSString *urlAddress = [[NSBundle mainBundle] pathForResource:#"PiPdf" ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:urlAddress];
NSURLRequest *urlRequestObj = [NSURLRequest requestWithURL:url];
[prescribingInfoWebView loadRequest:urlRequestObj];
return self;
}
Initialization
Your initialization method is wrong. Read Implementing an Initializer - Handling Initialization Failure at ...
http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocAllocInit.html%23//apple_ref/doc/uid/TP30001163-CH22-105377
It should look like ...
- (id)initAsPi {
self = [super init];
if ( self ) {
// Your initialization code
}
return self;
}
Why it does work in viewDidLoad and not in initAsPi?
prescribingInfoWebView is nil unless you're doing something obscure elsewhere.
You're probably creating prescribingInfoWebView in loadView method or it is automatically initialized during view load if you're loading your view from XIB.
When the init method of a view controller is executed, it has not yet loaded its view. So prescribingInfoWebView is probably nil at this point (you should check with the debugger) and so the message you are trying to send to it has no effect.
UIViewControllers load their interfaces lazily. This means any UI properties set up in Interface Builder or in -loadView will be nil until some request for the view causes it to load. In your initializer, this is almost certainly the problem.