视图控制器中有太多方法,但不知道要创建哪些其他类或将哪些方法移动到那里



我是一个开发应用程序的新手,该应用程序既显示用户从应用程序附带的集合中随机选择的俳句,又允许他/她编写俳句来添加该集合。它还将用户带到一个页面的网络视图,用户可以从中购买俳句书籍。问题是,我几乎把所有的方法都放在了视图控制器中,我有一种感觉,好的MVC工作会以不同的方式来划分它。以下是我的基本方法(为了简洁起见,我合并了一些,省略了一些其他方法的子方法,还有Foundation和UIKit方法),并进行了简要描述:

GHHaiku.m //This is a model class, or at least I think it is.
-(int)chooseNumber {
returns a random number 
}
-(NSString *)haikuToShow {
uses that number to return haiku from collection
}
GHViewController.m
-(void) viewDidLoad
-(void) viewDidUnload
-(void) clearScreen {
gets rid of all UITextFields and images so that a new set of them can be created, perhaps with different parameters.
}
-(void) saveData {
saves persistent information like whether user has read the instructions
}
-(void)home {
takes user “home” from whatever screen we're in and shows next haiku
}
//--code to set up navBar and Toolbar
-(void)createNavBar:title {
creates and adds buttons to a UINavigationItem with “title” as title and adds UINavigationBar to view
}
-(void) loadToolbar {
loads toolbar at bottom of screen and adds buttons
}
//--code for instructions page
-(void) haikuInstructions {
shows instructions on screen
}
//--code for Amazon page
-(void) loadAmazon {
loads webview of haiku page at amazon.com
}
-(void) webViewDidFinishLoad:(UIWebView)
-(BOOL) webview:(UIWebView) shouldStartLoadWithRequest:(NSURLRequest) navigationType:(UIWebViewNavigationType)
-(void) connectWithURL☹NSString) andBaseURLString: (NSString)
-(BOOL) connection: (NSURLConnection) didFailWithError: (NSError)
-(void) doneWithAmazon {
takes user out of webview Amazon and shows next haiku
}
//--code for compose page
-(void) createSpaceToWrite {
sets up and shows an editable UITextView
}
-(void) userWritesHaiku {
allows user to write haiku
}
-(void) userEditsHaiku {
takes user to edit screen for haiku s/he’s already written.
-(void) userFinishedWritingHaiku {
shows action sheet once user’s done
}
-(void) deleteHaiku {
allows user to delete haiku s/he’s written
}
-(void) saveUserHaiku {
saves user haiku to documents folder
}
-(void) takeToOptOut {
allows user to opt out of sending haiku to central database
}
//--code for sharing
-(void) createImage {
creates image of haiku
}
-(void) showActionSheet {
gives user options to tweet, faceBook, or email
}
-(void) share {
allows user to tweet, email, or facebook that image
}
//--code for display page
-(IBAction)chooseDatabase: (UISegmentedControl) {
allows user to choose whether to see his/her own haiku and/or haiku that come with the application
}
-(void) fadeView {
fades the UISegmentedControl for chooseDatabase
}
-(void) nextHaiku {
shows next haiku
}
-(void) previousHaiku {
shows previous haiku
}

我不得不想象其中一些属于不同的类,但我不知道该把什么放在哪里,在GHViewController中保存什么。(我想我的困惑部分是因为"视图控制器"中既有"视图"又有"控制器",我不明白它是视图、控制器还是两者都不是……)

不管怎样,我很喜欢关于我应该创建的其他类以及我应该在其中放入哪些方法的建议。

这些方法中的大多数都适用于视图控制器(任何"显示"或使用视图执行某些操作的地方)。不过,你可能想提取管理俳句的逻辑。。。像saveUserHaiku:和deleteHaiku:方法至少应该调用一个模型。然后,模型可能会发布通知或回调委托以通知更改的数据。。。你的视图控制器是代表或观察者,它会对现在删除的俳句做出适当的响应,如果有明显的变化,则会对保存的俳句作出适当的响应。

nextHaiku和previousHaiku方法在视图控制器中是有意义的,但同样应该调用模型对象来实际检索数据。

模型应该跟踪它正在使用的数据库,因此,虽然允许用户选择数据库的代码位于视图控制器中,但可用的数据库和选定的数据库来自模型。

此外,共享内容的代码也可以进行划分。显示可视化元素和控件的代码应该在视图控制器中,但您可能有一个共享实用程序对象,该对象知道如何打包数据并将其发送到适当的位置。

相关内容

最新更新