I'm currently refactoring a couple of view controllers that share a few IBOutlets and IBAction methods. I moved the outlet declarations and the IBAction method into a superclass, cutting these out of the subclasses.
Now, when I open up Interface Builder, I find that I can't see the outlets or actions declared in the superclass. The connections still exist, as I'd wired them up before the refactoring, but they're grayed out. (It's important to note that the connections also WORK, as my action fires on a button press, and my outlets are modified properly.)
The question is, how can I get interface builder to recognize outlets from a superclass? Is this possible, and, if not, what do you all recommend?
(Just for fun, here's my superclass header file:)
#interface TFMainViewController : UIViewController {
UIImageView *logoImage, *thinkfunImage;
UIButton *buyFullButton;
}
#property (nonatomic, retain) IBOutlet UIImageView *logoImage, *thinkfunImage;
#property (nonatomic, retain) IBOutlet UIButton *buyFullButton;
-(IBAction) buyFullVersion;
#end
EDIT: in case anyone's wondering, I'm using Xcode and IB 3.2.5, with the iOS 4.2 SDK.
I didn't realize it was even possible to connect to superclasses in interface builder until about an hour ago. Since this was the only question I could find regarding how to do this, I'll add my answer, even though this question is old. My answer is with regard to Xcode 4, not Xcode 3.
As far as I can tell, you can't connect to outlets in a superclass using the assistant editor, but you can do it by clicking on "File's Owner" in IB. That should show all the outlets in Utilities->Connections Inspector. You can then Ctrl+Click on the outlet in the inspector (click on the '+' sign), and drag it over to your view in IB.
The solution for the problem with the IBOutlet .. is to change the class type to the Base Class in the identity inspector
connect using Control + drag and drop and
change it back to the child class
This works for me
BTW: i used Xcode 6
IB should be able to see outlets from superclasses, I have done this a number of times with no issues. Are you sure you are importing the superclass correctly (using #import instead of #class)? IB needs some way to track back to the superclass.
Switching between the super and subclass in the identity inspector allows you to connect your outlets across the classes. The only issue I found is when you attempt to do this with a UITableViewCell and its subclass. I wanted to re-assign the default textLabel and detailTextLabel instances to labels I create in Interface Builder. The workaround is to create substitute labels and then override the getters to point to these instead.
I'm pretty sure that IB only looks at the actual class you're using to find outlets, and not at superclasses. I think that the easiest solution would be to leave the instance variable declarations in the superclass, but duplicate the #property lines in each subclass.
I'm doing this in XCode 3.2.6. I started with outlets connected to a class, and then made a subclass with additional outlets. When I changed the File's Owner class to the subclass, IB showed the superclass outlets as greyed out. I switched File's Owner to the superclass, then back to the subclass and now all outlets are showing not greyed out.
The simplest way: create interface and implementation files for your subclass(es)!
Perfect example: Juggleware's awesome ShadowButton Subclass of UIButton.
Make sure to create the .h & .m files in your project.
NOTE: There is no need to #import the header files at all since this is simply a class instance of UIButton.
In Interface Builder:
Select the element you which to connect.
Go to Utilities -> Identity Inspector
Change the Class to your subclass (or superclass). NOTE: You might have to type in your subclass name and hit ENTER.
You're done!
Even if you have declared a basic class (UIButton) as IBOutlet in your header file like so...
// YourViewController.h
#interface YourViewController : UIViewController {
IBOutlet UIButton *mybutton;
}
...the class you've set in Interface Builder (ShadowButton) will overwrite it since it's in the view layer.
The best part about this approach is that your code doesn't have any messy dependency issues.
On the project I am currently working, we have a BaseViewController with a UIScrollView as IBOutlet and handles keyboard appearance/disappearance events and slides the content accordingly. At first, I could not connect to that IBOutlet, than solved the problem like this, which is similar to Sosily's answer:
BaseViewController has an IBOutlet, called contentScrollView. I can see 5 previously connected outlets, which are UIScrollViews on other UIViewControllers, created by people who previously worked on the project
I tried to connect my UIScrollView as the contentScrollView. Although my UIViewController is a subclass of BaseViewController, I cannot connect it.
I tried to connect already connected UIScrollViews as the contentScrollView. Although all UIViewControllers are subclasses of BaseViewController, I cannot connect them again, as well. So, I started to look for a trick.
I have created the same contentScrollView IBOutlet on my own UIViewController, connected the scrollView to my own contentScrollView outlet and removed the one that I have just created.
Now the scrollView is connected as contentScrollView to File's Owner, but the only contentScrollView belongs to the BaseViewController. Tested and verified that keyboard events are handled correctly.
I ran into a similar problem with a superclass, but it was due to a bug in Xcode (8.2) where Interface Builder doesn't show outlets in the Connection Inspector if those outlets have been declared with a _Nullable type annotation for Swift compatibility.
Using nullable inside #property's parentheses appears to work around the problem.
This Xcode bug seems to affect outlets in any class (ie. not just superclasses).
I had the same problem, and it turns out it was because in the superclass I had the IBOutlets declared as "_Nullable".
Example:
#property (nonatomic, strong) IBOutlet UITableView *_Nullable mySuperTableView;
When I removed the _Nullable, suddenly the IBOutlets reappeared in IB and all was good again.
(I had only set them to _Nullable because Xcode was complaining "pointer is missing a nullability type specifier"... (don't know why). )
Related
Prior to Xcode 11 I could easily attach a view (label, switch, e.t.c) to an IBOutlet defined on a superclass, if I had the inspector window open and command clicked to my view (In this case a subclass of a UITableViewCell subclass it would open the superclass in the inspector window and I could attach to the IBOutlet. In Xcode 11 however it seems that when command clicking through to the "definition" this opens in the LHS panel, which replaces my storyboard or xib file with the views superclass.
Is there any way to open the superclass alongside the xib/storyboard any more in Xcode 11? My framework is a pre-compiled framework so I can't access the source files to open them in a new panel in Xcode.
Thought it might be wise to include a class hierarchy
LargeHeaderTableViewCell -> TableViewCell -> UITableViewCell
TableViewCell is within a pre-compiled framework (Installed using Carthage)
Edit:
To frustrate matters further... I can get the class definition of TableViewCell open next to my xib file (In a panel), however it seems you can only attach to IBOutlets in the assistant editor, not in a standard Xcode panel/tab/whatever you call it.
Found an answer (eventually) in here
I realize that this question was posted a while ago, but since I just
struggled with the same issue and finally came up with a solution, I
figured I would still post my findings...
I understand the problem to be as follows (at least that's the one I
solved):
How to have class A inherit from class B, with each class having its
own XIB file with the some common IBOutlet properties? The goal being
to be able to have the super class handle the actions related to the
IBOutlets that are common to its subclass(es), while still being able
to use Interface Builder to design the interface for the
subclass(es).*
In order to do so:
Make the IBOutlet connections in the superclass from the superclass'
XIB files Make the IBOutlet connections in the subclass from the
subclass' XIB files, with the same IBOutlet property names as in the
superclass for the ones you need to inherit. Delete the declaration of
the IBOutlet variables in the subclass
I have created a custom view (Quantity View) with nib file in Swift. I have created some IBOutlets & IBActions (for buttons, labels etc.) in my custom view.
I tried to use this custom view (Quantity View) by assigning class name to a UIView in my storyboard.
It's showing me all the IBOutlets & IBActions in the Connections Inspector, as shown in this screenshot: .
I just want to show only delegate for the Custom view.
Possible Answer:
I thought I can use the -viewWithTag to get the views instead of Outlets.
But, I want to know if it's possible with having Outlets also or if there is much better way to do this?
What are the other possible ways (optimum) to handle this situation?
You can also consider the following solution:
You can take the subviews of your QuantityViews(custom view) and you can identify the specific views by its frame origin.
Note : you should know the customview subviews frame
Its not possible to hide IBOutlets from storyboard if you declare the class members as IBs (IBOutlets or IBActions).
The IBOutlets or the IBActions are just indicators to the interface builder so that it can show the names on it when you try to bind them it actually calls the setValue: forKey: method to set the view's reference to the IBOutlet property.
Now if you try to access an subview from the file's owner class without any IBoutlets you need to have a pointer to point it, so for that either you can get the reference using ObjectID which is assigned to the subview by the interface builder or you can get it using the viewWithTag: method.
The ObjectID you need to find all time when you add or replace a subview from the view, so better and convenient approach is to use tag property of UIView class.
So my conclusion to this problem is to access the views using the viewWithTag method you mentioned earlier.
I think your way is correct. But sometimes Xcode doesn't work correctly.
The following makes the IBOutlets and IBActions reappear and work properly:
Clean project your project in Xcode.
Quit Xcode completely.
Delete all contents of ~/Library/Developer/Xcode/DerivedData/.
Restart MacOS just in case.
I hope you will resolve that :)
It's been awhile since I've used Xcode and I resumed an old project I was working on awhile ago. I created a new UITableViewCell subclass. In my storyboard, I dragged a UITableViewCell onto my UITableView of my UIViewController. I changed the type of the UITableViewCell to my subclass, but when I control + drag from the UITableViewCell subclass to the UITextField, it doesn't allow me to make the connection.
.h of my UITableViewCell custom subclass
#property (nonatomic, weak) IBOutlet UITextField *titleTextField;
I must be going crazy because I thought this was something that just worked. I saw in another UIViewController that there is a custom subclass for the UITableView when I worked on this project last. I changed the subclass that was having the problem to that type of UITableViewCell subclass and I'm still not able to ctrl+drag to make the IBOutlet connection. Am I missing something here? Wasn't this something that always worked this way? Is there something new I'm not aware of? I tried using the assistant editor as well to drag it to the code, but that doesn't work either. I went back to the UITableViewCell subclass that DOES have a connection already made from when I last worked on this, and I tried ctrl+dragging to the label again, and it doesn't bring up the menu on which outlet I want to connect to either.
Another thing that is weird, is when I'm trying to type the custom class of my UITableViewCell that is already created, even though I built my project, it doesn't autocomplete it in the Class field. I'm not sure if my Xcode is having problems. Also, I don't know if this matters, but in my UITableViewCell, I Have some standard UITableViewCells as well. Any thoughts? Thanks.
It looks like all I had to do for my class name to appear in the drop down was to quit Xcode and come back in. I thought things like that would have been fixed in the IDE.
I found that I could still add the connections in the connections inspector. I don't know why they removed the very easy ctrl+drag from the left hand side of the storyboard unless I'm missing something.
I am trying trying to copy all of the functionality of this example app provided by Apple called AVCam: https://developer.apple.com/library/ios/samplecode/AVCam/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010112-Intro-DontLinkElementID_2
I am 99% done with copying this code, but I just have one final problem. I have an IBOutlet statement that looks like this: #property (nonatomic, weak) IBOutlet MediaCapturePreviewView *previewView;
According to the Apple sample code, this outlet is supposed to be connected to a View object that has been placed on top of the normal/default view.
Here is a screenshot of what the Connections Inspector looks like in the Apple example:
You will notice that the IBOutlet called "previewView" has been connected to something called the "Cam Preview View".
Also, in this screenshot, you can see that I am able to select this View object by itself and that it shows a Referencing Outlet in it's connections inspector for the same IBOutlet and View Object:
My problem is that I cannot get the IBOutlet code to connect to this View Object. I have tried the normal behavior of clicking and dragging to make the connections but it just wants to create a new outlet. It will not let me connect to the outlet that I have already created.
I have been playing with this for 2 hours now and just can't get it to work like Apple's sample code.
Any help is greatly appreciated thank you.
In your .xib file, make sure that UIView class is assigned as AVCamPreviewView instead of UIView.
A few possible solutions:
Save the file with the IBOutlet you're trying to connect up (the source code, not the IB)
Clean, rebuild
Restart Xcode.
I'm trying to show a search bar above a table with a list of recent searches that will swap to matching search results once someone enters a search term.
I want to set a custom class MySearchViewController to be the delegate for doing the search and managing the display of search results back to the table so that I can separate the code and not have conditional statements in the default controller.
I've found a bunch of examples that describe how to do this in code but I can't figure out how to do it using Interface Builder.
I've tried dragging a new viewcontroller into my xib and setting the custom class to MySearchViewController and then dragging outlets from the SearchDisplayController as hinted at here: http://goo.gl/RgmwG
I've also tried dragging an Object into the objects column and changing this class to MySearchViewController.
But I feeling completely lost and really just trying things randomly. I'm guessing that I also need to create a property/IBOutlet for the SearchDisplayController somewhere but again lost.
If anyone has a reference to how to go about this I'd be so happy!
Like most problems, it seems pretty obvious in retrospect.
Add an 'object' placeholder in interface builder (orange cube).
Change the objects custom class to the class you want to be the delegate - e.g. MySearchViewController
Remove the default outlets from the standard SearchDisplayContoller to connect with the MySearchViewController object (see screenshot)
Make sure that the new delegate has an outlet to a parent view (in my case View)
Make sure that the delegate class is initiated from somewhere
// I did this from the parent ViewConroller, but probably better from the main app delegate?
#property (strong, nonatomic) IBOutlet MSSearchViewController *searchViewController;
Hope this helps someone else who was also stuck!