我正在尝试实现一个iCarousel,它将在选择图像时将信息传递给其他两个视图控制器。当iCarousel完美加载并过渡到下一个VC时,该信息不会显示在新VC上。

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

仅供参考:我确实在UIView的顶部添加了一个轻击手势识别器,如果有区别的话,它可以充当下一个VC的按钮。

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

我的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];
}


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

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

10-10 22:24