2017-09-08 11 views
2

選択項目はスライダーです。スライダーをクリックすると、データがSecond VC(WebViewController)に渡されます。最初のビューコントローラからのデータを目的のcで2番目のビューコントローラに渡す方法はありますか?申し訳ありませんが、これは客観C.目的のビューコントローラ間でデータを渡す方法-c?

まずVCの.mファイル

#import "WebViewController.h" 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    arraySliderProducts = [[NSMutableArray alloc]init]; 
} 


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [collectionView deselectItemAtIndexPath:indexPath animated:YES]; 

    UIViewController *controller = nil; 


    switch (indexPath.row) 
    { 

     case 0: 
     { 
      WebViewController *WebViewController = [[WebViewController alloc] init]; 
      //error: No visible @interface for "WebViewController" declares the selector 'alloc' 
      WebViewController.data = arraySliderProducts[indexPath.row][@"title"]; //pass this link value 
      //error: Property 'data' not found on object of type 'WebViewController' 
      [self.navigationController pushViewController: WebViewController animated:YES]; 
     }.. 

セカンドVCの.mファイル

@interface WebViewController() 
@property (nonatomic, retain) NSString *data; 

セカンドVC .hファイル

#import <UIKit/UIKit.h> 

@interface WebViewController : UIViewController 
{ 
    AppDelegate *appDelegate; 
    NSString *data; 
} 
@end 

答えて

2

2点:

1:これはあなたのアプリケーションに存在/表示するビューコントローラを開始するための方法ではありません。

2:NSString *dataは、SecondVCの.hファイルに宣言する必要があります。

は今1つの解決策は、あなたのdidSelectItemAtIndexPathに以下を使用して、コードを変更することですポイント:機能

switch (indexPath.row) 
{ 
    case 0: 
    { 
     // By Default storyboard name is "Main". Change it if you you have different name in your project 
     UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil]; 

     // Now instantiate your view controller with its identifier on this storyboard object. 
     // Note: don't forget to set viewControllers' identifier 
     WebViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier: @"WebViewController"]; 

     // Now Pass your value 
     WebViewController.data = arraySliderProducts[indexPath.row][@"title"]; 

     //Finally push this object on your navigation stack 
     [self.navigationController pushViewController: WebViewController animated:YES]; 
    }... 
} 
+0

NSString *データを追加しようとしています。内部または外部の@interface .hファイル。それでもエラーが発生します。 –

+0

このエラーは通常、オブジェクトの作成が許可されていないオブジェクトを作成すると発生します。 –

+0

あなたの2番目のvc '.h'ファイルコードを表示します –

0
コーディング初めてです

try try

注意すべき10
+0

プロパティ「データ」タイプ「WebViewController」の対象には見られません。私は2番目のVCに追加する必要がありますか? –

+0

SecondVCに何も追加する必要はありません。上記のように "WebViewController"のVArible名を変更するだけです。 –

関連する問題