Create a view controller with options

I would like to create a new viewController and pass data when creating the instance.

I have a dictionary with some data and you want to access this data as soon as the viewController is created.

I 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 setRecipeItems is fired before the view is loaded.

Ideally, I would like to do something like:

myRecipe = [[RecipeCard alloc] initWithData: dictionary];

But it did not work for me

thank

+3
source share
1 answer

You can do what you ask by doing this: (put 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 as follows:

myRecipe = [[RecipeCard alloc] initWithRecipes:someDictionaryObject];
+4
source

Source: https://habr.com/ru/post/1785522/


All Articles