"loadHTMLString:baseURL:" iPhone SDK - ios

I'm coding up my firs iPhone app..
How can I use "loadHTMLString:baseURL:" to send the user to a view called "pagetwo.m"?

To navigate from one view to another or to insert subview given solution is correct and following is the sample code to let you know, how to use delegate method of UIWebView loadHTMLString:baseURL:...
NSString *embedHTML = [NSString stringWithFormat:#"<HTML><HEAD><TITLE>Page 1</TITLE></HEAD><BODY><H2>HTML Code Editor</H2><H3>p1 - The Basic Page</H3>"
"<P>This is the basic webpage. The HTML tags, BODY, H2, H4, P and A were used, but since no attributes were added to these tags, they are left at their defaults. The links below are shown using their default colors.</P>"
"Google.com<BR>Yahoo<BR></BODY></HTML>"];
[webView loadHTMLString:embedHTML baseURL:nil];

I assume pagetwo.m is a UIViewController subclass, right? You can't send a user to view with loadHTMLString:baseURL: since that's a UIWebView's method to load html page into webview. You will need something like:
[self presentModalViewController:pagetwo animated:YES];
Or if it's a subclass of UIView, you need:
[self.view addSubview:pagetwo];
But before that you need to alloc/init pagetwo (and release it later) which will be hard to do since your class is also named "pagetwo" and you instance variable can't be called the same. you could call it (the instance variable) pagetwoview or something, but preferred way would be to follow Objective-C naming conventions and always name your classes starting with a capital letter.

Related

using category to split up work in a UIViewController - how to reference parent?

