【发布时间】:2014-02-13 18:51:39
【问题描述】:
我正在尝试实现一个 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
第一个 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
【问题讨论】:
标签: ios objective-c uiviewcontroller nsobject icarousel