我正在尝试实现一个iCarousel,它会在选择图像时将信息传递给另外两个视图控制器。当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
First 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
答案 0 :(得分:3)
而不是
- (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:
。