UIImage not being received via first view controller - ios

Hi there i currently have just "think i have cracked sending content through a prepare for segue" but the problem is that my images are not showing up in the second view controller. As I'm kind of new to this i would like to know if there is something that i am missing because no matter what i can't seem to get the image of my first view controller to show in my second view controller.
GroupsViewController.h
#import <UIKit/UIKit.h>
#interface GroupsViewController : UIViewController<UICollectionViewDataSource, UICollectionViewDelegate>
#property (weak, nonatomic) IBOutlet UICollectionView *GroupsCollectionView;
- (IBAction)cellToggleAction:(id)sender;
#end
GroupsViewController.m
#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"
#interface GroupsViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
}
#end
#implementation GroupsViewController
{
NSString *reuseIdentifier;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self GroupsCollectionView]setDataSource:self];
[[self GroupsCollectionView]setDelegate:self];
reuseIdentifier= #"SmallIcon"; //set inital value of reuse identifier
arrayOfImages = [[NSArray alloc]initWithObjects:#"a.png", nil];
arrayOfDescriptions = [[NSArray alloc]initWithObjects:#"A", nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
[self performSegueWithIdentifier:#"GroupsHomeSegue" sender:indexPath];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"GroupsHomeSegue"])
{
NSIndexPath* indexPath = [[self.GroupsCollectionView indexPathsForSelectedItems]firstObject];
if(indexPath !=nil)
{
//NSString *selectedDescription = arrayOfDescriptions[indexPath.item]; //collects description from cell
NSString *selectedImage = arrayOfImages [indexPath.item]; //collects image from cell
GroupsHomeViewController *groupsHomeViewController = segue.destinationViewController;
groupsHomeViewController.logoImage = [UIImage imageNamed: selectedImage];
//groupsHomeViewController.groupLabel = [:selectedDescription];
}
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
#end
GroupsHomeViewController.h
#import <UIKit/UIKit.h>
#interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UIImage *logoImage;
#property (strong, nonatomic) IBOutlet UILabel *groupLabel;
#end
GroupsHomeViewController.m
#import "GroupsHomeViewController.h"
#interface GroupsHomeViewController ()
#end
#implementation GroupsHomeViewController
-(void)viewDidLoad{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Thank You in advance for your kind help.

Lee Sugden,
Passing an image is not enough :) If you want it to show on your screen you should load it in an ImageView isn't it :)
Now either create UIImageView programmatically or add a imageView in your story board draw an IBOutlet and set the image as
self.yourImageView.image = self.logoImage;
else create an UIImageView programmatically as,
UIImageView *yourImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,self.logoImage.size.width,self.logoImage.size.height)];
yourImageView.image = self.logoImage;
[self.view addSubview:yourImageView];

The issue lies in your GroupsHomeViewController. You need a UIImageView as your IBOutlet rather than a UIImage, then you need to set the image property on the image view to your desired image. To do this, you need to update your GroupsHomeViewController.h file like so:
#interface GroupsHomeViewController : UIViewController<UITableViewDataSource, UITableViewDelegate>
#property (strong, nonatomic) IBOutlet UIImageView *logoImageView;
#property (strong, nonatomic) UIImage *logoImage;
#property (strong, nonatomic) IBOutlet UILabel *groupLabel;
#end
And then in your GroupsHomeViewController.m, set your image view's image in the viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.logoImageView.image = self.logoImage;
}
Now, when you set logoImage in your segue, the image view will load the image just before the view appears.
This is assuming that you have the image view already on your storyboard with the outlet correctly connected.

I think the guys above may have solved this with the advice to set the image to whatever image view is holding it. But if that still hasn't solved it, with prepareForSegue, I've had problems due to iOS voodoo where the variable isn't set. So to be sure, override the setter in your GroupsHomeViewController.m to make sure the object is properly assigned to your image property.
- (void)setLogoImage:(UIImage *)logoImage
{
_logoImage = logoImage;
}

Related

How to pass the value between viewcontrollerB into viiewcontrollerA using push segue

I'm having two view controller A and B. when click button on view controllerA it will navigate to view controllerB and their is a tableview with textlabel,when i click on tableviewcell i want to navigate back into viewcontrollerA and want to pass that textlabel value back to viewcontrollerA
if you are using push seque is the worst way, reason you increase the memory in your stack trace.
you can do this in multiple ways, search once in google you get multiple answer related to this
UnWind Segue
Protocols & delegates
local database method
using Singleton class.
Using bean object method.
using Plist
finally go for NSUserDefault
I created a sample using Protocols and Delegates, as follows:
ViewController.m
#import "ViewController.h"
#import "ColorsTableViewController.h"
#interface ViewController () <ColorsDelegate>
#property (weak, nonatomic) IBOutlet UILabel *textDataLabel;
- (IBAction)goButtonTapped:(id)sender;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)sendColor:(NSString *)colorName{
self.textDataLabel.text = colorName;
}
- (IBAction)goButtonTapped:(id)sender {
ColorsTableViewController *colorsTableViewController=(ColorsTableViewController *)[self.storyboard instantiateViewControllerWithIdentifier:#"colorTableIdentifier"];
colorsTableViewController.delegate = self;
[self.navigationController pushViewController:colorsTableViewController animated:YES];
}
#end
ColorsTableViewController.h
#import <UIKit/UIKit.h>
#protocol ColorsDelegate <NSObject>
#optional
- (void)sendColor:(NSString *)colorName;
#end
#interface ColorsTableViewController : UIViewController
#property (nonatomic, assign) id<ColorsDelegate> delegate;
#end
ColorsTableViewController.m
#import "ColorsTableViewController.h"
#interface ColorsTableViewController () <UITableViewDelegate, UITableViewDataSource>
#property (weak, nonatomic) IBOutlet UITableView *colorsTableView;
#property (strong, nonatomic) NSArray *colorsArray;
#end
#implementation ColorsTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.colorsArray = #[#"Red",#"Green",#"Blue",#"Brown",#"Yellow"];
[self.colorsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:#"colorCell"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.colorsArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *myCell = [self.colorsTableView dequeueReusableCellWithIdentifier:#"colorCell" forIndexPath:indexPath];
UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
if(!myTextLabel)
{
myTextLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, CGRectGetWidth(myCell.frame), CGRectGetHeight(myCell.frame))];
myTextLabel.tag = 100;
}
myTextLabel.text = self.colorsArray[indexPath.row];
[myCell.contentView addSubview:myTextLabel];
return myCell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *myCell = (UITableViewCell *)[self.colorsTableView cellForRowAtIndexPath:indexPath];
UILabel *myTextLabel = [myCell.contentView viewWithTag:100];
if(myTextLabel)
{
[_delegate sendColor:myTextLabel.text];
}
}
Screenshot:
Here is my GitHub Link, please download and check:
https://github.com/k-sathireddy/sendDataBack

