ios - loading decoupled view from nib file - ios

I am having difficulty with something that seems to be trivial. I am following this project https://github.com/eppz/iOS.Blog.UIView_from_Xib and trying to implement the decoupled way of loading a view from a nib file. I was not able to get the desired result in my app (nothing shows up and I get no crashes either) so I did the following:
I took the project from Github https://github.com/eppz/iOS.Blog.UIView_from_Xib
I then proceeded to duplicate the files in the "decoupled" group and name it something trivial like "decoupled2". Basically I am creating a whole new class that stands on its own with its own nib file.
I then changed all of the internal tags and class references. Then I made sure that all of the new references were pointing to this new class.
At this point I added a 6th button to the main VC and made sure to call the right method for my new "decoupled2" class when this 6th button is pressed.
PROBLEM: Everything runs but the view from nib does not show up. Debugging view hierarchy shows nothing. Clicking the 5th button still works, so I didn't break the existing code. Why won't the newly created nib file show up?
This seems rather simple, just duplicate what already works in a project that already works and it will work...or not. What am I missing? Thank you for your help.
I have spent a few days on the problem already. Lots of research but nothing of use so far.

I re-did the task and it worked this time.
Maybe restarting Xcode 8.0 was the key or something else that is under the hood.

Related

App crashes in AppDelegate and cannot get past launch screen

The app crashes in AppDelegate, unable to get beyond the launch screen. No view controller code ever gets executed.
The stack trace is also not very helpful nor is the line highlighted in AppDelegate when the crash occurs.
Images below provide the details.
How to debug from here?
Check any IBOutlet links carefully; you may have deleted the property from your view controller but the UI element is still connected to it. This should give you an exception stating something like Class ViewController is not key value coding-compliant for key XXX where XXX is your bad IBOutlet, however Swift's exception reporting does seem to be a bit hit-and-miss at times.
Here's another possible cause related to the reason Paulw11 stated that may help someone else.
If you have segue's in your storyboard that links to a storyboard reference in another bundle, and you change the name of the bundle where the reference resides, make sure to update the Bundle name in your storyboard for the linked reference.
I have a framework I'm developing that contains some storyboards and UI components. I decided to rename the framework, updated the podfile references in the app and all the file imports to reflect the new framework name but completely forgot that there were storyboard references in the app referencing view controllers provided in storyboards in the framework.
Here's an example:
If you do not rename the reference you will experience a crash when you try to load the storyboard containing your link with no hint as to why. I spent an embarrassing amount of time hunting this down.

Storyboards in Xcode 6

I have recently started to learn iOS with obj-c from "iOS Programming The Big Nerd Ranch Guide 4th Edition". This edition was released in 2014 and is written with Xcode 5.
I am trying to make a simple app with two buttons and two labels. The labels are connected to two arrays and when a button is pressed an object from the corresponding array is shown in the corresponding text label (it's the Quiz app in chapter 1).
I created the project as a Single View app in Xcode 6, and put all my objects in the view controller class. I have two labels two buttons two arrays and an int to keep track of the object that has to be displayed from the array.
In the book it says that I should initialize the arrays in the initWithNibName method. I tried that but for some reason it never gets called. So I changed the initialization of the arrays to the init method. They initialize fine but when they are called from another method they are nil. Do you have any idea why this is happening?
The second issue I'm having is that I can't manage to get the contents of the storyboard on screen. It says that I'm supposed to make an instance of the ViewController inside the AppDelegate and make it the root window controller but all I get is a white window (or black in case I don't set the color).
UPDATE: I changed the intialization of the arrays from the init method to the viewDidLoad method and now they seem to be working fine. Nothing on the screen though.
It sounds like you're initializing your UIViewController from the app delegate AND a storyboard. If you create a new project in XCode, a "Single view application", you won't have to touch the app delegate at all in order to get something on the screen.
I believe both your problems are related to this, since it sounds like you're seeing an empty UIViewController on the screen (the one you create in the app delegate)
As for the initialization of your array, viewDidLoad is a popular place to do this.
If you are using storyboards, the method initWithNibNameOrNil will not be called. In the BNR book, it teaches you to use XIB files, which do use this method. If you are trying to follow the tutorials, I would suggest using XIB files.
For use of a book, I would suggest downloading whatever version of Xcode is being used for that book -- otherwise you will be running into a lot of confusing problems while learning.
If you would like to download previous version of Xcode, refer to this post:
How to download Xcode DMG or XIP file?

