3

The select item is a slider. When I click the slider It will pass the data to Second VC (WebViewController). but how to pass data from first view controller to second view controller in objective-c? Sorry, This is my first time coding objective C.

First VC .m file

#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];
        }..

Second VC .m file

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

Second VC .h file

#import <UIKit/UIKit.h>

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

2 Answers 2

2

Two points to note:

1: This is not the way to initiate a view controller to show/present in your app.

2: You should declare your NSString *data in your SecondVC's .h file.

Now point 1 solutions is to change your code with below in your didSelectItemAtIndexPath: function

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];
    }... 
}
Sign up to request clarification or add additional context in comments.

7 Comments

I try to add NSString *data; inside or outside @interface on .h file. but still getting the error.
This error usually happens when you create an object where object creation is simply not allowed.
Ok, I updated .h file code to the question. not allowed?
now plz tell me what error you are getting? You have edited your first comment
Sorry, Its the same. Property 'data' not found on object of type 'WebViewController' and Unexpected interface name 'WebViewController': expected expression
|
0

try as

//Your class and obj name is same so You are getting this type of error

        WebViewController *VC = [[WebViewController alloc] init];
    //OR
        WebViewController *VC= [self.storyboard instantiateViewControllerWithIdentifier:@"WebViewController"];


        VC.data = arraySliderProducts[indexPath.row][@"title"]; 
        [self.navigationController pushViewController: VC animated:YES];

2 Comments

Property 'data' not found on object of type 'WebViewController'. What i need to add in second VC?
You dont need to add anything in SecondVC just change the VArible name of "WebViewController" as try above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.