xCode Storyboard: Connecting IB Elements to a source file - objective-c

I don't understand how to connect elements to a source file. I have a custom UITableViewCell and I want to drag a custom label from it to my CustomCell.h file, but when I drag it over it doesn't do anything..and in storyboards there's no files owner. Is there a short answer to this?

Open the assistant editor (from the toolbar) in xcode (it'll open two views side by side, one view with the xib and the other with the corresponding .h file). control drag from your label to the created property.

Yeh that is pretty weird the way they've done this. As you may know in interface builder you usually drag from files owner TO the element when you want to link to an outlet. It is the same case in this example. So after you've set the class of the cell, in which you have the label. And you've created an IBOutlet UILabel in your CustomCell.h, simply control drag FROM the cell in which you have the label, and you will see your desired option to link it.

Related

Change a button's background image programmatically in cocoa

All of the topics that I have searched are outdated or not complete in Obj-c.
I need to learn how to change a button's background image programmatically, when having the actual button in the InterfaceBuilder. (This sounds odd, but I need it for NSCollectionView as I have many similar button with different background images).
In the interface builder I drag a button onto my view window, what should I do after?
I understand that:
I need to create a NSButton Class
Connect the button from the interface builder to the code
Set the image
I have been struggling with this.
So did you connect the button to an IBOutlet property? If so then all you need to do is use [button setImage:]
If you haven't already done so, make sure the object instance that you want to change the image from is in interface builder, I.e has been dropped in as one of those blue boxes. Then if you set the object's class and have an IBOutlet property in the header file you can just drag the button outlet in the outlets tab (looks like an arrow) to the actual button itself to link the two
Edit: So it appears you're having trouble with the actual connecting part of the button? Chances are your IB file looks a bit like this:
Look for the objects section in the left hand list. These are the actual objects in your code that you can connect your button to. You might see an app delegate object there, which is included in the default IB file generated when you first create a project. If you want to handle the image changing in your app delegate, then simply add this property to your AppDelegate.h file to create an outlet:
#property IBOutlet NSButton *button;
If you go back to interface builder and select the app delegate object, you can see the outlet that you just created under the outlet tab:
Drag the little circle thing to the button to connect it, that should be the easiest bit.
But I'm going to just presume that you want to call it from somewhere else other than your app delegate, and for that we'll need to do some more explaining. If the class you want to call it from is a subclass of NSView and is already in your interface builder, you can just add that line to your view's header file and it will appear under the view's outlet tab.
If you want to call this from another object that isn't a view or such, you'll need to do either two options:
Create the object instance in interface builder. This means that instead of creating in normally with alloc] init]; etc. you'll have to actually drag in an object into interface builder. This can change the structure of your object quite a bit as you'll no longer be able to create it at will, since it will automatically be instantiated whenever you load your .nib file. Also important to note is that your init function will not be called anymore and you'll need to use awakeFromNib instead. If you do choose to go down this route, just drag over an object:
Add your outlet property to the header file:
Set the object's class:
And connect the outlet:
If making objects XIB loaded just isn't your thing, you can always just connect the property outlet to your app delegate/view controller and access it from that instead. Hopefully this clears things up, if this was the problem you were having.
For iOS:
Don't need to create a NSButton subclass.
You only need to add button on Storyboard, set the IBOutlet property for your button (ctr+drag from your button to your view controller), and set the background image with:
[myButton setBackgroundImage:[UIImage imageNamed:#"ImageName"] forState:UIControlStateNormal]
For MacOS:
You can use
setImage:
as describe on Apple Doc, and changes its size/position
(Sorry for my bad English)
ofcourse as Duukee Said,i think no need to create any NSButton Or UIButton Instances Manually When We have an object in Interface Builder,We can just use it's outlet as follows,
UIImage* Desired_Image=[UIImage imageNamed:#"yourimage.png"];
[My_Button setimage:Desired_Image forState:UIcontrolstateNormal];
HTH!Happy Coding :)

Xcode, can't find my class which I had created

I'm playing arround with Xcode (for iOS) and added a View Controller to my Storyboard. Now I've created a Class (File -> New File -> Objective-c Class) and made that a subclass of UIViewController. Now the .h and .m files are generated and listed on the left side in Xcode, but when I want to change the "custom class" of my added View Controller there is nowhere my class which I had generated. Am I doing something wrong?
Thanks in advance.
I've had the same problem. Quitting and opening Xcode will make the Class show up in the list.
edit: I've also recently found pasting the Class name will work in 5.02 Xcode.
I had the same problem. But I discovered that I was clicking inside the "window of storyboard" and was selecting a view, so with no viewController related to. My solution was: to click inside the owner, or the viewController from the left list, and then change the custom class.
There are multiple reasons and solutions....
#1
Select project icon in "Project Navigator".
Then go to your Target's Build Phases -> Compile Sources.
Finlly, click the + button, and add your *.m or *.swift file.
#2
In one case, the same file was added multiple times to project:
Remove all instance of the file.
Add file back to project (once).
Finally, select the file in "Project Navigator", and in the "File inspector" on the right, look at the "Target Membership" section. The app target (not the Test target) should be checked, similar to this:
#3
ANOTHER WAY IS
Save all my work;
Just quit Xcode & load it again;
Then I was able to insert the new outlet connection successfully.
#4
Close the project you are working on with.
Delete the【DerivedData】folder of you project.
Said folder may be:
Inside your project's folder,
Or, inside ~/Library/Developer/XCode/DerivedData/(your project)/
Or, somewhere else that was setup by you.
Restart you project.
#5
Simply Clean your Project,
Then Build Again,
Finally, check and/or run.
I'm not sure what you mean by " but when I want to change the "custom class" of my added View Controller there is nowhere my class which I had generated ". It seems like you are trying to make the UIViewController in your storyboard to be of the same type of your custom UIViewController subclass.
If that's the case, then select your UIViewController (make sure you're not selecting the UIView within that UIViewController)
With the UIViewController selected, open the right hand view and open the Custom Class tab, then start typing in the name of your UIViewController subclass.
If that isn't working, then you need to ensure that the in the .h file you say:
#interface MyCustomUIViewController : UIViewController
If it still isn't working, ensure that in storyboard you are selecting the UIViewController that you put down. If you are trying to fill in the Custom Class of a UIView with a UIViewController subclass, it shall not work!
If it all still doesn't work, try COMMAND+SHIFT+K, and COMMAND+B to Clean and Build!
Good luck.
Just a quick advice in case you are about to give up: Do check that the item you have in the storyboard is compatible with the custom class you created that you are trying to assign. For example, you cant choose a UIView based class if you have a UIImageView as a component in your storyboard. In that case, it simply wont appear in the dropdown list.
You don't have to completely quit Xcode. Just close all your Storyboard tabs completely and reopen a new tab by double clicking on it.
Solution for my situation - just close all other xCode windows (i got opened few projects at same time).
Also sometimes help cleaning of project.

Ctrl-Drag from button to method not working. Xcode/Interface Builder

I am building an iOS app and i have most of the code/GUI built (its a single view app). Now i have the .xib and controller.h file next to each other and I want to associate a button click with a certain action. Everytime I try nothing happens. The UI indictor that I am dragging shows up but it wont let me connect to the method. This happens for all the objects/methods.
I have an almost identical project that works just fine too!
Select your XIB file and then select "Files Owner" which is the yellow cube.
In the Utilities Sidebar, select the Icon with the circle and arrow inside of it (which is the "Connections Inspector").
You can then Control + Drag your to your button.
//// Edit ////
After investigating your project...
Pretty sure you deleted a storyboard and then added a .xib but didn't set it's class, or link it's view as an outlet.
To do that:
Select the new view (XIB), then selct the main view, and in the identity inspector, set it's class to your custom class name "AVYSViewController."
Switch to the connections inspector and Control + Drag the "view" outlet to the main view in your XIB.
Also, your project was looking for the MainStoryBoard.storyboard file. So get rid of that setting by selecting your project, then selecting your target, and in the "Summary" tab, delete anything in the "Main Storyboard" field.
Note: I checked to see if your project settings were valid before I deleted the storyboard setting and it returned NO errors, so beware of this problem for future projects.
I think i'm reading the same book as you (Big Nerd Ranch's "iOS Programming"). If so then yeah, I had the same issue as you. Admittedly I am using my laptop to vnc through to a mac box to work on xcode, so I thought it was vnc not capturing the keyboard properly, but turns out it wasn't vnc as I could right-click/cntl-click drag on other things.
Admittedly again I didn't do much research on why it wasn't working, but in the meantime I just right-clicked on the button, which shows the events that the button emits, then click-dragged the "Touch Up Inside" event and dropped it on the controller/owner, which then displayed the actions/methods to hook the event to. If you look on page 17 (if you are reading the same book) then that is the event that was hooked up.
Sorry if there are any obvious glaring errors with what I just say, have only just started reading this book today after a big lunch.
I think you should rephrase you question.. It's very confusing. But if i understand what you are saying, you now have the files open side-by-side in the using the assistant editor. The Xib file on the left hand side, the .h file on the right. correct?
Now you are trying to link the event in the control, to the method you have defined in the .h file. right?
So if you are in fact seeing the methods in the .h file that you want to link to, but the editor is not letting you link to it, then chances are that you have have the xib file side-by-side with the wrong .h file. Are you sure that the .xib file and the .h that are side-by-side are both called controller ? controller.h and controller.xib ?

