Phill Campbell
Phill Campbell

Reputation: 1305

Can you have two implementations of a ViewController with the same name for differen targets?

(This is a simplified description of a very large project)

I have an XCode project that has two targets. The only difference between the two targets is there is a different implementation of a a viewcontroller for each target.

The view controller has a xib and is just a simple view with a UITextView inside. Let's call it TextReportViewController.

Implementation "A" has printing, so it has a button that allows you to print the contents of the UITextView. It is in a group called "A".

Implementation "B" has email, so it has a button that allows you to email the contents of the UITextView. It is in a group called "B".

Target "A" includes in its "compile sources" the .m file from group "A" and "Copy Bundle Resources" takes the xib from group "A".

Target "B" includes in its "compile sources" the .m file from group "B" and "Copy Bundle Resources" takes the xib from group "B".

I added "B" after "A", that is, before I decided I wanted another target things were working with "A".

When I edit "B" xib file and I examine the "file's owner" and in the identity inspector I click on the arrow for the custom class type it goes to "A's" .m file, not "B's" .m file.

When I look at the outlets for the file's owner it is a combination of the outlets from "A" and "B".

I added a control to "B's" xib and in the assistant editor I had the "B's" .h file shown and I drag a button to it to create an IBOutlet and it puts it in "A's" .h file instead.

What will I have to do to make sure target A is using A's .h file and xib file and target B is using B's .h file and xib file.

Note that when I added target "B" it made a folder called "B". So the files are located like this:

Root:

TextReport:

    TextReportViewController.m

    TextReportViewController.h

    TextReportViewController.xib

B:

    TextReport:

        TextReportViewController.m

        TextReportViewController.h

        TextReportViewController.xib

Also when I am editing the "B" xib file and I show the assistant editor and select "automatic" to show the .h and .m files, it shows four, the two .h files and the two .m files.

Upvotes: 0

Views: 1055

Answers (2)

Max_B
Max_B

Reputation: 1027

This is a late answer, posting it for the record. I ran into the same situation and searched for a solution. Since I found no way to tell Xcode Interface Builder to ignore other target files, I set up this workaround:

Say that in an iOS target, one have a class ViewController and want to make an MacOS version for a MacOS target storyboard while using part of its original implementation.

  1. first make a category to host all common methods : ViewController+common and add mutual include to keep iOS target up and running
  2. Then in MacOs target directory, create a ViewController_X class to implement target variant. This class imports the common category
  3. Since the common category imports the original ViewController.h one need to create a stub version of ViewController.h on MacOS side. This stub file is a header only file like this:

#import "ViewController_X.h" @compatibility_alias ViewController ViewController_X;

Now in the MacOS story board, use the ViewController_X classe and only see the relevant IBOutlets.

Also, any common files including ViewController.h will see the appropriate class for its target.

Upvotes: 0

TotoroTotoro
TotoroTotoro

Reputation: 17622

I suspect you can't have this kind of setup. Even though each of your targets uses a different copy of your VC, you have to have both copies of the files with the same name in your Xcode project. This is when the confusion begins. When you specify the owner of a xib in IB, you provide the name of the class, but not the path to the file where it's defined. So you can't really know which of the two files is being used.

I'd suggest using different names for different targets.

Upvotes: 1

Related Questions