How to use an IBOutlet from parent class?
I'm working on an iOS and having some trouble accessing to an IBOutlet from a parent class, in this case a label with the life of the character.
My ViewController (parent) are like this:
@interface OpponentViewController : UIViewController {
IBOutlet UILabel *opponent_life;
IBOutlet OpponentView *opponentView;
}
@end
The OpponentView (child) class is where the character (image) reside and where player do all touch inte开发者_开发问答raction. When the player touch OpponentView the label should be refreshed.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
}
Thanks.
If I'm understanding your question correctly you would like to update opponent_life
when ever you detect a touch in opponentView
. Here are two ways of assigning the label to the view so it can be used from tocuhesBegan:withEvent:
1. Assigning the label to the view in IB
@interface OpponentView : UIView {
IBOutlet UILabel *opponent_life;
}
In the nib file for OpponentViewController
you change the type of the view from UIView
to OpponentView
and then connect the label to the outlet of the view.
2. Assigning the label in viewDidLoad
@interface OpponentView : UIView {
UILabel *opponent_life;
}
@property (nonatomic, retain) UILabel *opponent_life;
In `OpponentViewController'
- (void)viewDidLoad {
[super viewDidLoad];
opponentView.opponent_life = opponent_life;
}
OpponentView should be a subclass of something like UIImageView, for example.
I may be misinterpreting the problem you're having, but if you can't see the instance variables as outlets at all, you can try including IBOutlet
in your property declarations. For example:
@property (nonatomic, retain) IBOutlet UILabel *opponent_life;
@property (nonatomic, retain) IBOutlet OpponentView *opponentView;
In the code the OpponentView
class is one which extends any class like UIView
or UIImageView
etc.
So you have to drag a UIView
or UIImageView
(based on the requirement) to the OpponentViewController's
view in the IBOutlet
from Library.
then change the class in the "viewIdentity" in the inspector to OpponentView
. and then you can link to the file owner.
So that you can solve your problem.
Regards,
Satya
精彩评论