4 回答
TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊
MVC中的M用于“模型”,而在MVC范例中,模型類的作用是管理程序的數(shù)據(jù)。模型與視圖相反 - 視圖知道如何顯示數(shù)據(jù),但它不知道如何處理數(shù)據(jù),而模型知道如何處理數(shù)據(jù)的所有內(nèi)容,但不了解如何顯示數(shù)據(jù)。模型可能很復(fù)雜,但它們不一定是 - 您的應(yīng)用程序的模型可能像字符串或字典數(shù)組一樣簡單。
控制器的作用是在視圖和模型之間進(jìn)行調(diào)解。因此,它們需要引用一個(gè)或多個(gè)視圖對(duì)象和一個(gè)或多個(gè)模型對(duì)象。假設(shè)您的模型是一個(gè)字典數(shù)組,每個(gè)字典代表表中的一行。應(yīng)用程序的根視圖顯示該表,它可能負(fù)責(zé)從文件加載數(shù)組。當(dāng)用戶決定向表中添加新行時(shí),他們會(huì)點(diǎn)擊一些按鈕,您的控制器會(huì)創(chuàng)建一個(gè)新的(可變的)字典并將其添加到數(shù)組中。為了填充行,控制器創(chuàng)建一個(gè)詳細(xì)視圖控制器并為其提供新的字典。詳細(xì)視圖控制器填寫字典并返回。字典已經(jīng)是模型的一部分,因此沒有其他任何事情需要發(fā)生。
TA貢獻(xiàn)1946條經(jīng)驗(yàn) 獲得超3個(gè)贊
有多種方法可以將數(shù)據(jù)接收到iOS中的不同類。例如 -
分配另一個(gè)類后直接初始化。
委派 - 用于傳回?cái)?shù)據(jù)
通知 - 用于一次向多個(gè)類廣播數(shù)據(jù)
保存
NSUserDefaults- 以便稍后訪問單身人士課程
數(shù)據(jù)庫和其他存儲(chǔ)機(jī)制,如plist等。
但是對(duì)于將值傳遞給在當(dāng)前類中完成分配的其他類的簡單方案,最常見和首選的方法是在分配后直接設(shè)置值。這樣做如下: -
我們可以使用兩個(gè)控制器來理解它 - Controller1和Controller2
假設(shè)在Controller1類中,您要?jiǎng)?chuàng)建Controller2對(duì)象并使用傳遞的String值推送它。這可以這樣做: -
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj passValue:@"String"];
[self pushViewController:obj animated:YES];}在Controller2類的實(shí)現(xiàn)中,將有以下功能 -
@interface Controller2 : NSObject@property (nonatomic , strong) NSString* stringPassed;@end@implementation Controller2
@synthesize stringPassed = _stringPassed;- (void) passValue:(NSString *)value {
_stringPassed = value; //or self.stringPassed = value}@end您也可以使用與此類似的方式直接設(shè)置Controller2類的屬性:
- (void)pushToController2 {
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];
[obj setStringPassed:@"String"];
[self pushViewController:obj animated:YES];}要傳遞多個(gè)值,您可以使用多個(gè)參數(shù),例如: -
Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];[obj passValue:@“String1” andValues:objArray withDate:date];
或者,如果需要傳遞超過3個(gè)與常用功能相關(guān)的參數(shù),則可以將值存儲(chǔ)到Model類中,并將該modelObject傳遞給下一個(gè)類
ModelClass *modelObject = [[ModelClass alloc] init]; modelObject.property1 = _property1;modelObject.property2 = _property2; modelObject.property3 = _property3;Controller2 *obj = [[Controller2 alloc] initWithNib:@"Controller2" bundle:nil];[obj passmodel: modelObject];
所以,如果你想 -
1) set the private variables of the second class initialise the values by calling a custom function and passing the values.2) setProperties do it by directlyInitialising it using the setter method.3) pass more that 3-4 values related to each other in some manner , then create a model class and set values to its object and pass the object using any of the above process.
希望這可以幫助
- 4 回答
- 0 關(guān)注
- 930 瀏覽
添加回答
舉報(bào)
