如何在Objective-C中在视图控制器之间传递数据?

3
选择的项目是滑块。当我点击滑块时,它将向第二个VC(WebViewController)传递数据。但是如何从第一个视图控制器传递数据到第二个视图控制器在objective-c中?抱歉,这是我第一次编写objective 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:您应该在SecondVC的.h文件中声明您的NSString *data

现在,解决方案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];
    }... 
}

我尝试在.h文件的@interface内部或外部添加NSString *data;,但仍然出现错误。 - undefined
通常情况下,当您创建一个不允许创建对象的对象时,就会出现此错误。 - undefined
好的,我已经更新了问题中的.h文件代码。不允许吗? - undefined
请告诉我您遇到了什么错误?您已经编辑了您的第一条评论。 - undefined
你在问题中发布了完整的.h文件代码吗? - undefined
显示剩余2条评论

0
尝试一下
//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];

在第二个视图控制器中,您需要添加什么内容? - undefined
你不需要在SecondVC中添加任何内容,只需将"WebViewController"的变量名更改为上面的尝试即可。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接