Is there a good way to detect stray IBOutlets prior to running?
Like if I added this code:
#interface MyViewController()
#property (nonatomic, weak) IBOutlet UIView *magicView;
#end
Then I connected it in Interface Builder. Some time later, I remove the outlet but forget to remove it in, say, my iPad storyboard. When I run the app, it'll crash with a KVO error at runtime.
IB shows a warning on them, but you need to click each view controller and search for yellow exclamation points. I'd rather just see a list, compiler warnings, something more prominent.
Interface Builder saves to XML files that can be examined in with a text editor in an effort to find zombie connections.
Related
I am learning Stanford CS193p course with Xcode 4.3.3. I think the screenshot below is pretty much self-explaining. But i will describe the problem with words anyway.
I control-drag a UILabel from storyboard to corresponding implementation file to make the IBOutlet #property. Then I see two connections displayed when clicking the filled circle in the left side of the editor where shows line numbers. I don't know how to delete it.
Moreover, I see only ONE connection in storyboard's connections inspector of the UIlabel.
More weird, when I try to set the UILabel's text inside the setter of a public preperty, it fails to update the label's text:
-(void) setQuestion:(NSString *)question
{
_question = question;
self.questionLabel.text = question;
NSLog(#"The quesion is %#",question);
NSLog(#"The quesion label text is %#",self.questionLabel.text);
}
I use two NSLog to debug and get the following. It shows the NSString *question is #"What do you want your label to say?", yet the self.questionLabel.text is null. The value assign fails. I suspect this relate to the duplicated connections thing mentioned above.
2012-07-29 04:03:53.817 Kitchen Sink[18628:f803] The quesion is What
do you want your label to say?
2012-07-29 04:03:53.820 Kitchen
Sink[18628:f803] The quesion label text is (null)
The following is the screenshot showing the duplicated connections. I am probably missing something obvious, please help.
I would stalk this up to Xcode storyboard wonkiness.
First, try a clean of the project and a re-build.
Should you still have that problem, manually define the #property without dragging from the AskerViewController view to the AskerViewController implementation (.m) file by doing the following.
Cut that IBOutlet #property line in the interface section of the implementation (.m) file for AskerViewController. Click the label in storyboard. Disconnect any binding that label to the AskerViewController. Clean the project. Then, paste back in the property, and control-drag from the label to the view controller orb at the bottom of the AskerViewController view in your storyboard file.
I'm messing around with Xcode and Interface Builder. I want to make a timer or clock type app. What type of object should the numbers be? Just a 'Label'?
You could use a 'Label' (which is a modified NSTextField), or you could use an non-editable NSTextField or even a NSView with a custom drawing method.
If you're still learning, I suggest you use a Label and go from there.
When linking to your AppDelegate (or whatever object you're using), remember to create an IBOutlet on your property declaration so you can link your label, like so:
#property (nonatomic, assign) IBOutlet NSTextField *clockLabel;
And setup the clock value using the setStringValue: method:
[self.clockLabel setStringValue:myString];
If you just want to display the numbers, then UILabel will be just fine.
And, if you want some nice looks then i would recommend some nice graphics images for the numbers.
Once the code bellow is executed, the textfield's text doesn't change in the UI to "Fly" but the second NSLog does print "TextField: Fly" as it should.
#property (strong, nonatomic) IBOutlet UITextField *typeTextField;
....
UITableViewCell* cell = [self.theTableView dequeueReusableCellWithIdentifier:#"TypeCell"];
self.typeTextField = (UITextField*)[cell viewWithTag:1];
NSLog(#"TextField: %# ", self.typeTextField.text);
self.typeTextField.text = #"Fly";
NSLog(#"TextField: %# ", self.typeTextField.text);
Any help would be much appreciated.
You almost definitely forgot to connect the outlet for the UITextField in interface builder. Bring up the .xib file that that typeTextField is visible in, click on typeTextField, then show the Utility pane (the one on the far right in Xcode 4+). Click the Connections Inspector (the one that looks like a right arrow) and drag a New Referencing Outlet to your File's Owner.
When you don't connect the UITextField you drew in Interface Builder with the IBOutlet that you identified in your source file, both UITextFields get created as separate entities. You can make changes and work with the valid typeTextField with a broken IBOutlet, but it'll never appear on your view.
Consult How to connect an IBOutlet from an UITableViewController directly to custom cell? and http://www.youtube.com/watch?v=d_kO-J3DYvc on properly wiring your custom UITableViewCell objects.
When I use bindings I don't need to include the IBOutlet macro.
Ex.
#property NSString* stringToBind
Why is this?
When do we use the IBOutlet macro and when do we leave it out? I am confused because I thought we include the IBOutlet macro to when we want to use it as an object with interface builder.
What would happen if linked to an object (like would would normally do with an outlet) but excluded the IBOutlet macro? In other words created an outlet without the IBOutlet macro? It is optional in all cases? Is it just used to make things easier, so that they are detected?
Thanks in advance
As far as the compiler and linked are concerned, IBOutlet is a no-op. More specifically it is #define'd to nothing. Xcode and Interface Builder use static code analysis to figure out what to do, but it will have no impact on compiling or linking.
The NIB that is generated contains the names of the properties to connect when it is loaded and those names are resolved the same way that all other properties are resolved.
If you remove IBOutlet from a property declaration introduces a risk that the next time the NIB is generated, the link connection won't be made. I am not sure how Interface Builder handles that.
I created three IBOutlets in a .h but when I go to Interface Builder and go to Referencing Outlets or right click on the object that I want to connect to one of the outlets. It just doesn't display the outlets I made. How do I find and connect the images in IB to these. My goal is to make custom buttons, I have the images out in IB and have set the highlighted state. And I want the images to auto switch to the highlighted state when touchupInside is triggered.
#import <UIKit/UIKit.h>
#interface FirstViewController : UIViewController {
IBOutlet UIButton *brown
IBOutlet UIButton *red
IBOutlet UIButton *blue
}
-(void)brownPressed
-(void)redPressed
-(void)bluePressed
#end
If the code you posted is accurate, you are missing 6 semicolons. Interface Builder won't be able to properly parse the header file without them and so it won't show any IBOutlets.
Try changing the code to the following:
#interface FirstViewController : UIViewController {
IBOutlet UIButton *brown;
IBOutlet UIButton *red;
IBOutlet UIButton *blue;
}
-(void)brownPressed;
-(void)redPressed;
-(void)bluePressed;
#end
This answer is in reference to Xcode 4
If you changed the name of the .xib manually, don't forget to check these IB setting in the right pane:
Identity and Type: File Name
should be:
newName.xib
Custom Class : Class
should be:
newName
...
I was also experiencing this problem - after changing the .xib filename manually. I have no idea why renaming by refactoring was grayed out but that was my first attempted solution to rename. I then made some IBOutlets, but dragging to the File's Owner didn't give me any options to connect to it.
I right clicked on File's Owner and noticed some warnings telling me there was no IBOutlet by the name of the variable it tried to reference.
Basically, since I changed the .xib filename manually, at least one of the above configurations did not automatically follow (makes sense since I did a manual operation).
This happens to me try connecting the touch up inside connection of the button to files owner and then it will show up