我最近一直在做很多Objective-C编程,刚刚回到做更多的iPhone开发。我在其他语言/框架中使用MVC进行了大量编程,但我只想确保在iPhone开发中正确使用MVC。iPhone:正确使用视图和视图控制器
我创建了一个新的iPhone Utility应用程序,它创建两个视图:MainView和FlipsideView。两者都有自己的控制器(FlipsideViewController和MainViewController)和XIB文件。
我一直在做的是将IBOutlet UIControl myControl
变量放在我的MainView.h或FlipsideView.h文件中,然后将Interface Builder中的控件绑定到这些变量。然后,我将任何IBAction SomeAction myAction
方法放在MainViewController.h和FlipsideViewController.h文件中,并将事件绑定到Interface Builder中的这些方法。
这似乎在概念上是正确的,但似乎会导致问题。假设我有一个按钮,单击它时会更改标签的文本。因此,Controller不知道我的按钮的OnTouchUp事件处理程序中标签的变量名称是什么。所以我为它做了一个@property
。但由于MainViewController.view
属性的类型不是MyView
,所以无论何时尝试从视图控制器访问这些属性,都会收到警告或错误。
我这样做是否正确?有一个更好的方法吗?如果这是正确的,我如何正确使用这些变量而不会收到警告或错误?
感谢
这里的一些代码,显示我在做什么:
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
}
我知道我能投的观点是正确的类型,但这似乎是一个黑客工作。 我知道我可以忍受这个警告,但一个警告告诉我,我正在做一些我可能不应该做的事情。我不相信SDK会让我不得不一直做这样的事情。
我必须误解View
应该是什么,我错误地使用它。
“因此,控制器不知道标签的变量名称在我的按钮的OnTouchUp事件处理程序中,因此我为它创建了一个@property,但由于MainViewController.view属性不是的类型MyView,每当我尝试从视图控制器访问这些属性时,我都会收到警告或错误。“ - 请你能发布相关的代码吗? – 2010-03-27 19:02:37