Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 多个分段之间不传递对象数据_Iphone_Ios_Storyboard_Segue_Recipe - Fatal编程技术网

Iphone 多个分段之间不传递对象数据

Iphone 多个分段之间不传递对象数据,iphone,ios,storyboard,segue,recipe,Iphone,Ios,Storyboard,Segue,Recipe,我启动了一个项目,该项目最初是在Appcoda关于使用视图控制器在场景之间传递数据的教程之后完成的(请在此处找到:) 基本上,它从第一个场景获取表格数据,segue连接到另一个显示配方图像、准备时间和配料的控制器。然而,我想更进一步,添加另一个视图控制器,显示如何准备配方的说明。到目前为止的情况是: 初始视图控制器(表格数据)->RecipedDetailViewController->说明ViewConcoller 所以实际上只有三个视图控制器,但都共享相同的数据。我想在视图控制器之间共享的数

我启动了一个项目,该项目最初是在Appcoda关于使用视图控制器在场景之间传递数据的教程之后完成的(请在此处找到:)

基本上,它从第一个场景获取表格数据,segue连接到另一个显示配方图像、准备时间和配料的控制器。然而,我想更进一步,添加另一个视图控制器,显示如何准备配方的说明。到目前为止的情况是:

初始视图控制器(表格数据)->RecipedDetailViewController->说明ViewConcoller

所以实际上只有三个视图控制器,但都共享相同的数据。我想在视图控制器之间共享的数据是配方数据,格式如下:

Recipe *recipe1 = [Recipe new];
recipe1.name = @"Egg Benedict";
recipe1.prepTime = @"30 min";
recipe1.imageFile = @"egg_benedictl.jpg";
recipe1.ingredients = [NSArray arrayWithObjects:@"2 fresh English muffins", @"4 eggs",    @"4 rashers of back bacon", @"2 egg yolks", @"1 tbsp of lemon juice", @"125 g of butter", @"salt and pepper", nil];
recipe1.instructions = @"This is recipe one's instruction set";
当然,这个关联代码:

self.title = recipe.name;
self.prepTimeLabel.text = recipe.prepTime;
self.recipePhoto.image = [UIImage imageNamed:recipe.imageFile];

NSMutableString *ingredientText = [NSMutableString string];
for (NSString* ingredient in recipe.ingredients) {
    [ingredientText appendFormat:@"%@\n", ingredient];
}
self.ingredientTextView.text = ingredientText;

self.instructionTextView.text = recipe.instructions;

recipeLabel.text = recipeName;
但是,当我尝试将配方的说明传递给我的第三个视图控制器时,会得到混合的结果。要么不传递任何内容,要么传递拉丁文本字段占位符,要么只传递所有配方的第一个配方的recipe.instruction,无论选择了什么。我已经做了一个星期了,我想是时候寻求希望了

如果有人对如何在多个阶段之间传递我的配方数据有任何建议,我将不胜感激。我已将所有代码粘贴到下面,从顶部文件开始:

//
//  minuteMealsViewController.h
//  minuteMeals
//
//  Created by KMO on 8/18/12.
//  Copyright (c) 2012 KAM Digital. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "Recipe.h"

@interface minuteMealsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) IBOutlet UITableView *tableView;
//@property (nonatomic, weak) IBOutlet UIImageView *thumbnailImageView;



@end



首先尝试将指令设置为它自己的属性,并在viewDidLoad中设置标签

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showInstructions"]) {

        minuteMealsDetailViewController *destViewController = segue.destinationViewController;

        destViewController.instructionText = [recipe instructions];

    }
    NSLog(@"showInstructions Seque is being passed");
}
其中
instructionText
是一个新属性

并在viewDidLoad中加载说明VIEWCONTROLLER do

instructionTextView.text = self.instructionText;
编辑:

您没有看到任何内容的原因是视图没有完全加载到
prepareforsgue
中,所有视图的初始化都应该在
viewDidLoad
中。当可以安全地更改视图时,将调用该方法

您现在对“RecipedDetail”和“instructions”使用相同的viewcontroller 我建议为您提供一个单独的viewController类

只需将指令作为属性传递到该viewController中

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showInstructions"]) {

        MinuteMealsInstructionsViewController *destViewController = segue.destinationViewController;

        destViewController.instructionText = [recipe instructions];

    }
    NSLog(@"showInstructions Seque is being passed");
}
其中
MinuteMealsInstructionsViewController
是具有instructions属性的新类

您说希望将配方包含在recipe对象中。这仍然是正确的,因为您只传递对对象成员的引用。假设您更改了指令字符串,那么它也会在您的配方对象中更改,因为该字符串只是一个引用

另请注意:根据Objective-c样式指南,您应该使用大写字母开头的类名

如果您确实想在同一个viewController中执行此操作,只需传递整个配方对象
destViewController.recipe=recipe和在viewDidLoad中:

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.instructionTextLabel = recipe.instructions;
}

哎呀,忘了提到我正在使用按钮将recipeDetailViewController连接到RecipeInstructionViewController。此代码将RecipeInstructionViewController显示为recipeDetailViewController的一个类。我之前把它作为自己的类,但是没有解决它为什么应该这样做。我可以这样做,但是我的属性已经在recipe对象中列出了。除了名称、准备时间和配料,我还有我的食谱类型的属性说明。所有其他数据都会沿着segue传递到recipeDetailViewController,但当我想将指令传递到RecipeInstructionViewController时,不会传递任何数据。我希望尽可能多地使用对象,而不必为每个序列定义一组字符串数组。我希望这是可能的,但也许Xcode不能做到这一点?好的,现在我知道你做了什么。minuteMealsDetailViewController实例中的destinationViewController(与实例化它的viewcontroller类相同)。这真的是你想要的吗?我用另一种解决方案编辑了我的答案。是的,在这个项目的格式副本中,我对两个视图控制器使用了相同的类。我有一个单独的类,但在所有这些不同的类之间传递数据被证明是很麻烦的。我想如果我将视图控制器保持在同一类,那么指令ViewController上的指令ContextView(UITextView)将只是RecipedDetailViewController的扩展。最初的教程不是为这个设计的,也许我应该从头开始。我在别处读到,我应该将数据加载到一个.plist文件中,但我在这里没有这样做。我不确定你在追求什么,但这听起来像是一个过于复杂的设计。为您的说明创建一个单独的类,并将其连接到interface builder中的视图,然后按照我上面描述的操作,您应该是安全的。为此,不需要将数据存储在.plist中。只有当您希望它持续存在,但在这种情况下(存储配方),您应该正确使用核心数据或其他一些数据库解决方案。如果您对我的答案感到满意,请选中复选框接受我的答案:)
//
//  Recipe.m
//  minuteMeals
//
//  Created by KMO on 8/19/12.
//  Copyright (c) 2012 KAM Digital. All rights reserved.
//

#import "Recipe.h"

@implementation Recipe

@synthesize name;
@synthesize prepTime;
@synthesize imageFile;
@synthesize ingredients;
@synthesize instructions;

@end
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showInstructions"]) {

        minuteMealsDetailViewController *destViewController = segue.destinationViewController;

        destViewController.instructionText = [recipe instructions];

    }
    NSLog(@"showInstructions Seque is being passed");
}
instructionTextView.text = self.instructionText;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showInstructions"]) {

        MinuteMealsInstructionsViewController *destViewController = segue.destinationViewController;

        destViewController.instructionText = [recipe instructions];

    }
    NSLog(@"showInstructions Seque is being passed");
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.instructionTextLabel = recipe.instructions;
}