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 interaction. When the player touch OpponentView the label should be refreshed.

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

} 

Thanks.

Answers


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


Need Your Help

How do I substitute a row in a two dimensional numpy array with a list of substitutions?

python numpy

This is a VERY simplified example of a larger problem I'm working with. How do I replace multiple rows in a two dimensional numpy array? For example, I have an array...

What is the best way the _DoPostBack javascript method in Asp.net

asp.net asp.net-ajax

I want to set a breakpoint on the __DoPostBack method, but it's a pain to find the correct file to set the breakpoint in.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.