I've started from the standard PageView-based application template provided by Apple. Now instead of text I want the pages in the app to display images. An array is created as follows:
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
nil];
This array is handled the following way:
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
dataViewController.dataObject = [self.pageData objectAtIndex:index];
return dataViewController;
Then, in DataViewController.h:
@interface DataViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UIImage *dataObject;
And in DataViewController.m I'm trying to display the images in de imageView in the following way:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
NSLog(@"dataObject %@", _dataObject);
}
However, with this code I'm not succeeding in getting the images onto the PageViewController. Any idea how I could get this working properly?
A big thanks for you help!
Here
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
you are creating a new UIImageView that is dangling arround unused, invisible and without a parent. You need to set it's size somehow and put it to a parent view with a
[<parent> addSubview:self.imageView]
to show it.
If your original UIImageView is read from a resource file ( I assume so because it is a IBOutlet ), just do a
self.imageView.image = _dataObject
and this should do the job instead.
This worked for me
NSArray *anArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"transparente.png"],
[UIImage imageNamed:@"21001.jpg"],
[UIImage imageNamed:@"21002.jpg"],
[UIImage imageNamed:@"21003.jpg"],
[UIImage imageNamed:@"21004.jpg"],
[UIImage imageNamed:@"transparente2.png"],
nil];
_pageData = [anArray copy];
and in the dataViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.pageImage.image = self.dataObject;
}
The pageImage is an UIImageView with an IBOutlet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With