I've recently been doing a lot of Objective-C programming, and just got back into doing more iPhone development. I've done a lot of programming using MVC in other languages/frameworks, but I just want to make sure I'm using MVC properly in my iPhone Development.
I created a new iPhone Utility Application, which creates two views: MainView and FlipsideView. Both have a controller (FlipsideViewController and MainViewController) and XIB file of their own.
What I've been doing is putting the IBOutlet UIControl myControl
variables in my MainView.h or FlipsideView.h files and then tying the controls in Interface Builder to those variables. Then I put any IBAction SomeAction myAction
methods in the MainViewController.h and FlipsideViewController.h files and tying the events to those methods in Interface Builder.
This seems to be conceptually correct, but seems to cause problems. Say I have a button that when clicked it changes a label's text. So the Controller doesn't have a clue of what the variable name of the label is in the OnTouchUp event handler for my button. So I make a @property
for it. But since the MainViewController.view
property isn't of type MyView
, I get warnings or errors whenever I try to access those properties from the view controller.
I am doing this correctly? Is there a better way to do this? If this is correct, how do I properly work with those variables without getting warnings or errors?
Thanks
Here's some code showing what I'm doing:
MainView.h
#import ...
@interface MainView : UIView
{
IBOutlet UILabel* label;
IBOutlet UIButton* button;
}
@property UILabel* label;
@property UIButton* button;
@end
MainViewController.m
-(void) buttonTouchUp:(id) sender
{
self.view.label.text = @"The button was pressed!"; //This gives error because label is not in the view structure or union
[self.view label].text = @"The button was pressed!"; //This gives a warning
}
I know I can cast the view to be of the right type, but that seems like a hack job. I know I can live with the warning, but a warning says to me that I'm doing something that I probably shouldn't be doing. I don't believe the SDK would set me up to do have to do something like that all the time.
I must just be misunderstanding what the View
is supposed to be and I'm using it incorrectly.