2012-11-11 24 views
0

可能是一個noob問題,但它在這裏。我有我的AppDelegate從「application didFinishLaunchingWithOptions」中的文件實例化一個新字典。我有一個addViewController,我想傳遞一個新的對象添加到AppDelegate的字典中,以便以後保存到磁盤。以下是我所得到的一些片段。「沒有已知類選擇器的方法」混淆

// 
// AppDelegate.m 
// PersonLibraryiOS 
// 
// Created by Joey on 11/7/12. 
// Copyright (c) 2012 Joey. All rights reserved. 
// 

#import "AppDelegate.h" 
#import "AddViewController.h" 
#import "Person.h" 
@implementation AppDelegate 
@synthesize PersonDict; 

-(void)addtoDict:(Person *)newPerson 
{ 
    [PersonDict setObject:@"newPerson" forKey:[newPerson name]]; 
} 



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    // Override point for customization after application launch. 
    return YES; 
    PersonDict = [NSMutableDictionary dictionaryWithContentsOfFile:@"diskDict"]; 

和AddViewController:

// 
// AddViewController.m 
// personLibraryiOS 
// 
// Created by Joey on 11/8/12. 
// Copyright (c) 2012 Joey. All rights reserved. 
// 

#import "AddViewController.h" 
#import "TableViewController.h" 
#import "person.h" 
#import "AppDelegate.h" 

@implementation AddViewController 
@synthesize nameLabel; 
@synthesize ageLabel; 
@synthesize heightLabel; 
@synthesize weightLabel; 
@synthesize hairColorLabel; 
@synthesize eyeColorLabel; 


- (IBAction)saveButton:(id)sender 
{ 
    person *newperson = [[person alloc]init]; 

    newperson.name = [nameLabel text]; 
    newperson.age = [ageLabel text]; 
    newperson.height = [heightLabel text]; 
    newperson.weight = [weightLabel text]; 
    newperson.hairColor = [hairColorLabel text]; 
    newperson.eyeColor = [eyeColorLabel text]; 



    [AppDelegate addtoDict:newperson]; <---- the error is here 

    } 

我知道這可能是基本的,但我真的很困惑。我在addViewController中導入AppDelegate.h文件,所以它應該知道所有關於AppDelegate的方法。

謝謝大家。

回答

3

行:

[AppDelegate addtoDict:newperson]; 

試圖呼籲命名AppDelegate類名爲addtoDict:類方法。很可能你想調用你的應用程序委託實例上的addtoDict:實例方法(不是類方法)。

你可能想:

[(AppDelegate *)[NSApplication sharedApplication].delegate addtoDict:newperson]; 
+0

謝謝!我忘記了類方法和實例方法。感謝您寫出我需要的方法。 –

4

AppDelegate是一個類。您大概想要與作爲應用程序代理的類的具體實例進行交談,該實例將是[[UIApplication sharedApplication] delegate]。一個類不能與其實例互換。

+0

非常感謝您,先生。 〜弓〜 –

相關問題