Reputation: 442
I am trying to "Connect" my objects in the view to the File's Owner
using the interface builder
, but the blue line does not 'link' with it. Here is my code:
CalculatorViewController.h
:
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"
@interface CalculatorViewController : UIViewController {
IBOutlet UILabel *display;
IBOutlet UIButton *button;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
}
- (IBAction)digitPressed: (UIButton *)sender;
- (IBAction)operationPressed: (UIButton *)sender;
@end
CalculatorViewController.m
:
#import "CalculatorViewController.h"
@implementation CalculatorViewController
- (CalculatorBrain *)brain
{
if (!brain) {
brain = [[CalculatorBrain alloc] init];
}
return brain;
}
- (IBAction)digitPressed:(UIButton *)sender
{
NSString *digit = [[sender titleLabel] text];
if (userIsInTheMiddleOfTypingANumber) {
[display setText: [[display text] stringByAppendingString:digit]];
} else {
[display setText:digit];
userIsInTheMiddleOfTypingANumber = YES;
}
}
- (IBAction)operationPressed: (UIButton *)sender
{
if (userIsInTheMiddleOfTypingANumber) {
[[self brain] setOperand: [[display text] doubleValue]];
userIsInTheMiddleOfTypingANumber = NO;
}
NSString *operation = [[sender titleLabel] text];
double result = [[self brain] performOperation:operation];
[display setText: [NSString stringWithFormat: @"%g", result]];
}
@end
Upvotes: 1
Views: 6018
Reputation: 667
Im no expert on Interface Builder or Objective C, but did you use:
@property (nonatomic, retain) IBOutlet UIButton *yourButton;
and then in the .m file:
@synthesize yourButton;
Im also not sure, but you may have to 'build' it first, then go into Interface Builder and then it may 'connect' up.
Upvotes: 0
Reputation: 4656
Once you have made sure the File's Owner is set to be CalculatorViewController, make sure that the IBOutlet type in the CalculatorViewController.h file matches the UI component type you are trying to connect it to. Your header defines an IBOutlet for a UILabel, which means only a UILabel can be connected to it. If the component is of a different type, lets say a UIButton, then you would change your header file to include an IBOutlet like so:
IBOutlet UIButton *button;
Once you have defined the UIButton in your header, switch to IB. Double check that file's owner is set to your viewcontroller class, then add a UIButton to the view. Then you should be able to either:
If you want the UIButton in IB to trigger one of your defined IBActions, you will make a connection to the action. I usually perform this by right-clicking on the File's Owner, which will show all available IBActions and IBOutlets.
Hope this helps!
Upvotes: 2
Reputation: 19469
Try and check if class of your File's Owner is set to your ViewController Class properly in your XIB.
For checking Go to your XIB
Click on File's Owner and Open the Inspector.
In inspector, go to the last(fourth) tab and check whether you have set your class as <yourViewControllerName>
Hope this helps you.
EDIT:
For better understanding I have added an image of where you need to look for the class.
Also please cross check that you have declared the variables with IBOutlet
Prefix in your ViewController's header file
Upvotes: 9
Reputation: 5117
Did you set the file owner(in interface builder) to the class CalculatorViewController.
Upvotes: 0