Playing video chosen in ViewController in IOS - ios

I have a Master-Detail Application. In there, I've been able to download a XML file and use it as an array.
But I want to choose a tab and have it to play a video.
All of this works except in the following code block.
(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
When I try to alloc a MPMoviePlayer, it crashes the program. Any insight would be greatly appreciated.
Code :
MasterViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface MasterViewController : UITableViewController
#property (strong, nonatomic) NSDictionary *presidents;
#property (strong, nonatomic) NSArray *number;
#property (strong, nonatomic) NSArray *name;
#property (strong, nonatomic) NSArray *years;
#property (strong, nonatomic) NSArray *party;
#property (strong, nonatomic) NSArray *image;
#end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController () {
NSMutableArray *_objects;
}
#end
#implementation MasterViewController
#synthesize number, name, years, party, image;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
self.navigationItem.title=#"Presidents";
NSString *urlStr = [[NSString alloc]
initWithFormat:#"http://pickupboyd.com/wordpress/wp-content/uploads/2012/04/Presidents12.plist"];
NSURL *url = [NSURL URLWithString:urlStr];
_presidents = [[NSDictionary alloc] initWithContentsOfURL:url];
name = [_presidents objectForKey:#"Names"];
years = [_presidents objectForKey:#"Years"];
party = [_presidents objectForKey:#"Party"];
image = [_presidents objectForKey:#"Images"];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//This is to the get count of the array in dictionary "Presidents" called Number
number = [_presidents objectForKey:#"Number"];
return number.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSString *nameOfPresident = [name objectAtIndex:indexPath.row];
cell.textLabel.text = nameOfPresident;
NSString *datesInOffice = [years objectAtIndex:indexPath.row];
cell.detailTextLabel.text = datesInOffice;
//set the image view-------------------------------------
NSString *presidentImages = [image objectAtIndex:indexPath.row];
cell.imageView.image = [UIImage imageNamed:presidentImages];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSURL *movieURL = [party objectAtIndex:indexPath.row];
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
[[player view] setFrame:[self.view bounds]];
[self.view addSubview:[player view]];
[player play];
MPMoviePlayerViewController *playVideo = [[MPMoviePlayerViewController alloc]initWithContentURL:movieURL];
//NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] presentMoviePlayerViewControllerAnimated:playVideo];
}
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface DetailViewController : UIViewController
#property (strong, nonatomic) id detailItem;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
- (void)configureView;
#end
#implementation DetailViewController
#pragma mark - Managing the detail item
- (void)configureView
{
// Update the user interface for the detail item.
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end

Try changing this line:
[[player view] setFrame:[self.view bounds]];
To this:
[[player view] setFrame:[self.view frame]];
And see this for the differences between bounds and frame.

Here's the code i use for playing video in a view after a button tap
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
#interface ViewController : UIViewController
{
NSURL * videoURL;
UITableView * view;
}
#property (nonatomic, strong) MPMoviePlayerController*player;
-(IBAction)playMovie;
#end
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
-(IBAction)playMovie
{
NSString *url = [[NSBundle mainBundle]
pathForResource:#"testy3 (Converted)" ofType:#"mov"];
self.player = [[MPMoviePlayerController alloc]
initWithContentURL: [NSURL fileURLWithPath:url]];
// Play Partial Screen
self.player.view.frame = CGRectMake(0, 0, 320, 400);
[self.view addSubview:self.player.view];
// Play Movie
[self.player play];
}
- (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.
}
#end

Related

how to update label from my tableview, objective-c

Here is I trying to update my label from tableview. I am doing something wrong, and can't grasp it. Label in different table view. So, I am trying to send data from table view to detail view controller
#import "ViewController.h"
#import "TechStars.h"
#import "InfoViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.techstars = [TechStars stars];
self.tableView.delegate = self;
self.tableView.dataSource = self;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [self.techstars count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 80;
}
- (UIImage *)imageScaledToSize:(UIImage *)image size:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"employeeCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[[cell detailTextLabel] setNumberOfLines:0]; // unlimited number of lines
[[cell detailTextLabel] setLineBreakMode:NSLineBreakByWordWrapping];
[[cell detailTextLabel] setFont:[UIFont systemFontOfSize: 12.0]];
NSDictionary *employee = self.techstars[indexPath.row];
cell.textLabel.text = [employee objectForKey:#"employeename"];
cell.detailTextLabel.text = [employee objectForKey:#"position"];
cell.imageView.image = [self imageScaledToSize :[employee objectForKey:#"profilePicture"] size:CGSizeMake(95, 75)];
return cell;
}
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([sender isKindOfClass:[UITableViewCell class]])
{
if ([segue.destinationViewController isKindOfClass:[InfoViewController class]])
{
InfoViewController *nextViewController = segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForCell:sender];
TechStars *selectedObject = [self.techstars objectAtIndex:path.row];
// this also can be written as self.techstars[path.row]; literal syntax
nextViewController.stars = selectedObject;
}
}
}
InfoViewController.h
// Test
//
// Created by 123 on 10/07/16.
// Copyright © 2016 123. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "TechStars.h"
#import "ViewController.h"
#interface InfoViewController : UIViewController
#property (strong, nonatomic) TechStars *stars;
#property (strong, nonatomic) IBOutlet UILabel *employeeName;
#property (strong, nonatomic) IBOutlet UIImageView *employeeImage;
#property (strong, nonatomic) IBOutlet UILabel *employeeDuties;
#end
// InfoViewController.m
// Test
//
// Created by 123 on 10/07/16.
// Copyright © 2016 123. All rights reserved.
//
#import "InfoViewController.h"
#import "ViewController.h"
#interface InfoViewController ()
#end
#implementation InfoViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Please try the below code:
-(void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"secondView"])
{
UINavigationController* controller = [segue destinationViewController];
NSArray *viewControllers = controller.viewControllers;
SecondViewController* viewController = [viewControllers objectAtIndex:0];
NSIndexPath *path = [self.tableView indexPathForCell:sender];
TechStars *selectedObject = [self.techstars objectAtIndex:path.row];
viewController.stars = selectedObject ;
}
}

Master Detail Template Not Displaying Rows

I used the Master - Detail application to create a new project in xcode. Each time I run my program, the rows are not visible in the master view. Thanks in advance! Please check my code below...
MasterViewController.h
#import <UIKit/UIKit.h>
#class DetailViewController;
#interface MasterViewController : UITableViewController
#property (strong, nonatomic) DetailViewController *detailViewController;
#end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#interface MasterViewController ()
#property NSMutableArray *objects;
#end
#implementation MasterViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] ***emphasized text***initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
self.detailViewController = (DetailViewController *) [[self.splitViewController.viewControllers lastObject] topViewController];
}
-(void)viewWillAppear:(BOOL)animated {
self.clearsSelectionOnViewWillAppear = self.splitViewController.isCollapsed;
[super viewWillAppear:animated];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender {
if (!self.objects) {
self.objects = [[NSMutableArray alloc] init];
}
[self.objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Segues
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = self.objects[indexPath.row];
DetailViewController *controller = (DetailViewController *)[[segue destinationViewController] topViewController];
[controller setDetailItem:object];
controller.navigationItem.leftBarButtonItem = self.splitViewController.displayModeButtonItem;
controller.navigationItem.leftItemsSupplementBackButton = YES;
}
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section {
return self.objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"Cell" forIndexPath:indexPath];
NSDate *object = self.objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[self.objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
#end
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController
#property (strong, nonatomic) id detailItem;
#property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#end
DetailViewController.m
#import "DetailViewController.h"
#interface DetailViewController ()
#end
#implementation DetailViewController
#pragma mark - Managing the detail item
-(void)setDetailItem:(id)newDetailItem {
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
-(void)configureView {
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
-(void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
-(void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
I figured, if you scale the size of the device to 75% and 100%, the rows will display. I guess this has something to do with xcode.

ToDoList tutorial for XCode No Getter Method for read from property- OBJ C

I've gone through the XCode app development tutorial a few times now and keep coming up with the same error.
source.ToDoItem gives the error "no getter method to read from property"
Any ideas on how to resolve this? I can't seem to write a correct getter method and I can't find an example in the development tutorial to go off of.
- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
AddToDoItemViewController *source = [segue sourceViewController];
ToDoItem *item = source.ToDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
ToDoItem.h
#import <Foundation/Foundation.h>
#interface ToDoItem : NSObject
#property NSString *itemName;
#property BOOL completed;
#property (readonly) NSDate *creationDate;
#end
ToDoItem.m
#import "ToDoItem.h"
#implementation ToDoItem
#end
ToDoListTableViewController
#import "ToDoListTableViewController.h"
#import "AddToDoItemViewController.h"
#import "ToDoItem.h"
#interface ToDoListTableViewController ()
#property NSMutableArray *toDoItems;
#end
#implementation ToDoListTableViewController
- (void)loadInitialData {
ToDoItem *item1 = [[ToDoItem alloc] init];
item1.itemName = #"things";
[self.toDoItems addObject:item1];
ToDoItem *item2 = [[ToDoItem alloc] init];
item2.itemName = #"Things to do with things";
[self.toDoItems addObject:item2];
ToDoItem *item3 = [[ToDoItem alloc] init];
item3.itemName = #"Write Ethics Paper";
[self.toDoItems addObject:item3];
}
- (IBAction)unwindToList:(UIStoryboardSegue *)segue {
AddToDoItemViewController *source = [segue sourceViewController];
ToDoItem *item = source.ToDoItem;
if (item != nil) {
[self.toDoItems addObject:item];
[self.tableView reloadData];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.toDoItems = [[NSMutableArray alloc] init];
[self loadInitialData];
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
// Return the number of rows in the section.
return [self.toDoItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"ListPrototypeCell" forIndexPath:indexPath];
ToDoItem *toDoItem = [self.toDoItems objectAtIndex:indexPath.row];
cell.textLabel.text = toDoItem.itemName;
// Configure the cell...
if (toDoItem.completed) {
cell.accessoryType = UITableViewCellAccessoryCheckmark;
} else {
cell.accessoryType = UITableViewCellAccessoryNone;
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tableView deselectRowAtIndexPath:indexPath animated:NO];
ToDoItem *tappedItem = [self.toDoItems objectAtIndex:indexPath.row];
tappedItem.completed = !tappedItem.completed;
[tableView reloadRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}
#end
and ToDoListTableViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"
#import "AddToDoItemViewController.h"
#interface ToDoListTableViewController : UITableViewController
- (IBAction)unwindToList:(UIStoryboardSegue *)segue;
#end
AddToDoItemViewController.m
#import "AddToDoItemViewController.h"
#import "ToDoItem.h"
#interface AddToDoItemViewController ()
#property (weak, nonatomic) IBOutlet UITextField *textField;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
#end
#implementation AddToDoItemViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 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 {
if (sender != self.saveButton) return;
if (self.textField.text.length > 0) {
}
self.toDoItem = [[ToDoItem alloc] init];
self.toDoItem.itemName = self.textField.text;
self.toDoItem.completed = NO;
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
#end
AddToDoItemViewController.h
#import <UIKit/UIKit.h>
#import "ToDoItem.h"
#interface AddToDoItemViewController : UIViewController
#property ToDoItem *toDoItem;
#property NSString *itemName;
#property BOOL completed;
#property (readonly) NSDate *creationDate;
#end
In AddToDoItemViewController you have
#property ToDoItem *toDoItem;
but in the code you use it as:
ToDoItem *item = source.ToDoItem;
where it should be:
ToDoItem *item = source.toDoItem;
Note the naming convention. The class name starts with a capital letter and the property name starts with a lower case letter.
Try to change these two lines:
#property NSString *itemName;
#property BOOL completed;
For this:
#property (nonatomic, strong) NSString *itemName;
#property (nonatomic, readwrite) BOOL completed;

Objective C - Passing/retrieving selected image from UIImageView to second VC with tableview so cell can display that image

i already passed input text with prepareForSegue, text into mutablearray, then passing that array with prepareforeSegue to second VC with tableview so table cell now displays name. But what with selected/picked image in uIImageView in first vc so table cell in second VC can display image in a row next to name? i manage to do it with image from imagexxassets i followed the same logic like passing text but keep failing.
VIewController.h
#import <UIKit/UIKit.h>
#import "TableViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#interface ViewController : UIViewController<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
#property (strong, nonatomic) IBOutlet UITextField *inputName;
#property (nonatomic) NSMutableArray *items;
//#property (nonatomic) NSMutableArray *images;
- (IBAction)AddData:(id)sender;
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (strong, nonatomic) IBOutlet UIButton *selectPhoto;
#property (strong, nonatomic) IBOutlet UIButton *takePhotoFromCamera;
#end
ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
_items = [[NSMutableArray alloc]init];
//_images = [[NSMutableArray alloc]init];
[self.takePhotoFromCamera addTarget:self action:#selector(_takePhoto) forControlEvents:UIControlEventTouchUpInside];
[self.selectPhoto addTarget:self action:#selector(_selectPhoto) forControlEvents:UIControlEventTouchUpInside];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)AddData:(id)sender
{
//[_images addObject:self.imageView.image];
//[_images insertObject:self.imageView.image atIndex:0];
if ([self.inputName.text length]> 0)
{
[_items addObject:self.inputName.text];
//[_images addObject:self.imageView.image];
[self performSegueWithIdentifier:#"tableSegue" sender:self];
}
else
{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:#"Error"
message:#"You must enter some data"
delegate:self
cancelButtonTitle:#"OK" otherButtonTitles:nil, nil];
[alertView show];
}
}
- (void)addItemViewController:(TableViewController *)controller didFinishSelectingItem:(NSMutableArray *)item selectedTag:(int)tag{
NSLog(#"DATA=%#", item);
}
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([segue.identifier isEqualToString:#"tableSegue"])
{
TableViewController *controller = (TableViewController *)segue.destinationViewController;
controller.items = _items;
//TableViewController *controller1 = (TableViewController *)segue.destinationViewController;
//controller1.images = _images;
NSLog(#"%#",self.inputName.text);
self.inputName.text = NULL;
}
}
#pragma mark code for images
-(void)_takePhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)_selectPhoto
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:NO completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
self.imageView.image = image;
}
}
-(void)imagePickerControllerDidCancel:
(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#end
TableViewController.h
#import <UIKit/UIKit.h>
//using delegate
#protocol TableViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishSelectingItem:(NSMutableArray *)item selectedTag:(int)tag;
#end
//extra attention here
#interface TableViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
// id of delegate
#property (nonatomic, weak) id <TableViewControllerDelegate> delegate;
#property (nonatomic) NSMutableArray *items;
//#property (nonatomic) NSMutableArray *images;
#property (strong, nonatomic) IBOutlet UITableView *tableView;
#end
TableViewController.m
#import "TableViewController.h"
#interface TableViewController ()
#end
#implementation TableViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// code for reversing objects in array so tableview can put new rows on top
//_items = [[[_items reverseObjectEnumerator] allObjects] mutableCopy];
self.tableView.delegate = self;
self.tableView.dataSource = self;
// setting invisible footer to display only rows which contains data and to remove border lines in tableview
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 10.0f)];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *myCell = [tableView dequeueReusableCellWithIdentifier:#"BasicCell" forIndexPath:indexPath];
//NSString *lijevaSlika = [NSString stringWithFormat:#"dice%i", sum];
//[questionTexts objectAtIndex:0]
//NSString *selectedImage = [NSString stringWithFormat:_images];
myCell.imageView.image = [UIImage imageNamed:#"2.png"];
myCell.textLabel.text = _items[indexPath.row]; //name of objects in array are displayed in cells
myCell.textLabel.textColor = [UIColor blueColor]; //changing color of text in cell
//changing background of cell
/*myCell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"back.png"]];
myCell.selectedBackgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"front.png"]];
*/
return myCell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.items count]; //objects in arrays - rows in table
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
[_items removeObjectAtIndex:indexPath.row]; //swipe to left do delete row
[self.tableView reloadData];
}
/*- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//selected row returns to previous viewcontroller
[self.navigationController popViewControllerAnimated:YES];
}
*/
/*
#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
First you have to store selected images in array then you have to pass that array in same way you are passing _items array.Then In cellForRowAtIndexPath of TableViewController add this line and check you are getting anything in _images array
myCell.imageView.image = [_images objectAtIndex:indexPath.row];

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.

Resources