1

我是一个新手,正在开发一个应用程序,该应用程序既显示用户从应用程序附带的集合中随机选择的俳句,又允许他/她编写俳句来添加该集合。它还将用户带到一个网页的网页视图,她/他可以从中购买俳句书籍。问题是,我已经将几乎所有的方法都放在了视图控制器中,而且我觉得好的 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 中放置什么以及保留什么。(我认为我的部分困惑是因为“视图控制器”同时包含“视图”和“控制器”,我不明白它是视图、控制器还是两者都不是......)

无论如何,我很喜欢关于我应该创建的其他类以及我应该在其中放入哪些方法的建议。

4

1 回答 1

1

这些方法中的大多数都适用于视图控制器(任何“显示”某些东西或用视图做某事的东西)。您可能想要提取管理俳句的逻辑......就像 saveUserHaiku: 和 deleteHaiku: 方法至少应该调用一个模型。然后模型可能会发布通知或回调委托以通知更改的数据......您的视图控制器是该委托或观察者,并且它将适当地响应现在删除的俳句,或者如果有可见的更改则保存的俳句。

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

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

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

于 2012-10-05T19:12:47.417 回答