I would like to create a new viewController and pass data along when I instantiate.
I have a dictionary with some data and would like to access that data as soon as the viewController is created.
I've tried this:
//create the recipe
myRecipe = [[RecipeCard alloc] init];
//create a dictionary here...
//call the setRecipeItems method of the recipe I have created
[myRecipe setRecipeItems: dictionary]
;
The problem is that the setRecipeItems fires before the view did load.
Ideally, I would like to do something like:
myRecipe = [[RecipeCard alloc] initWithData:dictionary];
But that hasn't worke开发者_StackOverflow中文版d for me
Thanks
You can do exactly what you are asking by doing this: (place this in your .h file)
@interface RecipeCard : UIViewController {
NSDictionary *recipes;
}
- (id)initWithRecipes:(NSDictionary *)Recipes;
@end
(Then in your .m file)
@implementation RecipeCard
- (id)initWithRecipes:(NSDictionary *)Recipes
{
if(self = [super init]) {
recipes = [NSDictionary dictionaryWithDictionary:Recipes];
[recipes retain];
}
return self;
}
@end
Now you can create your RecipeCard like this:
myRecipe = [[RecipeCard alloc] initWithRecipes:someDictionaryObject];
精彩评论