gpt4 book ai didi

ios - 通过 iCarousel 将信息传递给多个 viewController

转载 作者:行者123 更新时间:2023-11-28 20:07:02 25 4
gpt4 key购买 nike

我正在尝试实现一个 iCarousel,它会在选择图像时将信息传递给另外两个 View Controller 。虽然 iCarousel 完美加载并过渡到下一个 VC,但信息不会显示在新 VC 上。

我选择的方法是创建一个 NSObject 文件。我不能简单地将信息从 VC 传递到 VC,因为我有几个 VC 需要这些信息,而且如果可能的话,我不希望创建单例或使用 AppDelegate。

仅供引用:我确实在 UIView 的顶部添加了一个点击手势识别器,如果有任何不同的话,它可以作为下一个 VC 的 segue。

我已经尝试了所有可能的教程,但似乎无法找出我的问题。我只需要显示一个文本标签和一张图片,这应该非常简单。有人可以快速浏览一下我的代码,看看我做错了什么吗?

我的 NSObject 文件:

#import <Foundation/Foundation.h>
@interface Stop : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *image;
@end

第一个 ViewController.h(上面有 iCarousel):

#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#import "iCarousel.h"
#import "DirectionsViewController.h"
#import "Stop.h"

@interface StopsMenuViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>

@property (strong, nonatomic) IBOutlet iCarousel *carousel;
@property (strong, nonatomic) IBOutlet UILabel *titleLabel;

//Title
@property (nonatomic, strong) NSArray *stopTitles;
@property (nonatomic, strong) NSString *stopChosen;

//Image
@property (nonatomic, strong) NSArray *stopImages;
@property (nonatomic, strong) NSString *imageChosen;
@end

第一个ViewController.m:

#import "StopsMenuViewController.h"

@interface StopsMenuViewController () {
NSMutableArray *allInfo; }
@end

@implementation StopsMenuViewController
@synthesize titleLabel, carousel, stopImages, stopTitles, stopChosen, imageChosen;


- (void)awakeFromNib {
NSString *myPlist = [[NSBundle mainBundle] pathForResource:@"Chinatown" ofType:@"plist"];
NSDictionary *rootDictionary = [[NSDictionary alloc] initWithContentsOfFile:myPlist];

self.stopImages = [rootDictionary objectForKey:@"StopImages"];
self.stopTitles = [rootDictionary objectForKey:@"StopTitles"];
}

- (void)carouselDidScroll:(iCarousel *)carousel {
[titleLabel setText:[NSString stringWithFormat:@"%@", [self.stopTitles
objectAtIndex:self.carousel.currentItemIndex]]];
}

- (void)dealloc {
self.carousel.delegate = nil;
self.carousel.dataSource = nil;
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"toDirections"])
{
DirectionsViewController *dvc = [segue destinationViewController];
int itemId = [self.carousel currentItemIndex];
NSIndexPath *path = [NSIndexPath indexPathForRow:itemId inSection:0];

Stop *current = [allInfo objectAtIndex:path.row];
[dvc setPassInfo:current];
}
}

- (void)viewDidLoad {
[super viewDidLoad];
self.carousel.type = iCarouselTypeCoverFlow2;

allInfo = [[NSMutableArray alloc] init];

Stop *info = [[Stop alloc] init];
stopChosen = [NSString stringWithFormat:@"%@", [self.stopTitles objectAtIndex:self.carousel.currentItemIndex]];
[info setTitle:stopChosen];
[allInfo addObject:info];

info = [[Stop alloc] init];
self.imageChosen = [NSString stringWithFormat:@"%@", [self.stopImages
objectAtIndex:self.carousel.currentItemIndex]];
[info setTitle:self.imageChosen];
[allInfo addObject:info];
}

- (void)viewDidUnload
{
[super viewDidUnload];
self.carousel = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel {
return [self.stopImages count];
}

- (NSUInteger)numberOfVisibleItemsInCarousel:(iCarousel *)carousel {
return 4;
}

- (UIView *)carousel:(iCarousel *)_carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
if (view == nil)
{
view = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[self.stopImages objectAtIndex:index]]];
}
return view;
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
DirectionsViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"dvc"];
[self.navigationController pushViewController:dvc animated:YES];
}
@end

第二个 ViewController.h:

#import <UIKit/UIKit.h>
#import "Stop.h"

@interface DirectionsViewController : UIViewController

@property (strong, nonatomic) IBOutlet UILabel *titleLabel;
@property (strong, nonatomic) IBOutlet UIImageView *imageBox;

@property (nonatomic, strong) Stop *PassInfo;
@property (nonatomic, strong) NSString *stopTitle;
@property (nonatomic, strong) NSString *myStopTitle;
@end

第二个 ViewController.m:

#import "DirectionsViewController.h"

@interface DirectionsViewController ()
@end

@implementation DirectionsViewController
@synthesize PassInfo;

- (void)viewDidLoad {
[super viewDidLoad];

[self.titleLabel setText:[PassInfo title]];

UIImage *image = [UIImage imageNamed:[PassInfo image]];
[self.imageBox setImage:image];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
@end

最佳答案

代替

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
DirectionsViewController *dvc = [self.storyboard instantiateViewControllerWithIdentifier:@"dvc"];
[self.navigationController pushViewController:dvc animated:YES];
}

使用

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index {
[self performSegueWithIdentifier:@"toDirections" sender:self];
}

在您的代码中,您正在实例化第二个 View Controller 并呈现它,这与执行 segue 不同。因此方法 - prepareForSegue:sender: 将不会被调用。

关于ios - 通过 iCarousel 将信息传递给多个 viewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21763020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com