Why doesn't this local html page load? - ios

I have the following:
#interface HelpViewController : UIViewController <UIWebViewDelegate>
#property (nonatomic, retain) IBOutlet UIWebView *webView;
#end
And for the same view controller
#implementation HelpViewController
#synthesize webView = _webView;
- (void) viewDidLoad
{
[super viewDidLoad];
NSString *filePath=[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSLog(#"%#", filePath);
[self.webView loadData:[NSData dataWithContentsOfFile:filePath]
MIMEType:#"text/html"
textEncodingName:#"UTF-8"
baseURL:nil];
}
And a basically empty nib to go with this view controller. The file's owner is has an outlet to view. The file path comes back OK. But nothing shows up in the view. I think the problem is that IBOutlet UIWebView *webView is not connected to anything, 'cause I don't know what to connect it to. Is that the problem? If so I need to know what to connect it to; otherwise, I need any kind of help! Thanks!
EDIT: Forgot to mention that I think I have the same problem as this SO Question where it says to connect the outlet, but doesn't say to what.

Normally when you add a new file to the project xcode will automatically add it to the "Copy Bundle resources" section of your target.
Sometimes, if you are collaborating with another person the project.pbxproj will get out of sync and this entry will get removed. The fix is to ensure that all the files in the Resources folder (that are required in the release) are also present in the "Copy Bundle resources" section, and if it isn't just drag it from the resources to that section.
In your case drag check if your html file exists in copy bundle resources. Check that in the project > target > build phases > copy ressources to bundle.

Alright, after a lot of study of other SO pages and some non-SO material, I figured out that I had not added a web view from the library to the nib. So that's why I couldn't hook it up. So I made sure first that the 'regular' view was hooked up, added the web view from the library and hooked up it's view. Then I needed the following additional line in my code:
[self.view addSubview:_webView];
to viewDidLoad. Whew!

Related

Why loadNibNamed:owner:options will automatically install on current view?

- (void)viewDidLoad
{
[super viewDidLoad];
UIView *testView = [[[NSBundle mainBundle] loadNibNamed:#"VBImageView" owner:self options:nil] lastObject];
NSLog(#"%d", [self.view.subviews count]); // result is 1
}
Something I don't understand is, just by calling loadNibNamed method will actually load imageView and add to current subview? Why? Can someone please explain that?
Updates
I found that in xib file, the Files's owner has the view delegate setup, If I remove this delegate, then the view will not automatically setup. what doest that view delegate mean?
I have now uploaded all the source files at here
I did the same thing but log 0 , so I think your view controller's view is not empty, could you try NSLog(#"%#", self.view.subviews) and show me the result?
for the second question, that view outlet just the property of your ViewController,that view will not automatically setup when disconnect because your window could not find that view to add.
EDIT:
That's too wired man, I download your source code and run it on simulator, it log 0 too.
after I delete loadFromNib method in viewDidLoad,it log 2.then I found there are two guide layout and one view in your storyboard.
By delete that view in storyboard, it log zero. so I think this is the reason .However, I not familiar with storyboard , so I can't completely explain it.sorry about that.

FBProfilePictureView object won't show image

I'm doing the scrumptios facebook developer tutorial for the 3.1 iOS SDK.
I've managed to display my profile name from facebook, however the FBProfilePictureView wont show the picture
here's the code for iboutlet
#property (strong, nonatomic) IBOutlet FBProfilePictureView *useProfileImage;
here is what I used to display the pic
self.useProfileImage.profileID = user.id;
I have confirmed that the it changed the profileid variable of the image by displaying it in a label.
When I first ran the project I had this error
"2012-05-18 04:04:11.620 Scrumptious[6548:f803] Unknown class
FBProfilePictureView in Interface Builder file."
and solved it they way the FB tutorial suggested- by adding [FBProfilePictureView class];
to "applicationdidfinishlaunching: withoptions" in the app delegate.
any idea why the picture wouldn't show?
link to the tutorial
Adding the [FBProfilePictureView class]; as the first line in the app delegate method of didFinishLaunching seems kinda hacky to me.
Check out the HelloFacebookSample inside the SDK. Here is the 4 line comment:
// If you have not added the -ObjC linker flag, you may need to uncomment the following line because
// Nib files require the type to have been loaded before they can do the wireup successfully.
// http://stackoverflow.com/questions/1725881/unknown-class-myclass-in-interface-builder-file-error-at-runtime
// [FBProfilePictureView class];
I tried adding the -ObjC flag and it worked fine.
To resolve this, add this as the first line in the app delegate's application:didFinishLaunchingWithOptions: method:
[FBProfilePictureView class];
As documented in the facebook webpages you need to put the [FBProfilePictureView class]; as the first line in the app delegate method of didFinishLaunching. (Note their sample code does not have this but still works, in my case it will not work without it.
I created a UIView object sized it up and then selected the class from UIView to FBProfilePictureView. This will disconnect any connection between your program and IB so you need to reconnect again, the error in which you are not able to see the picture is due to the object not being wired up to an IBOutlet item.
I am not sure if a cut and paste will work, maybe it will, but I created the object from UIView and just changed the class manually through IB.
Without the [FBProfilePictureView class]; it will give you an error.
If done correctly, if you view the XIB - in the object window/list you should see "Profile Picture View"along with Button and Label instead of FBProfilePictureView. The profile pic from facebook should show.
I have the same problem. My solution is to uncheck the "Use autolayout" option in "File Inspector" of the xib file. To open the "File Inspector", press cmd+option+1 after opening the xib file.
Edit 1: Just find another solution.
Try to set a width constraint and height constraint to the profile picture view. I find this tutorial is useful.
I had the same issue, xcode4.5.1 ios6.
to fix do this: in IB uncheck use autolayout for FBprofilepictureview, also uncheck "autoresize subview" into the view
They(fb developer forum) said paste that in your app delegate didLunch method like that
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[FBProfilePictureView class];
// Create a LoginUIViewController instance where the login button will be
LoginUIViewController *loginUIViewController = [[LoginUIViewController alloc] init];
// Set loginUIViewController as root view controller
[[self window] setRootViewController:loginUIViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
just replace
[FBProfilePictureView class];
with these commented lines it will work
// Override point for customization after application launch.
// Load the FBProfilePictureView
// You can find more information about why you need to add this line of code in our troubleshooting guide
// https://developers.facebook.com/docs/ios/troubleshooting#objc
[FBProfilePictureView class];
it will work magically :)
Classes that use categories to extend existing classes are not loaded automatically. Classes that are used from the Xcode interface builder aren't loaded automatically, for example when you add a FBLoginView to your interface by drawing a view in your .xib file and then classing it as FBLoginView from the interface builder UI.
class helps to create a pointer to the FBProfilePictureView.

How to get a reference to an object in a .nib file?

so far I have done most view programming in code, but it really is cumbersome to position buttons perfectly in code. I searched for some time, maybe I'm using the wrong words for my search? Anyway, I created a UI in a nib file, it has several buttons. I loaded the .nib file into my view like this:
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"UCMenuView" owner:self options:nil];
UIView *menuView = [nibViews objectAtIndex:0];
self.delegate = (id<UCMapviewDelegate>)delegate;
// another controller does the view switching, so I'm sending him the view
[self.delegate pushView:menuView];
This is 'fine'. But I don't understand how I get a pointer to my buttons. I know about IBOutlet, but how do I connect them to the elements in the .nib? How would I connect this #property (strong, nonatomic) IBOutlet UIButton *calendar;
to a specific button in the .nib?
I tried to ctrl-drag (from that little points on the left side of the #property, but that does not work (from neither side). Excuse me if this is an easy question, but I couldn't find a clear explanation
thanks for your help
Instead of just a UIView *menuView, you need to create a custom
UIView subclass for that, e.g. UCMenuView. Define your properties on this custom
class.
Open your .xib file in the editor, select the File's Owner in
the left column and set its Custom Class in the identity
inspector to UCMenuView.
Right-click the File's Owner in the left column, and connect
your IBOutlets.
Check out this tutorial:
http://developer.apple.com/library/ios/#documentation/IDEs/Conceptual/Xcode4TransitionGuide/InterfaceBuilder/InterfaceBuilder.html
or
http://www.hongkiat.com/blog/ios-development-guide-part2-your-first-app/
Kind regards,
Bo
See Apple's documentation: Creating and Connecting an Outlet and more generally Designing User Interfaces in Xcode.

how to add a ready-to-use project ( created and started with application delegate but without xib,nib) to another project

I have a ready-to-use project ( created and started with application delegate but without xib,nib). Now I want to invoke and start it from another project.
But I can't use initwithnib, how can I include and start this ready-to-use project from another project?
More specifically, how to add and integrate Apple's Sample code " TableViewUpdates ( TVAnimationsGestures.xcodeproject ) " which only has its own appDelegate and Mainwindow.xib file to my own application? thanks!
Or can you add a xib file to the Apple's Sample code " TableViewUpdates ( TVAnimationsGestures.xcodeproject ) " so that I may use initWithNib in my another project to invoke and run the sample code. thanks !
I work on Xcode 4.3.2.
Create sigle view sigle view app.
Expand all folders in MoviePlayer example. Select all files, except info.plist, main, Frameworks, Products folders, readme.txt, MoviePlayer.project. Copy them to new empty project. Don't forget to check "add to target".
In target's build phase tab added MediaPlayer.framework (in link Binary with libs)
Copy all object from MainWindow.xib to your own xib for your viewController.
Than in ViewController.h :
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITabBarController *tabBarConroller;
#end
Connect your tabBarController from ViewController.xib to the property.
Than in ViewController.m :
#implementation ViewController
#synthesize tabBarConroller;
- (void)viewDidLoad
{
[super viewDidLoad];
[self.view addSubview:tabBarConroller.view];
}
- (void)viewDidUnload
{
[self setTabBarConroller:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
}
...other appdelegateMethods ...
#end
7. Check that all objects in viewController.xib has necessary outlets.
6. Edit > Refactor > Convert to ARC. (next, ok...)
It's done.

Loaded nib but the 'view' outlet was not set

I added a new nib file to my project, and tried to load it.
However, when I click on the toolbar icon that is supposed to take me to the view that I created, I get an NSInternalInconsistencyException with the message:
Terminating app due to uncaught exception
'NSInternalInconsistencyException', reason: '-[UIViewController
_loadViewFromNibNamed:bundle:] loaded the "..." nib but the view outlet was not set.'
So I opened up my nib file, and I see for the view that there are no referencing outlets set. However, I try to click and drag the circle for "new referencing outlet" to File's Owner, but it won't let me...what do I need to do to get my view to display?
Here's what worked for me:
Open the XIB file causing problems
Click on file's owner icon on the left bar (top one, looks like a yellow outlined box)
If you don't see the right-hand sidebar, click on the third icon above "view" in your toolbar. This will show the right-hand sidebar
In the right-hand sidebar, click on the fourth tab--the one that looks a bit like a newspaper
Under "Custom Class" at the top, make sure Class is the name of the ViewController that should correspond to this view. If not, enter it
In the right-hand sidebar, click on the last tab--the one that looks like a circle with an arrow in it
You should see "outlets" with "view" under it. Drag the circle next to it over to the "view" icon on the left bar (bottom one, looks like a white square with a thick gray outline
Save the xib and re-run
This is Josh Justice proposal, but in a graphical way (pictures are mine):
Select File owner
On right hand side panel select custom class.
Enter the custom class name
On right hand side panel select oultets
Drag view outlet to view component
Finally the View Controller is instantiated with the rolling code:
PTFilterUserVC *aFilterUserVC = [[PTFilterUserVC alloc] initWithNibName:#"FilterVC" bundle:nil];
//OPTIONAL.This is how 'I' am interested in present the view controller.
[self.navigationController pushViewController:aFilterUserVC animated:YES];
I can generally fix it by remaking the connection between File's Owner and the view. Control-drag from the File's owner to your View (in IB) and select view from the pop-up menu.
The View Identity - Class Identity was not set. After setting it to the appropriate class, the issue was resolved.
Are you sure you have a UIView (or subclass) assigned to the "view" property of yourViewController?
Right click on "File Owner" in the left pane of the xib for yourViewController and verify that the "view" outlet is set.
If not, set it to a view!
this will definetly fix the Issue
For me all the things stated here https://stackoverflow.com/a/6395750/939501 were true but still it was throwing error, reason was I created a View class with name ABCView and then deleted it later I added a view controller as ABCViewController so somehow it was referring to old ABCView in new view controller, I had to delete the ABCViewController and add a new one with different name that solved my issue.
Thanks
I had the same issue with XCode 4.6.3. I had started out with a couple files named MySettingsView.h and .m but deleted them in favor of MySettingsViewController.h, but despite trying most of the hints mentioned here, it still kept erroring with,
2013-07-05 11:48:17.205 MyApp[39024:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason:
'-[UIViewController _loadViewFromNibNamed:bundle:] loaded the
"MySettingsView" nib but the view outlet was not set.'
It was evidently still "confused", trying to load MySettingsView.xib instead of MySettingsView Controller.xib. Maybe its "do what I mean" logic is too fancy.
So I worked around the problem by hardcoding the NIB/XIB name in MySettingsViewController.m:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:#"MySettingsViewController" bundle:nibBundleOrNil];
}
I ran into this problem in a slightly different way from the other answers here.
If I simply created a new xib file, added a UIViewController to it in Interface Builder, and set that UIViewController's custom class to my view controller, that resulted in the "view outlet was not set" crash. The other solutions here say to control-drag the view outlet to the View, but for me the view outlet was greyed out and I couldn't control-drag it.
I figured out that my mistake was in adding a UIViewController in Interface Builder. Instead, I had to add a UIView, and set the Custom Class of the File's Owner to my view controller. Then I could control-drag the view outlet of the File's Owner to my new view UIView and everything worked as it should.
To anyone that is using an xib method to create a UIView and having this problem, you will notice that you won't have the "view" outlet under the connections inspector menu. But if you set the File's Owners custom class to a UIViewController and then you will see the "view" outlet, which you can just CMND connect an outlet to the CustomView.
My issue with this was caused by having a duplicate nib in the class folder that did not have the view set. xcode seemed to be choosing one nib for a build and then the other the next time I built the project. Just deleted the other one. Looks good. Doh!
Just spent more than hour trying to find out why my view property is not set in my view controller upon initiating it from nib. Remember to call "[super initWithNibName...]" inside your view controller's initWithNibName.
I just fixed this in mine. Large project, two files. One was "ReallyLargeNameView" and another was "ReallyLargeNameViewController"
Based on the 2nd answer chosen above, I decided I should clean my build. Nada, but I was still suspect of XCode (as I have two identical classes, should abstract them but eh...) So one's working, one's not. File's owner names are so far as copy and pasted, outlets rehooked up, xCode rebooted, still nothing.
So I delete the similar named class (which is a view). Soon, new error "outlet inside not hooked up" literally was "webView not key value" blah... basically saying "Visual Studio's better". Anyway... I erase the smaller named file, and bam, it works.
XCode is confused by similar-named files. And the project is large enough to need rebooting a bit, that may be part of it.
Wish I had a more technical answer than "XCode is confused", but well, xCode gets confused a lot at this point. Unconfused it the same way I'd help a little kid. It works now, :) Should benefit others if the above doesn't fix anything.
Always remember to clean your builds (by deleting off the simulator too)
The previous answers almost solved the problem for me, but the last step was missing.
Create a xib and swift file with the same name.
Set the file owner to be the UIView subclass.
Drag an outlet from the View to the UIView subclass, name it "contentView"
Add this custom initializer so when the xib loads it attaches the contentView
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
Bundle(for: self.classForCoder).loadNibNamed("SampleView", owner: self, options: nil)
addSubview(contentView)
contentView.frame = self.bounds
contentView.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
Now any #IBOutlets you add will be attached.
Cheers,
Richard
I also had the same problem and my issue was that i added an other Localisation (English) to the ViewControllers nib so my App with the Localisation German couldĀ“t find the nib with the Localisation English!! Hope this helps anybody!
I had face the same problem while accidentally deleted xib reference and added it again.I just fixed by making connection between Files owner and the view.Also make sure that your FilesOwner's custom class is your expected viewController.
For me, the problem was caused by calling initWithNibName:bundle:. I am using table view cells from a nib file to define entry forms that sit on tableViews. As I don't have a view, doesn't make sense to hook to one. Instead, if I call the initWithStyle: method instead, and from within there, I load the nib file, then things work as expected.
I had the same problem, but a slightly different solution was called for. The problem in this case was the class of the File Owner, rather than the class of the View. To set this, I had to click the "backwards play" icon in the lower left corner of the Interface Builder window, and options then appeared that isolated the characteristics of the File Owner, the First Responder, and the View. Clicking on the first one (a large transparent box), enabled me to then set its custom class as suggested above.
I had the same problem, but a different solution was called for. The problem in this case was the class of the File Owner was not connected to xib file.
I ran into something very similar tonight, with a Swift UIViewController subclass. In this case, none of the above fixes worked, but reordering my code a bit did. Net-net, having an extension to the subclass occur before the subclass's definition itself in the same file seems to confuse XCode, despite compiling fine; the fix was to place the extensions after the subclass's definition.
I've posted the details in an answer to this similar question.
In my case , the designated initializer - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil in ***ViewController class was implemented so even if when I call other initializer to initialize the object ,the designated initializer will be called .
So to resolve this problem checking wether the - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil is also a proper way.
My problem was in wrong classes. I used custom .xib for my custom view.
Correctly it has to be set like here:
View shouldn't have a class
Class which is used with the .xib is set in File's Owner tab
Outlets are connected to File's Owner and not to the View.
Just had the same error in my project, but different reason. In my case I had an IBOutlet setup with the name "View" in my custom UITableViewController class. I knew "view" was special because that is a member of the base class, but I didn't think View (different case) would also be a problem. I guess some areas of Cocoa are not case-sensitive, and probably loading a xib is one of those areas. So I just renamed it to DefaultView and all is good now.
select the files owner and goto open the identity inspecter give the class name to which it corresponds to. If none of the above methods works and still you can't see the view outlet then give new referencing outlet Connection to the File's Owner then you can able to see the view outlet. Click on the view Outlet to make a connection between the View Outlet and File's owner. Run the Application this works fine.
In my case, the view was not viewed in xib.
in xib the View was size = none (4th tab right hand). I set size to Freeform and reload xCode.
view was appealed and I set the proper link to View.
If you have tried everything and you still get this error, try re-creating the class file from scratch but remember to select the "Also create XIB file" check box. This will auto link up a few items that are not linked when creating these files separately. After this is created, you can likely cut and paste everything onto the new XIB and it should work fine.
I am finding this issue specifically with creating files separately in Swift.
for me it happened, when
I have a ViewController class ( .mm/h ) associated with the Nib file,
UIView from this ViewController has to be loaded on the another view as a subview,
we will call something like this
-(void)initCheckView{
CheckView *pCheckViewCtrl = [CheckView instance];
pCheckView = [pCheckViewCtrl view];
[[self view]addSubview:pCheckView];
[pCheckViewCtrl performCheck];
}
Where
+(CheckView *)instance{
static CheckView *pCheckView = nil;
static dispatch_once_t checkToken;
dispatch_once(&checkToken, ^{
pCheckView = [[CheckView alloc]initWithNibName:#"CheckView" bundle:nil];
if ( pCheckView){
[pCheckView initLocal];
**[pCheckView loadView];**
}
});
return pCheckView;
}
Here loadView was missing,,, adding this line resolved my problem.
I had the same problem, I figured out and it is because of i had ticked "Static cells" in the properties of Table View under Content option. Worked when it changed to "Dynamic Prototypes". Screenshot is below.
I had a similar problem with Xcode 9.3, and setting "Module" under the "File Owner's" attribute inspector to the project module fixed this for me.
Open your storyboard file where your viewController exists, or XIB related file with textEdit.app and check if the storyboard or XIB name is the same of your viewController, then change it, save and reload/restart Xcode.
If you're using a custom init method, check that you're returning something valid. I ran into a piece of code that crashed on something like this:
- (id)init {
self = [super init];
if (self) {
CustomController *controller = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(className) owner:self options:nil];
} return self;
}
In another class, the controller was created like so:
CustomController *controller = [[CustomController alloc] init];
The problem is that in the init method, self hasn't changed and should look like this instead:
- (id)init {
self = [super init];
if (self) {
CustomController *controller = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(className) owner:self options:nil];
[controller viewDidLoad];
self = controller;
} return self;
}

Resources