Error in Xcode 6 - view controller does not have a

2020-01-29 08:43发布

问题:

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.

回答1:

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 !



回答2:

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 :)



回答3:

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.



回答4:

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.



回答5:

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!



回答6:

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.



回答7:

Why dont you update your xcode 6.0.1 to xcode 6.1, Apple have fixed many bugs.

Download Xcode 6



回答8:

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).



回答9:

@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.



回答10:

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



回答11:

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.



回答12:

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.



回答13:

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.



回答14:

Try to add the header file to the project, if it's missing.



回答15:

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:

  1. Close xcode project
  2. Delete derived data for project (Window->Projects)
  3. Rename the entire project/source folder, ie. project -> project-1
  4. (Update source-management bookmarks like sourcetree as required)
  5. 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.