Error in Xcode 6 - view controller does not have an outlet named (subview)

I just got this error in one of my apps, and after checking some other apps the same error is happening. None of my IBOutlets and IBActions are connected anymore. When I recreate the outlet by control-dragging into the .h file, the outlet still displays the error in Connections Inspector > Outlets, saying (view controller) does not have an outlet named (subview). Before today I didn't have this problem, so is to do with Xcode 6? The view controller is referencing the correct class under Identity Inspector > Custom Class. I have deleted the derived data folder, removed the reference to the .h file, cleaned the project, and cleaned the build folder. None of that has worked and I can't progress with development until this issue is fixed.
Update: I just reopened the project after a couple of days and my outlets no longer have the exclamation mark next to them, and the 'easyLabel' outlet that I disconnected has reappeared. I didn't change anything, so I guess it's a bug in Xcode 6-6.1.
Update 2: This error came back. Exclamation marks next to every outlet. If I delete them I can't re-add them. Can't create new outlets. Also can't give a view controller a custom class. I really just want to make some progress on an app, but this issue keeps recurring. Really just can't stand it anymore. Any help would be appreciated.
Duplicate of: Xcode 6: can't connect any IBOutlet to ViewController but here's the trick:
You can also see that the link between the parent view and the custom class is broken (not visible anymore) which is a huge problem.
I had the exact same issue with the app i'm working on actually, updating Xcode from 5.xxx to 6.1. The workaround that worked for me was to remove the reference of every view controller and re-add them to the project... Unfortunately, in some cases, mine actually, all the connections get lost again when XCode is closed.
To everyone facing that issue, here's the (annoying) trick :
Step 1 : select both .h and .m view controller files
Step 2 : remove the reference of those files
Step 3 : re-add the files to your project tree
Step 4 : open the storyboard, eventually re-build the project and smile
I can understand those things could be reaaally annoying, but it worked for me... Hope it will help someone else !
I had the exact same problem and what was breaking my storyboards was the fact that my project was inside the folder /Dropbox (MyName)/projects/
Apparently something about the name of the dropbox folder with spaces and ( ) was BREAKING my storyboard completely, try what I did and move your project completely to another folder and see if that helps :)
I had the same problem, changed the class under identity Inspector > Custom Class to something else, saved it and then set it again to the correct one, after doing that the connections appeared normally.
I had this issue with a Swift UIViewController subclass. I had removed the original view that IB created for me, and added a new view—which I was unable to connect.
I went to the Identity inspector for the File's owner, and tried to re-enter it, thinking that maybe it had been modified somehow. Oddly, the class name did not autocomplete. It seemed like XCode was unable to actually see my class.
I tried removing and re-adding both the .swift and .xib files, to no avail. XCode would allow me to manually type in my owning class name, but it would not autocomplete. It seemed to think it didn't exist, or wasn't valid for this context.
Looking back at my code, I had something like the following:
extension SomeViewController {
func foo() -> Bool {
return false
}
}
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
It compiles just fine, but on a hunch, I removed the extension. I saved the file, went back to the .xib, and was able to set the File's Owner identity with autocompletion again. I was also able to wire up the view.
It would suck if extensions always broke things of course, so I tried again, this time with the extension after the class:
class SomeViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}
extension SomeViewController {
func foo() -> Bool {
return false
}
}
Everything still worked in IB. To sanity check, I moved the extension back ahead of the class definition, and things were again horked.
I took a look a the *-Swift.h generated by XCode, and at least relative to the affected class, there seemed to be no differences—regardless of where I put the extensions in the Swift file, they were always declared after the #interface definition for the actual class in the header.
So, long story short, in my case this was due to Swift extensions (I say extensions because my actual code has many) coming before the class definition. I moved them after the class definition, and the problem went away.
For me I had to remove the reference and add back to my project the -XIB- file for my view controller. Simply removing the view controller .h/.m did not reset the outlets, but removing the .xib and adding it back did!
I have this problem only with #protocol defined IBOutlets if I don't redefine them in the implementing class.
Probably the only thing we can do is file a bug.
Why dont you update your xcode 6.0.1 to xcode 6.1, Apple have fixed many bugs.
Download Xcode 6
Do you get the same issue if you connect the actions/outlets in Interface Builder instead of the assistant editor? For actions try control dragging from your object in IB to your first responder object (or whatever you have your class set to).
#robb actually discovered the fix for this, but the issue he found was that the group in the project that mirrored a folder on disk used a different case.
So if the project group was Broken/classone.m
and on disk it was broken/classone.m
Interface builder could not find the files. Renaming either the group or the folder to match case resolved the issue.
I was having the same issue but it was because i changed the name of the view controller in the Project Navigator to MapViewController but its name stayed the same (ViewController) in the code. once i changed it, it worked
I've had this same problem since using XCode 6.1, answers above didn't work for me. (removing/re-adding my class files, changing the class to something else and then changing it back. The only thing I didn't try was creating a new project from scratch).
I noticed by accident that my 'Application' placeholder within the MainMenu.xib had somehow got its class set to NSObject. Changing that to NSApplication seemed to clear the 'does not have an outlet named' problem from all of my other objects.
I updated my project from Xcode 6.x to Xcode 7.x about two months ago without any issues. But then this morning I ran into this bug. I tried pretty much everything on this page but nothing worked. But then Justin Middleton's solution gave me an idea.
When I switched my project from the 10.9 SDK to the 10.11 SDK, I decided I wanted my code to remain backwards compatible just in case I had to revert back to Xcode 6.2. So I put Preprocessor code around several of my object declarations, e.g.:
// This caused the (!) problem in Interface Builder
#if( MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_10 )
#interface CLChatWindowController : NSObject <WebPolicyDelegate, WebUIDelegate, WebFrameLoadDelegate>
#else
#interface CLChatWindowController : NSObject
#endif
...
// Removing all the Preprocessor code fixed the problem
#interface CLChatWindowController : NSObject <WebPolicyDelegate, WebUIDelegate, WebFrameLoadDelegate>
....
Once I removed all the Preprocessor code, the (!) characters in Interface Builder disappeared and everything went back to normal. Hope that helps someone.
Xcode 6.3.2. Similar issue. A project(s) that i've been working on suddenly 'disconnects' IBOutlets and shows the error
view controller does not have an outlet named (subview)
The project still builds and works and if you hover over the IBOutlets in code it thinks there are still connected.
I've nailed this down (at least in my case) to an issue with storing projects on dropbox (similar to an above post). The fix for me was super easy:
Drag the project folder off of dropbox to your desktop. Then drag the project back into dropbox.
This solution has fixed numerous projects with the issue.
Try to add the header file to the project, if it's missing.
I had this in Xcode 7.0.1 on a swift project. Specifically, tableview cell connections were broken (permanently) while other things still worked. Deleting derived data, rebooting etc - nothing worked.
My solution ended up being:
Close xcode project
Delete derived data for project (Window->Projects)
Rename the entire project/source folder, ie. project -> project-1
(Update source-management bookmarks like sourcetree as required)
Open project in xcode (from finder): notice that the storyboard and a few other files are in red. Delete the references, re-add them. Boom, it works.
Note that deleting the reference to the storyboard and readding it, even with deleted derived data, was not enough. The rename of the entire source folder was necessary for me.

Linking Storyboard View with Costum View Controller?

i am fairly new to IOS programming, and i am having small issues linking the view inside the storyboard with the ViewController i creat.
I can do it programmaticly, by editting the storyboard source, but i cannot seem to do it via the interface.
<viewController title="Navigation" id="2" customClass="ViewControllerNav" sceneMemberID="viewController">
works, but if i go in to the interface and set the Costum class to ViewControllerNav, nothing happens, and i get a error on compile?
(It dosnt even show up when i start typing "ViewContr...." etc, its like the class is not registerd there)
I had the same problem and deleting the XCode cache has fixed this for me (assuming that IB can't see your class when you type it's name). Firstly quit XCode, then delete the cached files by deleting all of the contents of this folder /Library/Developer/Xcode/DerivedData
Doing this and then opening xcode again should fix the problem
Close out xcode and re open i bet it will magically be there. Seriously though, if you can type in custom class in storyboard source then theres nothing wrong with your code, you should even be able to type it into where you type in custom classes, just sometimes after freshly creating it, it wont show.

Xcode 4: Creating a UIView xib, not properly connecting

I'm trying to create a nib that contains a view that will be embedded in a TableViewCell. I've created the interface and implementation files, ResultCell.h and ResultCell.m. They're stock, out-of-the-box, no code changes.
I then create an empty XIB file, and drag a UIView onto it. Then I click File's Owner, and set the type to ResultCell. I click the view, and set its class to also be ResultCell.
Here are the problems I have:
When using the Assistant Editor view (which I live in), the ResultCell.h file doesn't appear when I'm viewing the ResultCell.xib file. I have to force it to load by clicking on Automatic and selecting the file.
When I drop a label in the view, and then Ctrl+Click and drag to the .h to create an outlet, I get this error message: "Could not insert new outlet connection: Could not find any information about the class named ResultCell."
I've tried creating and re-creating the view, and it's just not working, and I've started to lose my patience. Any help would be very, very appreciated!
I got into a similar state just today. It was very odd: I could attach any XIB (new or existing) to any already-existing ViewController class in the project, but I could not create a new ViewController and have it attach properly to any XIB. The "assistant" functionality didn't work, nor did the IB-to-headerfile-connection functionality.
Closing the project and re-opening did not fix it.
Quitting XCode and restarting did not fix it.
Creating a new project and testing the functionality worked fine, which led me to believe there was something corrupt in some cache somewhere.
My solution
Close your project.
Go to the ~/Library/Developer/XCode/DerivedData folder and REMOVE all subfolders there referencing the project you are working on.
Open your project in XCode. The problem should be fixed now.
Incidentally, just running a full clean did not seem to clear things up. I had to trash the derived data. I'm certain I got into this position because of git games I was playing, but I had no idea how to get out, because even switching back to earlier git revisions didn't help. (That was a big clue also that it was something untracked by the project itself.)
This might not work for your specific issue, but occasionally I get that error when working with newly created nibs. Deleting and recreating the nibs and View Controllers with the same names as before didn't resolve the issue, but relaunching Xcode did.
I just had this problem and restarting Xcode did not fix it. I removed the class files from the project then added them back in and it started working.
I had the very same issue. And I tried :
Restarting Xcode
Deleting Derived Data
And many more
None of that worked. What worked for me was simply to :
Remove 'suspect' class files from Project (just remove References, huh?)
Re-add them to the project
And done!
If restarting Xcode doesn't work of you, I have found that Toggling the new .m file out and back into the target membership works.
This helped me out:
from the project file Panel (left side) select the xib file that is broken.
Click on the Files Owner icon from the editor view.
from the properties Panel (right side) select the third tab (at the top)
specify the "Custom Class"
If you do not see a "panel", please have a look at the top-right corner of the window and enable the proper "view" buttons. If you don't see the "view" buttons then click on the top-right most capsule button.
I noticed that .m file was moved inside en.lproj folder.
Just delete (reference only) the .m file from the Xcode and moved .m out of the en.lproj. Add it again.It will fix the issue.
Don't worry, you will get all your connections back.
I encountered the same problem today. Restarting XCode did not fix the problem for me. I was able to get things back to normal by using "Delete" option of "Derived Data" of the project that can be found under Organizer. Organizer says that, "Derived Data includes index, build output and logs". I guess that either index or build output was causing this issue.
in XCode go to organizer, click project, click delete derived data... than clean the project
In my case it helped to make a "fake change" (just a space) to the according header file.
ResultCell should be a subclass of UITableViewCell
You should not drag a UIView onto the empty canvas, you should drag a UITableViewCell
You should select the cell and change it to be your subclass
The owner is most often a controller with an outlet to the cell
Everytime you want to load a new cell, call:
[[NSBundle mainBundle] loadNibNamed:nibName
owner:controllerWithOutletToCell
options:nil]
after loading, use the cell, and set the ivar/outlet to nil, ready for the next load
there are other ways, but this is common
Perhaps what worked for me is this.. (Xcode v4.5)
This did not work
I was trying to control drag into the interface definition of my .h
#interface SearchViewController : UIViewController
#end
This worked (may be that is how it was supposed to work, I did not know it before)
See the open and close brackets. Control drag and drop after the closed bracket.
#interface SearchViewController : UIViewController
{
}
#end
I had a similar problem with a project written in Swift.
What worked for me was setting up the IBOutlet in code like this
#IBOutlet var foo: UIView?
and afterwards connect it to Interface Builder by dragging to the little circle that appeared right next to the code line.
None of the above workarounds resolved the connector issue for me so I shelved my pet project until I came across the following stackoverflow thread:
https://stackoverflow.com/a/15873770/2846800
I had been working on a large project in a previous version of XCode and had turned off indexing. By re-enabling indexing my issue is now fixed:
defaults delete com.apple.dt.XCode IDEIndexDisable
I can now use the D&D features of Interface Builder. I hope this can help others...
This problem seems to be an Xcode bug which creeps up mostly when you replace a file with a new file of the same name. Xcode seems to keep a reference of the older file of the same name.
Anyhow, the way to fix this seems to be:
Clean the project
Click (Your Project) in Project Navigator view. Go to Compile Sources under Build Phases of the target for which you are building. Remove and add back the .m file which is giving you trouble.
Alternatively, in the File Inspector (Utilities view) of the NIB file, under Target Membership, uncheck and check back the target name.
Restart Xcode for good measure.
Of course, deleting and adding back the .m file alone should fix it too.
(Steps 1 and 2 alone fixed it for me.)
If you copied files from other project just make sure you check the 'Add to targets' box
Renaming the class files may unlink them from the XIB. This answer helped me discover this:
Ctrl-Drag from button to method not working. Xcode/Interface Builder
Check that your custom class is set correctly in the Identity Inspector.
Similar symptom, but different cause.
Apparently I hit backspace when the focus was on the assistant view, because one of the standard file template comment lines went from // to / which results in the file not compiling.
Fixing the comment allowed the SDK to parse the file, recognize it as a UIViewController, and add in the outlet.
SO -- if you have this problem, do a build or analyze to see if there are errors that need fixing in your view controller .h file. THEN try the other solutions.
This might be an old topic but just in case anyone has the same issue in future, try deleting the associated .xib,.h and .m files and create new ones. For me, the UIViewController in my .h file wasn't purple and even backspacing and typing it didn't help.
Just put a line between the #implementation and the #synthesize. Most of the answers here have that in common. It worked for me
#implementation
#synthesize
I just tried everything here and nothing worked (using a Developer Preview for Xcode 5).
The only thing that worked for me was to put this incomplete line in my interface file:
#property (nonatomic, weak)
For whatever reason, adding outlets and actions by drag and drop from a .xib worked fine after that.
For me wether cleaning nor deleting the DerivedData solved the problem.
I tried to delete and recreate my UIViewController class several times and got the problem again and again.
Then I recreated the UIViewController class and gave it a different name. This solved the problem for me.
I have had this problem and found a solution not listed above.
In my case, I could see there was something wrong in the class .h file because my custom view controller did not recognise the class : UIViewController (it was in black not purple). All the other custom view controllers had the : UIViewController in purple.
in my case, and possibly yours, I needed to add the class to targets/build phases/compile Sources drop down. The .m needs to be added. All of the other .m's were there but not this one.
Once I added it, the :UIViewController appeared in purple and everything worked fine.
For my case, I have multiple project with some other dependencies. I tried to solve it by deleting derived data or restarting XCode but it couldn't work. In the end, I tried the following method and it works:
Go to target > Build Target Architectures Only > and set both Debug and Release to NO.
Set this for all projects in the workspace and recompile to make sure there is no other errors. Maybe not making sense but somehow did it. During my compilation, I had other issues such as linker errors and symbols not found.
For *.m file of this class view open "Show File Inspector" and unchecking "Target membership" for this project, then do Clean (menu Product->Clean) and checking again "Target membership" for this project
Using Swift, I had a similar problem. I found out that the comments were part of the problem for me.
I leveraged the default view controller, did some work in it, then created a second view controller by copying the entire first one, stripping it down to only viewDidLoad() and renaming the class to TestViewController. Builds worked, code executed. All was good.
When I went to drag a UITextField in to create an outlet, it would not let me. I noticed that my comments still said "ViewController.swift", so I changed the comment text to "TestViewController.swift" and rebuilt. I was able to connect my outlet!
I wonder if what occurred is that the Indexer was reading the comments as well as the class names, and somehow got confused.
Hope this helps someone out there.
I just had to delete the derived data folder. You need to click Window -> Organizer -> Projects -> Delete Derived Data
AND RESTART XCODE.
You should be good to go!
The new class not being part of the "TARGET" is likely the root cause of this, as alluded to by some of these answers.
When creating a new cocoa class, by default the option to add new class to target is by default checked, but should, for any reason, this not be checked when you hit save, you will have this issue.
Any of these workarounds that add the new class to the apps target will work to resolve the problem, and is something that all these 'fixes' have in common.
I think XCode likely has some bugs that causes a new file to, by default, NOT be added to the apps main target. The fix boils down to adding your 'broken' class to the main TARGET
In XCode 7.1, adding the connector at a different point in my source file worked. The error I was getting referred to one of my variables as if it were a class (didn't make any sense). The error stopped occurring when I added the connector below that variable instead of above it.
There is also a diferent reason for the issue ... if you have created new cocoa class file with template other than ios ... i can happen...
The solution would be to delete it and re create it with right template

Resources