1

アプリに写真を撮らせてから、画像を別のビューに渡して編集しようとしていますが、ビューを変更する方法、ストーリーボードのビューに「ID」を追加する方法、またはビュー間でデータを渡します。

4

1 に答える 1

1

2つのUIViewController間の通信は手動で管理する必要がありますが、ストーリーボードを使用してアプリを作成する場合は、考慮しなければならないことがいくつかあります。

FirstViewControllerとSecondViewControllerがあるとしましょう(ストーリーボードにすべてが設定されていると仮定しましょう)。FirstViewControllerはUIImageをSecondViewControllerに渡し、次のようになります。

@interface FirstViewController : UIViewController

- (IBAction)transitionToNextViewController;

@property (retain, nonatomic) UIImage *image;

@end

@implementation FirstViewContoller

- (IBAction)transitionToNextViewController;
{
    [self performSegueWithIdentifier:@"SegueIdentifier"];
}

@end

と:

@interface SecondViewController : UIViewController

@property (retain, nonatomic) UIImage *image;

@end

おそらく、画像をSecondViewControllerにどのように渡すのか疑問に思っているでしょう。ストーリーボードを使用する場合、UIViewControllersはメソッドprepareForSegue:sender:への呼び出しを受け取ります。あなたがしなければならないのは、そこにある2番目のUIViewControllerのimageプロパティを設定することだけです。

@implementation FirstViewController

- (IBAction)transitionToNextViewController;
{
    [self performSegueWithIdentifier:@"SegueIdentifier"];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    SecondViewController *secondViewController = (SecondViewController *)segue.destinationViewController; // You have to cast it

    secondViewController.image = self.image;
}

@end

以上です。ストーリーボードをよりよく理解するには、こちらのアップルのドキュメントをお読みください。

于 2012-09-14T00:12:57.013 に答える