I read somewhere recently that if you have an exceptionally large view controller class, you might consider splitting it up into multiple categories based on the work being performed. I'm attempting to do that, but I can't for the life of me figure out how to do it properly. And, on top of that, I can't find any online sources on the practice.
So I have this view controller that has 5+ UIViews laid out in a UIScrollView. I'm trying to create a private category for each UIView and put any work for those views within the category, i.e. any programmatic visual work, button presses, etc. However, I need a reference to the parent object to change the visual values because all labels and buttons are defined on the parent object. I can't define them within the category, Xcode won't let me. It will only let me define button presses. I've already tried sending a weak copy of self to the category upon initialization, but using it didn't change the visual values of any of the labels or anything.
It's pretty imperative that I have access to other objects within the parent. If I don't, this is kind of a lost cause. So, is it a lost cause? or am i doing it wrong?
within main class
__weak __typeof(self)weakSelf = self;
[_diaryViewController setupDietDiaryViewForController:weakSelf];
within category
- (void)setupDietDiaryViewForController:(PADashboardViewController *)mainDashboard {
mainDashboard.chooseHungerView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
mainDashboard.chooseHungerView.layer.borderWidth = 0.4f;
mainDashboard.chooseHungerView.layer.borderColor = [UIColor whiteColor].CGColor;
A category is nothing more than an extension of the base class. In essence, you could place any code within a category at the bottom of the main file and it would work exactly the same. Commonly, programmers prefer to split functionality up into categories in an attempt to make the code more readable.
Since it's nothing more than an extension of the main functionality, there is no need to create a reference to the parent class. If you want to make variables available to the category, all you need to do is place the variables in the header file of the main file. When you call a category function, you'll want to do it as if the function was within the base file itself.
[self setupProgressView];
Make sure the function setupProgressView is not static (it should be defined with a - instead of a +).
and within the category itself, you access variables like so:
[self.chooseHungerButton changeTitle:#"New Title"];
[self.deviationButton changeTitle:#"New Title"];
If you're trying to shift IB functionality over to a category, you'll need to define the buttons/views/objects within the base file's header. After defining the variables, you can access them from the category. If you want to create a method call for the button, you can control-drag the button straight into the category's .m file.
Make sure you include the category's .h file in the base file before attempting to use any functions within.

Loading hidden/offscreen UIWebView

Actually I have two related questions here, about different use cases of loading requests in a UIWebView.
Is it safe to call - [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy and its hidden property or the one of its superview is set to YES?
Is it safe to call - [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy?
In particular I'm interested whether it is considered to be a good practice to load request in a UIWebView that is not visible, and whether the delegate assigned to the instance of UIWebView will be notified once the request succeeds/fails. The reason I'm asking is that UIWebView class reference says "create a UIWebView object, attach it to a window, and send it a request to load web content", where the part telling that a UIWebView should be attached to a window makes me doubt if the above approaches are reliable.
I have successfully used [UIWebView loadRequest:] with objects that are not in the view hierarchy. I expect that the class reference just assumes that the view will be displayed as it's probably the most common use case.
Yes it is safe to call [UIWebView loadRequest:] on a web view that is inserted in the view hierarchy.Because you are going to use web in the view.Also if you just give the URL in the program,it is enough to get.
The following code for [UIWebView loadRequest] is
NSString *strurl =#"http:www.google.com";
NSURL *url=[NSURL urlWithString:strurl];
NSURLRequest *urlrequest =[NSURLRequest requestWithUrl:url];
[webView loadRequest:urlrequest];
Even it is safe to call [UIWebView loadRequest:] on a web view that is not inserted in the view hierarchy.Because you can dynamically create the view and write the code for web view through the program.
It works. The approach is completely reliable as long as you are not using any private API and following HIG. It is not a bad practice as long it suits to your requirement. If there is a hidden property available to you for UIWebView then of-course you can hide the webView as per your requirements.
About your below query, it is written in the documentation as per the sentence context.
The reason I'm asking is that UIWebView class reference says "create a
UIWebView object, attach it to a window, and send it a request to load
web content", where the part telling that a UIWebView should be
attached to a window makes me doubt if the above approaches are
reliable.
The full context is below, which clearly means that to display a webpage in your application using UIWebView, you have to do it in the mentioned way.
You use the UIWebView class to embed web content in your application.
To do so, you simply create a UIWebView object, attach it to a window,
and send it a request to load web content.

UIWebView contextual menu while displaying PDF content

In my application, I use UIWebView to display different types of files like, html, pdf, doc etc.
I am customizing the contextual menu on html content, by disabling callout and using gesture recognizer. It works fine for html content.
However when displaying pdf content web view uses different classes from corepdf framework which is private framework. So none of the technique which worked on html content works with pdf content.
UIWebView uses UIPDFPageView and related classes to display contextual menu on long pressing on hyperlink.
Is there any method to override the display of contextual menu without having to deal with private framework?
With clever tricks, you can override the underlying UIWebDocumentView's -canPerformAction:withSender: method to only pass what you wish to display. This should work for PDF as well, as the internal classes are deeper in the view hierarchy and you should be fine with the UIWebDocumentView.
On iOS5, 6 and 7 seed 1, the UIWebDocumentView object is a subview of the scroll view of the UIWebView. What I do is find the subview of the scroll view that has the #"UIWeb" prefix in its class name, and use the Objective C runtime to dynamically subclass it to my own subclass (I create it at runtime - like what the system does for key-value observing) and replace the implementation with my own. You can even call the previous implementation by calling the super.
This is hacky, but it seems to be safe. Apple does not seem to have introduced the off-process UIWebView (using XPC), so it should be safe with iOS7 as well.
As always the disclaimer, this API is not documented and is subject to change at any given moment, and your app may stop functioning at any timeĀ®.

UIWebView's _UIWebViewScrollView

I created an instance of UIWebView called myWebView. I populated myWebView with the content from loadHTMLString, added several UIImageViews and UIViews. It worked as expected: UIImageViews and UIViews are all on top of the webview's content from html string.
I then archived and un-archived myWebView using NSKeyedArchiver and NSKeyedUnarchiver. After un-archived, only the html string content is visible. All other UIViews and UIImageViews were moved to back. So they are not visible. An NSLog of myWebView.subviews before archiving and after un-archiving showed that there a _UIWebViewScrollView was moved from the first object in the subviews to the last object. Note that only the _UIWebViewScrollView order was changed.
Not sure if it was by design by the SDK but to restore the views order and make them visible as normal, I did this:
[myWebView sendSubviewToBack:[myWebView.subviews lastObject]];
Question: Will doing this violate any of Apple's rule as I am not sure what if the _UIWebViewScrollView is considered as Apple's private thing. But since I did not touch that _UIWebViewScrollView.
Well, as far as rules go, you're fine.
HOWEVER: Since the UIWebViewScrollView is an internal member of the UIWebView, if they change the workings of UIWebView in a different iOS version, your code might get screwed up.
To play it a little on the safer side, you should try to set tags on each of the subviews you add, and then do:
[myWebView bringSubviewToFront:[myWebView viewWithTag:100]];
[myWebView bringSubviewToFront:[myWebView viewWithTag:101]];
[myWebView bringSubviewToFront:[myWebView viewWithTag:102]];
... etc (remember to use the tags that you used for your subviews)
On another note, messing with the UIWebView might result in troubles later on, there might be other ways to accomplish what you're trying to do, ways that are less risky

Not all UITextFields are calling all delegate methods

I've got a view controller xib file with several views in it. I'm building a wizard-type interface. I'm just doing a simple fade between the views, and I'm already using a navigation controller for the main interface. I'd prefer not to use one for this wizard. Anyway, in the views, each panel has at least a button, some sort of input field (usually a UITextField) and some helper text hard coded in a UILabel.
The problem is that not all the UITextField objects are calling the textFieldDidChange method in the delegate (File's Owner - .m file associated with the xib), but all the UITextField objects ARE calling the textFieldDidBeginEditing method.
Makes no sense to me. I feel like I must be missing something simple in how I set up the screens, but I'll be darned if I can figure it out. Each of the screens all look identical in the property sheets (on the right hand side of Xcode), and everything is wired up correctly in the File's Owner property sheet, both in IBOutlet and IBActions.
Here are some shots of what's going on...
Ideas? Thanks.
Here are links to the screen caps of the vital parts.
(being a new member is making it hard to add all the info I need with screen caps!)
As far as I now, there is no delegate method with the header textFieldDidChange. You have created a method of your own, which is depending on a NSNotification. Make sure all the UITextFields are send the right notification.
There is no such method on a UITextFieldDelegate
You may have confused textViewDidChange, which is a delegate method for a UITextView, but itis passed the UITextView that generated the event, not an NSNotification.
Seems like you want textField:shouldChangeCharactersInRange:replacementString: instead.
This is resolved. I'm a knucklehead. :-)
I was attaching my own notifier/observer and hadn't done so for the last few UITextField objects. Sorry to bother y'all.

Resources