CollectionViewCell Open a new view controller with previous cell title and image [duplicate]

This question already has answers here:
Passing data between view controllers
(45 answers)
Closed 7 years ago.
I have looked all over this website for 3 hours now and i cannot find out how to do this without referencing to manually connecting all of my 106 collection view cells with segues to another ViewController.
I would like to be able to click on my CustomCell (we will call it A) when i click on A i want to open a new viewController with the Page Title being A and the image i have set in the CustomCell to be the Page Logo within a UIImage on the view controller page.
I am aware this has to do with passing information between ViewControllers and i have no idea how to do this. Therefore i am very stuck.
I have only been using Xcode for a month now and i need it for my apprenticeship project so it would be very helpful if someone could help me.
My Current code is as follows, if someone is so kind enough to help please could you comment your code so that i get a further understanding as i don't just want to copy and paste the code and learn nothing from it.
Many thanks in advance.
ViewController.m
#import "GroupsViewController.h"
#import "GroupsHomeViewController.h"
#import "CustomCell.h"
#interface GroupsViewController ()
{
NSArray *arrayOfImages;
NSArray *arrayOfDescriptions;
}
#end
#implementation GroupsViewController
{
NSString *reuseIdentifier;
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self GroupsCollectionView]setDataSource:self];
[[self GroupsCollectionView]setDelegate:self];
reuseIdentifier= #"SmallIcon";
arrayOfImages = [[NSArray alloc]initWithObjects:#"A.png", nil];
arrayOfDescriptions = [[NSArray alloc]initWithObjects:#"A",nil];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [arrayOfDescriptions count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
[[cell IconImage]setImage:[UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]]];
[[cell IconLabel]setText:[arrayOfDescriptions objectAtIndex:indexPath.item]];
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:#"customSegue" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = (NSIndexPath *)sender;
UIImage *imageToShow = [UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]];
NSString *titleToShow = [arrayOfDescriptions objectAtIndex:indexPath.item];
GroupsHomeViewController * destination = (GroupsHomeViewController *)segue.destinationViewController;
destination.groupLabel = *titleToShow; //assigning to UILabel form incompatible type NSString
destination.logoImage = *imageToShow; // UIImageView from incompatible type UImage
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
//Dispose of any resources that can be recreated.
}
// Toggle View Button
- (IBAction)cellToggleAction:(id)sender {
if([reuseIdentifier isEqualToString:#"SmallIcon"]){
reuseIdentifier=#"ListView";
[sender setImage:[UIImage imageNamed:#"LargeIcon"]];
}
else if
([reuseIdentifier isEqualToString:#"ListView"]){
reuseIdentifier=#"LargeIcon";
[sender setImage:[UIImage imageNamed:#"SmallIcon"]];
}
else if
([reuseIdentifier isEqualToString:#"LargeIcon"]){
reuseIdentifier=#"SmallIcon";
[sender setImage:[UIImage imageNamed:#"ListView"]];
}
[self.GroupsCollectionView reloadData];
}
//Toggled Cell Sizes
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
CGSize cellSize;
if([reuseIdentifier isEqualToString:#"SmallIcon"])
cellSize = CGSizeMake(100, 130);
else if
([reuseIdentifier isEqualToString:#"ListView"])
cellSize = CGSizeMake(320, 50);
else if
([reuseIdentifier isEqualToString:#"LargeIcon"])
cellSize = CGSizeMake(320, 350);
return cellSize;
}
#end
GroupsHomeViewController.m where the groups view controller pay goes to.
#import "GroupsHomeViewController.h"
#interface GroupsHomeViewController ()
#end
#implementation GroupsHomeViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.groupLabel.text = self.groupLabel; //incompatible pointertypes assigning to NSSstring
self.logoImage.image = self.logoImage; //incompatible pointer types assigning to NSSstring
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
#end
CustomeCell.h
#import <UIKit/UIKit.h>
#interface CustomCell : UICollectionViewCell
#property (weak, nonatomic) IBOutlet UIImageView *IconImage;
#property (weak, nonatomic) IBOutlet UILabel *IconLabel;
#end
Ok. This is a pretty big question. But first of all you would need to make a custom VC subclass for the target of the segue. You should definitely not have 106 different segues. Especially since it sounds like they will all be basically leading to the same place. A View controller that can show an image and a name. setup this new VC in your storyboard and make one segue from the viewController (not the cell) (Drag from the yellow circle ontop of the collection VC) to the new VC. and call it customSegue
SO setup a subclass something like:
#interface SelectedInfoViewController : UIViewController
#property (strong, nonatomic) NSString *selectedInfoName;
#property (strong, nonatomic) UIImage *selectedInfoImage;
#end
#implementation SelectedViewController
-(void)viewDidLoad{
[super viewDidLoad];
self.labelForTitle.text = self.selecteInfoName;
self.imageViewForImage.image = self.selecteInfoImage;
}
#end
Then you would add to your collection view controller.
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
[self performSegueWithIdentifier:#"customSegue" sender:indexPath];
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSIndexPath *indexPath = (NSIndexPath *)sender;
UIImage *imageToShow = [UIImage imageNamed:[arrayOfImages objectAtIndex:indexPath.item]];
NSString *titleToShow = [arrayOfDescriptions objectAtIndex:indexPath.item]];
SelectedInfoViewController * destination = (SelectedInfoViewController *)segue.destinationViewController;
destination.selectionInfoName = titleToShow;
destination.selectionInfoImage = imageToShow;
}
Hope this makes some sense. Let me know if you need more help.
As per your errors:
destination.groupLabel = *titleToShow; //assigning to UILabel form incompatible type NSString
destination.logoImage = *imageToShow; // UIImageView from incompatible type UImage
self.groupLabel.text = self.groupLabel; //incompatible pointertypes assigning to NSSstring
self.logoImage.image = self.logoImage; //incompatible pointer types assigning to NSSstring
These are two separate things that you are assigning to each other.
So as I highlighted you need to setup:
#interface SelectedInfoViewController : UIViewController
#property (strong, nonatomic) NSString *selectedInfoName;
#property (strong, nonatomic) UIImage *selectedInfoImage;
but also we need:
#property (strong, nonatomic) IBOutlet UILabel *infoLabel;
#property (strong, nonatomic) IBOutlet UIImageView *infoImage;
#end
Soooo. In the segue you should be setting the selectedInfoName, and selectedInfoImage. And then in the viewDidLoad you should be setting the infoLabel.text property to the passed selectedInfoName and the infoImage.image property to the passed selectedInfoImage.
Oh also as I had alluded before you need to link the infoLabel & infoImage to their respective counterparts in the storyboard.

My tableview's cell is not displayed

FirstPage.h
#import <UIKit/UIKit.h>
#import "StudentPage.h"
#interface FirstPage : UIViewController <UITableViewDelegate,UITableViewDataSource,StudentInfoDelegates>
#property (strong, nonatomic) IBOutlet UITableView *tableView;
- (IBAction)addButtonAction:(id)sender;
#end
FirstPage.m
#import "FirstPage.h"
#interface FirstPage ()
#end
#implementation FirstPage
{
NSMutableArray *firstPageArray;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
//[self performSegueWithIdentifier:#"StudentInfo" sender:sender];
/*StudentPage *student=[[StudentPage alloc]init];
student.delegates=self;*/
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//the next following didn't created..
//the RowAtIndexPath are not been executed..
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *concateValue=#"";
NSMutableDictionary *studentSecondDictionary=[[NSMutableDictionary alloc]init];
static NSString *CellIdentifier=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell==nil){
cell=[[UITableViewCell alloc]initWithFrame:CGRectZero];
[cell setBackgroundColor:[UIColor redColor]];
[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
[cell setIndentationWidth:0.0];
studentSecondDictionary=[firstPageArray objectAtIndex:indexPath.row];
NSString *name1=[studentSecondDictionary valueForKey:#"NAME"];
NSString *regno1=[studentSecondDictionary valueForKey:#"REGNO"];
NSString *marks1=[studentSecondDictionary valueForKey:#"MARKS"];
NSString *rank1=[studentSecondDictionary valueForKey:#"RANK"];
concateValue=[[[[[[name1 stringByAppendingString:#" "]stringByAppendingString:regno1]stringByAppendingString:#" "]stringByAppendingString:marks1]stringByAppendingString:#" "]stringByAppendingString:rank1];
cell.textLabel.text=concateValue;
}
return cell;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return firstPageArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return 50;
}
- (IBAction)addButtonAction:(id)sender {
StudentPage *stdView=[self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"];
stdView.delegates=self;
[self.navigationController pushViewController:stdView animated:YES];
//[self.view addSubview:stdView.view];
}
-(void)didFinishSave:(NSMutableArray *)in_studentList{
firstPageArray=[[NSMutableArray alloc]init];
firstPageArray=in_studentList;
[self.tableView reloadData];
}
#end
StudentPage.h
#import <UIKit/UIKit.h>
#protocol StudentInfoDelegates
-(void)didFinishSave:(NSMutableArray*)in_studentList;
#end
#interface StudentPage : UIViewController<UITextFieldDelegate>
#property(assign,nonatomic)id<StudentInfoDelegates> delegates;
#property (strong, nonatomic) IBOutlet UITextField *name;
#property (strong, nonatomic) IBOutlet UITextField *regno;
#property (strong, nonatomic) IBOutlet UITextField *marks;
#property (strong, nonatomic) IBOutlet UITextField *rank;
- (IBAction)save:(UIButton *)sender;
#end
StudentPage.m
#import "StudentPage.h"
#import "FirstPage.h"
#implementation StudentPage
{
NSMutableArray *studentArray;
NSMutableDictionary *StudentDictionary;
}
#synthesize name,regno,marks,rank;
#synthesize delegates;
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self.view endEditing:YES];
[super touchesBegan:touches withEvent:event];
}
- (IBAction)save:(UIButton *)sender {
studentArray=[[NSMutableArray alloc]init];
StudentDictionary=[[NSMutableDictionary alloc]init];
[StudentDictionary setValue:name.text forKey:#"NAME"];
[StudentDictionary setValue:regno.text forKey:#"REGNO"];
[StudentDictionary setValue:marks.text forKey:#"MARKS"];
[StudentDictionary setValue:rank.text forKey:#"RANK"];
[studentArray addObject:StudentDictionary];
[delegates didFinishSave:studentArray];
NSLog(#"%#",studentArray);
FirstPage *firstView=[self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier1"];
[self.navigationController pushViewController:firstView animated:YES];
}
#end
As #BiWoj said in the comments, you are checking for
if (cell==nil)...
and rely on that check to go through the first time so you can fill out your data. This is wrong, you instead dequeue the cell and then when it's not nil you can start putting your data in its views. The only reason that cell will be nil is when you dequeue, using a non-existent identifier i.e. it should never happen.
First just check your delegate method is getting called or not when you click on save button.
I think you forgot to set delegate to self before
[delegates didFinishSave:studentArray];
It should be
self.delegate = delegates
[delegates didFinishSave:studentArray];
If this does not work please check that, you are pushing student page from first page like this
- (IBAction)addButtonAction:(id)sender {
StudentPage *stdView=[self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier"];
stdView.delegates=self;
[self.navigationController pushViewController:stdView animated:YES];}
And one again on your save action method of student page you are pushing first page like this.
FirstPage *firstView=[self.storyboard instantiateViewControllerWithIdentifier:#"MyIdentifier1"];
[self.navigationController pushViewController:firstView animated:YES];
Well i guess instead of that you have to just use
[self.navigationController popToRootViewControllerAnimated:YES];
There is no need to push again the same controller you just have to pop to base controller.

How do you load video from an array in table detail view when the user clicks on the cell?

I have loaded a table view with NSArray data. Now, I want to play a video in a detail view. The video played depends on what cell the user clicks.
How do I test the cell clicked and load a video from the array based on that?
I am using Xcode 5 for iOS 7.
Here is my ViewController.m
#import "ViewController.h"
#import "VideoDetailViewController.h"
#interface ViewController ()
{
NSMutableArray * titlearray;
NSMutableArray * arrayVidSrc;
}
#end
#implementation ViewController
#synthesize mytableview;
- (void)viewDidLoad
{
[super viewDidLoad];
//sets delegate methods of table view
self.mytableview.delegate=self;
self.mytableview.dataSource=self;
//assigns values to objects in array's
titlearray = [[NSMutableArray alloc]initWithObjects:#"intro", #"skating",nil];
arrayVidSrc = [[NSMutableArray alloc]initWithObjects:#"Step1-Intro.mp4", #"Step2-Skating.mp4", nil];
}
// returns one section in the table
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//counts the items in the title array
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [titlearray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier =#"vidCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath: indexPath];
cell.textLabel.text = [titlearray objectAtIndex:indexPath.row];
return cell;
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"showDetailsSeg"]) {
NSIndexPath *indexpath =nil;
NSString *titlestring =nil;
indexpath = [mytableview indexPathForSelectedRow];
titlestring = [titlearray objectAtIndex:indexpath.row];
NSURL * movieURL = [arrayVidSrc objectAtIndex:indexpath.row];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL: movieURL];
[[player view] setFrame: [self.view bounds]];
[self.view addSubview: [player view]];
[player play];
[[segue destinationViewController] setTitlecontents:titlestring];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And here is my ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
//allows us to use the delegate methods of table view
#interface ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
#property (weak, nonatomic) IBOutlet UITableView *mytableview;
#end
I have a custom view controller class VideoDetailViewController.m:
#import "VideoDetailViewController.h"
#interface VideoDetailViewController ()
#end
#implementation VideoDetailViewController
#synthesize arrayVidSrc = _arrayVidSrc;
#synthesize titlelabel;
#synthesize navBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.titlelabel.text = self.titlecontents;
self.navBar.title = self.titlecontents;
//video load from array
// NSURL * movieURL = [_arrayVidSrc objectAtIndex:NSIndexPath.row];
//Play the movie now
/*MPMoviePlayerViewController *playercontroller = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:playercontroller];
playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[playercontroller.moviePlayer play];
playercontroller = nil;*/
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
and the .h file that goes with it:
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface VideoDetailViewController : UIViewController
#property (weak, nonatomic) IBOutlet UINavigationItem *navBar;
#property (weak, nonatomic) IBOutlet UILabel *selectedRow;
#property (strong, nonatomic) NSString * titlecontents;
#property (weak, nonatomic) IBOutlet UILabel * titlelabel;
#property (strong, nonatomic) NSArray *arrayVidSrc;
#end
Implement the method - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender. The sender is the cell that was clicked. You can then call something like [self.tableView indexPathForCell:sender] to get the cell address and use the row of the returned index path to pick the video out of your array.
The new view controller that will be shown when the segue finishes is available within the prepareForSegue:: method as [segue destinationViewController] and you can pass it whatever it will need.

UIImageView background color but not image displaying

I have a UITableViewCell nib file in which there is a UIImageView and a UILabel. I have outlets for both of these to my controller as well as an outlet for the cell itself. I am setting the label and image programmatically, but the image does not show up.
So I went to test it, and even if I set the image itself in the nib file, it does not show up. If I set the background color, it shows up fine. Any ideas? I'm stuck.
It seems to be unrelated to code, in my mind, since it doesn't even work via nib file. But here it is anyway in case it helps somehow.
MyViewController.h
#interface MyViewController : UITableViewController
#property (strong, nonatomic) MyModel *myModel;
#property (strong, nonatomic) NSArray *tableViewCells;
#property (strong, nonatomic) IBOutlet UITableViewCell *tableViewCell;
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#property (weak, nonatomic) IBOutlet UIImageView *myImage;
#end
MyViewController.m
#interface MyViewController ()
- (void)bindMyModel:(MyModel*)model toView:(UITableViewCell*)view;
- (UITableViewCell*)copyUITableViewCell:(UITableViewCell*)cell;
#end
#implementation MenuViewController
#synthesize myModel = _myModel;
#synthesize tableViewCells = _tableViewCells;
#synthesize tableViewCell = _tableViewCell;
#synthesize myLabel = _myLabel;
#synthesize myImage = _myImage;
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableArray *cells = [[NSMutableArray alloc] init];
[MyModel loadAndOnSuccess:^(id data, id context) {
self.myModel = data;
for (MyModel *item in self.myModel.items) {
[[NSBundle mainBundle] loadNibNamed:#"TableCellNib" owner:self options:nil];
[self bindMyModel:item toView:self.tableViewCell];
[cells addObject:[self copyUITableViewCell:self.tableViewCell]];
self.tableViewCell = nil;
}
self.tableViewCells = [[NSArray alloc] initWithArray:cells];
} onFail:^(NSString *error, id context) {
NSLog(#"FAIL with error: %#", error);
}];
}
- (void)viewDidUnload
{
[self setTableViewCell:nil];
[self setMyLabel:nil];
[self setMyImage:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void) bindMyModel:(MyModel*)model toView:(UITableViewCell*)view
{
if (view) {
self.myLabel.text = model.myLabelText;
self.myImage.image = model.myImageResource;
self.myLabel = nil;
self.myImage = nil;
}
}
- (UITableViewCell*)copyUITableViewCell:(UITableViewCell*)cell
{
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject: cell];
return [NSKeyedUnarchiver unarchiveObjectWithData: archivedData];
}
#pragma mark - Table view data source
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return [self.tableViewCells objectAtIndex:indexPath.row];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return self.myModel.items.count;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Irrelevant code here
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ((UITableViewCell*)[self.tableViewCells objectAtIndex:indexPath.row]).frame.size.height;
}
#end
You are trying to use two IBOutlets on your UITableViewController to populate a multitude of UILabels and UIImageViews that are part of the UITableViewCell. You need to create a custom subclass of UITableViewCell and add the IBOutlets to that subclass.
#interface CustomCell : UITableViewCell
#property (weak, nonatomic) IBOutlet UILabel *myLabel;
#property (weak, nonatomic) IBOutlet UIImageView *myImage;
#end
then in your bindMyModel:toView:
- (void) bindMyModel:(MyModel*)model toView:(CustomCell*)view
{
if (view) {
view.myLabel.text = model.myLabelText;
view.myImage.image = model.myImageResource;
}
}
Now you have independent IBOutlets for each of your Cells. You will also need to change some of your bindings as well. This is a fix, but honestly I would rewrite a lot of the code and just use dequeueReusableCellWithIdentifier in your cellForRowAtIndexPath call, and keep a pool of CustomCells that you will reuse, and just set up the myLabel.text and myImage.image in the cellForRowAtIndexPath call.

Resources