选择项目是一个滑块。当我单击滑块时它会将数据传递给第二个VC(WebViewController)。但如何将数据从第一个视图控制器传递到objective-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
答案 0 :(得分:2)
需要注意的两点:
1:这不是启动视图控制器在您的应用中显示/显示的方式。
2:您应该在SecondVC的NSString *data
文件中声明.h
。
现在,第1点解决方案是在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];
}...
}
答案 1 :(得分: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];