Domanda

I'm trying to implement an iCarousel that will pass information on to two other view controllers when an image is chosen. While the iCarousel loads perfectly and transitions to the next VC, the information is not displayed on the new VC.

The approach I chose was to create an NSObject file. I can't simply pass the info from VC to VC since I have several VC's that need the information and I'd prefer not to create a singleton or use AppDelegate if possible.

FYI: I do have a tap gesture recognizer added on top of the UIView that acts as the segue to the next VC if that makes any difference.

I've tried every possible tutorial out there and can't seem to figure out my problem. I just need to display a text label and a picture, which should really be pretty easy. Can someone take a quick glance at my code to see what I'm doing wrong?

My NSObject File:

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

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

First ViewController.h (with iCarousel on it):

#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

Second 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

Second 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
È stato utile?

Soluzione

Instead of

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

Use

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

In your code, you're instantiating the second view controller and presenting it, which is not the same as performing a segue. Therefore the method - prepareForSegue:sender: will not be invoked.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top