Xcode: how to reset. No longer able to create outlet for newly added control in XIB

I have several XIBs following this pattern: a View Controller subclass containing a View subclass. I put my controls on that view.
Just now, I tried to add a new control to the view. I can do this, but I'm unable to connect the new control to an outlet. The assistant editor won't display the view subclass header either, though it will for a short while if I restart Xcode. Some other XIBs I have don't have the view controller object but instead have File's Owner set to a view controller subclass. They show the same problem.
Here is the Objects bar in IB:
"Matrix Editor View Controller" is a UIViewController subclass. This shows up fine in the Assistant Editor's "Counterparts" menu and I can drag from UIControls to it to create outlets & actions.
"Popup Cell View" is a UIView subclass. This is where the majority of my outlets and actions live. Previously this would show up in the Assistant Editor "Counterparts" menu and I could drag between it and my controls and the code quite happily. Now, whenever I add a new control in to this view, I can't drag from the Popup Cell View object to the new control, nor can I drag from the new control to the Assistant Editor window if it's showing the Popup Cell View header.
I recently had to upgrade to Lion (and hence Xcode 4.2.1 build 4D502); the last time I added controls to my XIBs was under Snow Leopard with the latest Xcode on that OS.
I believe that Xcode's caches or some internal state are out of whack. I have tried:
Restarting Xcode
A clean
Removing and re-adding the view subclass from the project
Deleting the DerivedData folder's contents from ~/Library/Developer/Xcode
And various combinations thereof. What else can I do to force Xcode to re-scan everything and allow me to connect up my controls to outlets?
Edit: I have noticed that I can create outlets in the View Controller for my new controls; I'm just not able to do this on the View subclass itself which is where I want them. I already have many on there. Could my new version of Xcode really be telling me that I'm not allowed to do this? Surely not. The Assistant Editor's Automatic mode changes every minute or so between two counterparts (the VC .h and .m) and four (the VC .h/.m and the View .h/.m).
Edit: I was able to connect an outlet, but only via this convoluted method:
Force the assistant editor to open my View subclass
Manually typing in an outlet for my new control
Dragging from the outlet in the assistant editor to the control itself
So something is very clearly broken. How can I fix it?
Very frustrating!
Thanks
When I have this problem, it is usually due to a mismatch in the class type somewhere. Verify that the class name of the object in you xib matched the class name in the interface and the implemetation files.
I'm a newb - I recognize this is a kludge, but hope this helps somebody. Sounds kinda sorta like the problem described above.
Suddenly, xcode refused to "insert new action outlets" into my AppDelegate.m (using control+drag, from a newly added xib element, into the class #implementation). Also, Xcode does not display that little-dark-circle next to each IBAction method in the .m file, like it used to.
Xcode allows drag of the blue-line from the 'source xib element' -- but would not show the "Insert Outlet or Action" & blue-circle-line with destination-highlight at the 'target line of insertion', in the .m file.
However, I can insert new outlet into my AppDelegate.h, using that above-described blue-line drag-method. That file is adorned with the cute little-black-circles, properly.
The kludge-around I came to was this
add 'bogus #implementation code' to the AppDelegate.h, as per:
#if 1
#implementation AppDelegate
#end
#endif
control+drag from the new xib element into/inside that #if 1 #implementation region in the .h
viola! xcode now displays the desired "Insert Action" & blue-circle-line target at the position for insertion. I can assign the method name and express whatever semantics I wish. Xcode source-editor shows a cute little-dark-circle to the left of the 'autogenerated' -(IBAction) line. For example, I added imanewxibobject as per:
#if 1
#implementation AppDelegate
- (IBAction)imanewxibobject:(id)sender {
printf("%s: hi there!\n",\_\_FUNCTION\_\_);
}
#end
#endif
(1+2)=3. Copy & paste that -(IBAction)imanewxibobject:... function into my actual AppDelegate.m (where it really belongs).
(2+2)=4. Turn that #if 1 (in the AppDelegate.h) to #if 0
(3+2)=5. recompile. Kablammo, the new UI element is alive with semantics!
I tried all: cleaning the project, deleting the files from the project, etc.
I peered into that .xib file and found it contained lots of what appears to be cross-referenced identifier indices, kinda what you may expect to see, in a file of this sort.
So I still don't have those little-dark-circles beside my -(IBAction)s -- but at least the code compiles, and I see the correct method-label string, for the Xcode Utilities->"Show Connections Inspector", with respect to the new UI element.
Anybody know how I can restore those cute little-dark-circles beside my "-(IBAction)"s? I find them very cute, and wish to have them back. I don't see any "Display cute-little-dark-circles" menu options. Bummer. Maybe in the next Xcode?

How to resize view in XIB?

I'm trying to follow these instructions with Xcode 4, but am unable to resize the xib. Does anyone know how? ( The width and height cannot be clicked upon ) Also there is no UITableViewController so I just used UIViewController.
EDIT - My goal is actually not to use the popover controller, I simply want to create a modal login dialog following the example here: How to resize a UIModalPresentationFormSheet? and am trying to make the view controller 300x400
A popover controller has to have a
view controller inside of it. So we
need to create a new view controller.
Go to File > New File > Cocoa Touch
Classes > UIViewController >
UITableViewController subclass >> With
XIB for UI >> Targeted for iPad. Name
it OptionsViewController. Drag your
viewcontroller.h and viewcontroller.m
files into the classes group and the
new XIB into the resources group.
Open the XIB and go to the Size
inspector. Change the width and height
properties to 250 and 300. Save your
file and close interface builder.
It appears that in Xcode 4.5+, just to be annoying: Apple changed this again.
Instead, you now have to:
select the viewcontroler
select the "size" dropdown
set it to "Freeform"
...which removes their "block" on editing the size fields.
(this is pretty close to how I expected it to work in the first place, but I have to say it's not easy to find - you have to edit a different value on a different screen (with no help from Apple), in order to un-break the edit field on the correct screen. Not good design!)
Interface Builder (and the IB equivalent in Xcode 4) won't let you change the size (or autosizing attributes) of a top-level view when any Simulated User Interface Elements are enabled.
Select your view, and in the View Attributes inspector set Status Bar, Top Bar, and Bottom Bar to Unspecified. You should then be able to change the size.
In swift 4.2 interface builder won't let you change the size
click on the view and open the attribute inspector and change top, bottom bar to none and size to freeform. Now you are good to go for the change in size.