2011-01-14 37 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

相關問題