我想创建一个新的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]
;
Run Code Online (Sandbox Code Playgroud)
问题在于setRecipeItems在加载视图之前触发。
理想情况下,我想执行以下操作:
myRecipe = [[[RecipeCard alloc] initWithData:dictionary];
但这对我没有用
谢谢
您可以通过执行以下操作来完全按照您的要求进行操作:(将其放置在.h文件中)
@interface RecipeCard : UIViewController {
NSDictionary *recipes;
}
- (id)initWithRecipes:(NSDictionary *)Recipes;
@end
Run Code Online (Sandbox Code Playgroud)
(然后在您的.m文件中)
@implementation RecipeCard
- (id)initWithRecipes:(NSDictionary *)Recipes
{
if(self = [super init]) {
recipes = [NSDictionary dictionaryWithDictionary:Recipes];
[recipes retain];
}
return self;
}
@end
Run Code Online (Sandbox Code Playgroud)
现在,您可以像下面这样创建RecipeCard:
myRecipe = [[RecipeCard alloc] initWithRecipes:someDictionaryObject];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1523 次 |
| 最近记录: |