我已经在UIView(我的详细视图)中添加了一个UIScrollView,现在如何显示内容?

时间:2012-07-18 21:25:34

标签: iphone xcode uiview uiscrollview

(在带有故事板的XCode 4中,适用于iPhone的应用程序)

My DetailView:一个充满Labels和ImageViews的UIView,用于显示在MainTableViewController中选择单元格时通过segue收到的详细信息。现在我在这个UIView上添加了一个UIScrollView,因为我希望将标签和ImageViews显示在滚动视图中。

当我运行应用程序时,DetailViewController中唯一可见的是空白ScrollView。我没有显示从TableView传递的内容,也没有获取selectedObject。如何在添加UIScrollView后显示我的内容并能够使用swipeDetectedLeft轻扫单元格?如果你能用我的代码做出示例,我会很高兴。

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..
}

编辑:

如果我设置aLabel.text = @“Test”,那么它可以工作。因此UIScrollView没有从以下位置获取selectedObject:

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;
}
}

1 个答案:

答案 0 :(得分:2)

实际上答案非常简单:我只需要从故事板中的场景中删除UIView,然后将UIScrollView直接放在DetailViewController场景上。