2011-01-14 33 views
3

我想创建一个新的viewController并在实例化时传递数据。使用参数创建视图控制器

我有一个包含一些数据的字典,想在创建viewController后立即访问这些数据。

我已经试过这样:

//create the recipe 
    myRecipe = [[RecipeCard alloc] init]; 
     //create a dictionary here... 

//call the setRecipeItems method of the recipe I have created 
     [myRecipe setRecipeItems: dictionary] 

; 

的问题是,认为前setRecipeItems火灾没有负载。

理想情况下,我想这样做:

myRecipe = [[RecipeCard页头] initWithData:字典]。

但是,这并没有为我工作

感谢

+0

写这之后,我想我可以只设置与第一种方法的数据,然后将这些数据在我viewDidLoad中后调用一个方法,但我很好奇,如果这是可能的,所以我并没有删除我正在尝试做的 – 2011-01-14 20:10:40

回答

4

你可以做的正是你被这样问的: (这发生在你的.h文件中)

@interface RecipeCard : UIViewController { 
    NSDictionary *recipes; 
} 

- (id)initWithRecipes:(NSDictionary *)Recipes; 

@end 

(然后在你的.m文件中)

@implementation RecipeCard 

- (id)initWithRecipes:(NSDictionary *)Recipes 
{ 
    if(self = [super init]) { 
    recipes = [NSDictionary dictionaryWithDictionary:Recipes]; 
    [recipes retain]; 
    } 
    return self; 
} 

@end 

现在你可以创建你的配方这样的卡:

myRecipe = [[RecipeCard alloc] initWithRecipes:someDictionaryObject]; 
+0

。谢谢 – 2011-01-14 21:58:33

相关问题