(In XCode 4 with Storyboards, app for iPhone)
My DetailView: a UIView filled with Labels and ImageViews to display details recieved through a segue when a cell was selected in the MainTableViewController. Now I’ve added a UIScrollView on this UIView because I want the Labels and ImageViews to be diplayed in a scroll view instead.
The only thing visible in DetailViewController now when I run the app, is a blank ScrollView. None of the content I’ve passed from the TableView is displayed, it’s not getting the selectedObject. How can I display my content and be able to swipe through the cells with swipeDetectedLeft now after the UIScrollView was added? I would be happy if you could make example with my code.
DetailViewController.h:
@interface DetailViewController : UIViewController {
IBOutlet UIScrollView *viewScroller;
//Added a ScrollView..
}
@property (strong, nonatomic) NSArray *detailsDataSource;
//Recieved from the TableView to know the order of the sorted TableView cells, cells populated with a mutable array from a plist of dictionaries)
@property int detailIndex;
//To know which cell selected (for swiping throug cells)
@property (nonatomic, strong) NSString *selectedObject;
//Content of selected cell from tableview (for swiping through cells)
@property (nonatomic, strong) IBOutlet UILabel *aLabel;
@property (nonatomic, strong) IBOutlet UIImageView *anImageView;
//to display content
DetailViewController.m:
#import "DetailViewController.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
@synthesize aLabel,anImageView;
@synthesize selectedObject;
@synthesize detailsDataSource, detailIndex;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[viewScroller setScrollEnabled:YES];
[viewScroller setContentSize:CGSizeMake(320, 700)];
//A customized Label for NavigationBar
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 480, 44)];
label.backgroundColor = [UIColor clearColor];
label.numberOfLines = 2;
label.font = [UIFont boldSystemFontOfSize: 14.0f];
label.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
label.textAlignment = UITextAlignmentCenter;
label.textColor = [UIColor whiteColor];
label.text = [selectedObject valueForKey:@"Name"];
self.navigationItem.titleView = label;
[label release];
aLabel.text = [selectedObject valueForKey:@"A Label"];
anImageView.image = [UIImage imageNamed:[selectedWine valueForKey:@"An Image"]];
self.navigationController.navigationBar.translucent = YES;
self.wantsFullScreenLayout = YES;
UISwipeGestureRecognizer *leftGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeDetectedLeft:)];
leftGesture.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:leftGesture];
}
- (void)swipeDetectedLeft:(UISwipeGestureRecognizer *)sender
{
if (detailIndex != [detailsDataSource count])
detailIndex++;
aLabel.text = [[detailsDataSource objectAtIndex: detailIndex] valueForKey:@"A Label"];
//Here follows ImageView and NavigationbBar Label for the swipe..
}
EDIT:
If I set aLabel.text = @”Test” then it works. So the UIScrollView isn’t getting the selectedObject from:
MainTableViewController.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:@"DetailSegue"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.selectedObject = [sortedObjects objectAtIndex:selectedRowIndex.row];
detailViewController.detailsDataSource = [[NSArray alloc]initWithArray:sortedObjects];
detailViewController.detailIndex = selectedRowIndex.row;
}
}
Actually the answer was very simple: I just had to remove the UIView from the scene in the storyboard and place the UIScrollView directly onto the DetailViewController scene instead.