Ok, Here is the code in my AppDelegate that loads the Schedule nib.
Schedule *newestVideoController = [[Schedule alloc] initWithNibName:#"Schedule" bundle:nil];
newestVideoController.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Schedule" image:[UIImage imageNamed:#"app-icon_2_newest.png"] tag:2];
NSArray *controllers = [NSArray arrayWithObjects:navController, newestVideoController, nil];
mainController.viewControllers = controllers;
Now here is my header file for schedule.
#import <UIKit/UIKit.h>
#import "AnotherViewController.h"
#interface Schedule : UITableViewController
<UITableViewDelegate, UITableViewDataSource> {
NSArray *mySchedule;
}
#property (nonatomic,retain) NSArray *mySchedule;
#end
Lastly here again is my implementaion file for "Schedule".
#import "Schedule.h"
#import "AnotherViewController.h"
#implementation Schedule
#synthesize mySchedule;
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return mySchedule.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//create a cell
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:#"cell"];
//fill it with contents
cell.textLabel.text = [mySchedule objectAtIndex:indexPath.row];
//return it
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:#"AnotherViewController" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
}
/*
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
NSString *myFile = [[NSBundle mainBundle] pathForResource:#"exercise" ofType:#"plist"];
mySchedule = [[NSArray alloc] initWithContentsOfFile:myFile];
[super viewDidLoad];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Need more code/info. It looks like you implemented tableView:didSelectRowAtIndexPath: and pushed another View Controller onto the stack correctly, so it's hard to say. But here's a hit list:
Are you certain tableView:didSelectRowAtIndexPath: is getting called? Did you set a breakpoint? If not, you may have forgotten to set your Schedule class as the delegate either programatically or in Interface Builder.
Is schedule's navigation controller definitely set? That is, was it pushed onto a Navigation Controller stack?
Are you sure the nib file for AnotherViewController exists?
Related
I'm trying to create a simple TableView in my current project.
but I got this error after connected datasource in IB and run iOS simulator.
2013-03-22 14:58:32.372 M[1875:c07] -[ViewController tableView:numberOfRowsInSection:]:
unrecognized selector sent to instance 0x88191f0
2013-03-22 14:58:32.374 M[1875:c07] *** Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[ViewController tableView:numberOfRowsInSection:]:
unrecognized selector sent to instance 0x88191f0'
,but when I create a simple TableView in an empty project with the same code it's working.
Here is my code
"MCEventActivityViewController.h"
#import <UIKit/UIKit.h>
#interface MCEventActivityViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#end
"MCEventActivityViewController.m"
#import "MCEventActivityViewController.h"
#interface MCEventActivityViewController ()
#end
#implementation MCEventActivityViewController
{
NSArray *tableData;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
tableData = [NSArray arrayWithObjects:#"1", #"2", #"3", nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *simpleTableIdentifier = #"SimpleTableItem";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
}
cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
return cell;
}
#end
Could anyone help me figure out why this error came out?
The screenshots are here.
Edited:
Make sure,
1.TableView is connected with FileOwner in XIB.
2.#interface Controller : UIViewController UITableViewDelegate,UITableViewDataSource included.
3.Delegate & DataSource both connected with FileOwner.
4.numberOfSections return 1.
5.numberOfRowsInSection returns [required].
6.cellForRowAtIndexPath
{ data goes here...
}
Edited:
Create,
#property (nonatomic,strong) IBOutlet UITabelView * tableView;
and connect this Outlet in Xib with Fileowner
Add numberOfSectionsInTableView method to your code
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
Add NSArray *tableData; to MCEventActivityViewController.h
and set its #property and #synthesize properly.
and write
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.tableData = [[NSArray alloc] initWithObjects:#"1", #"2", #"3", nil];
}
You should link the (dataSource and delegate) from Outlets to the view controller not to view ! like these pictures :
Okay, so I'm writing an iPhone application, and to load the data into the grouped table views, I created a data model:
DataModel.m
#import "GeometryDataModel.h"
#implementation GeometryDataModel
#synthesize titleGeo =_titleGeo;
-(id)initWithTitleGeo:(NSString *)titleGeo
{
if ((self = [super init])) {
self.titleGeo = titleGeo;
}
return self;
}#end
Then I continued to write a data table...
DataTable.m
#import "GeometryTableData.h"
#import "GeometryDataModel.h"
#implementation GeometryTableData
#synthesize dataGeo = _dataGeo;
#synthesize thumbImageGeo = _thumbImageGeo;
- (id)initWithTitleGeo:(NSString *)titleGeo thumbImageGeo:(UIImage *)thumbImageGeo {
if ((self = [super init]))
{
self.dataGeo = [[GeometryDataModel alloc] initWithTitleGeo:titleGeo];
self.thumbImageGeo = _thumbImageGeo;
}
return self;
}
#end
then I created a view controller to set it all up
ViewController.m
#import "GeometryViewController.h"
#import "GeometryTableData.h"
#import "GeometryDataModel.h"
#interface GeometryViewController ()
#end
#implementation GeometryViewController
#synthesize geoCalculatorList = _geoCalculatorList;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// 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
{
return _geoCalculatorList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *geoCell = [tableView dequeueReusableCellWithIdentifier:#"Geometry"];
GeometryTableData *geoTableStuff = [self.geoCalculatorList objectAtIndex:indexPath.row];
geoCell.textLabel.text = geoTableStuff.dataGeo.titleGeo;
geoCell.imageView.image = geoTableStuff.thumbImageGeo;
return geoCell;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
*/
}
#end
And then finally, in the AppDelegate.m file I implement it to the table:
sectionOne = [[TableData alloc] initWithTitle:#"A cell" thumbImage:[UIImage
imageNamed:#"none.png"]];
sectionTwo = [[TableData alloc] initWithTitle:#"Another separated cell" thumbImage:[UIImage
imageNamed:#"none.png"]];
NSMutableArray *TableData = [NSMutableArray arrayWithObjects:sectionOne,sectionTwo, nil];
UITabBarController * tabController = (UITabBarController *) self.window.rootViewController;
ViewController * algebraController = [tabController.viewControllers objectAtIndex:0];
Controller.calculatorList = TableData;
My question is, how would I create sections in the grouped table view, say to put "sectionOne" and "sectionTwo" into different sections on the table using my current system?
Thanks!!!
I'm not sure what you mean by putting "sectionOne" and "sectionTwo" in different sections. Do you want them to be the section views? In general, you put data in sections by having an array that's more complicated than a simple array. It could take several forms, an array of arrays is probably the simplest. If your data is set up that way, then you pass theData.count in numberOfSectionsInTableView and [[theData indexAtObject:section] count] in numberOfRowsInSection. In cellForRowAtIndexPath you would use something like this:
cell.textLabel.text = [[theData objectAtIndex:indexPath.section] objectAtIndexPath:indexPath.row];
You can use the new array syntax to shorten that to :
cell.textLabel.text = theData[indexPath.section][indexPath.row];
I'm new to Objective-C, I like it,
I'm developping a free application for the local firefighters.
The app does nothing really hard, but I have a big problem with the main menu:
I have already created a table content all the section of my application, I am trying to implement the drill down method by means I can access to other ViewControllers, but I really don't know how to do it, I googled a lot but I've only found fragmentary documentation referenced to old versions of Xcode.
I'm using version 4.5.2 and the storyboard.
Here is the menu.h
#import <UIKit/UIKit.h>
#interface Menu : UITableViewController
#property (nonatomic, retain) NSMutableArray *listaMenu; //questo sarà il mio array contentente le varie opzioni della tabella
#end
And the menu.m
#import "Menu.h"
#import "News.h"
#interface Menu ()
#end
#implementation Menu
#synthesize listaMenu; //creo i metodi getter e setter
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Menu";
//Elementi da visualizzare nella tabella
listaMenu = [[NSMutableArray alloc] initWithObjects: #"News", #"Memebri", #"Calendario", #"Interventi", #"Galleria", #"Mezzi", #"Reclutamento", nil];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
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
{
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//funzione in cui va inserito il numero di righe da visualizzare
return [listaMenu count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = nil;
cell =[tableView dequeueReusableCellWithIdentifier:#"PlistCell"];
if(cell == nil){
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:#"PlistCell"];
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
}
cell.textLabel.text = [listaMenu objectAtIndex:indexPath.row];
return cell;
}
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath //Rende gli oggetti NON editabili
{
// Return NO if you do not want the specified item to be editable.
//return YES;
return NO;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
#end
I hope my problem is well explained.
Thank you very much!
You need to read up about UISegue - this is the object that handles transitions from one UIViewController to another controller. In Interface Builder you can Ctrl-Drag to create a Segue between, say a UIButton and the desired UIViewController. This is the simplest usage of segues.
If you need to pass data to the destination UIViewController then you'll need to implement the - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender method. Within this method you can get the segue.destinationViewController and then send it whatever data you need. N.B. The destination view controller's view has not loaded yet, so if you're trying to customise UIViews directly, then they won't exist. You can fudge it by doing [segue.destinationViewController view] to get going, although it would be better to configure non-UI properties and have the view controller itself do any necessary customisation in viewDidLoad.
I have been coding a example of a UITableView with a custom UITableViewCell (PlayerCell). All work fine, I can see the elements in my table and I can select them, but when I do a scroll up, the app crash. I have reviewed the identifier, the class of the controller, everything, but when I use a custom cell, it not works. If I use a default style, it works fine, inclusive the scroll up.
I think I have the problem in this method: didSelectRowAtIndexPath
But the debugger, don't show me an error, only something like: EXEC BAD ADRESSS (I don't have the computer here)
This is my code:
MyTeamViewController.h
#import <UIKit/UIKit.h>
#interface MyTeamViewController : UITableViewController
#property (nonatomic, strong) NSMutableArray *players;
#end
MyTeamViewController.m
#import "MyTeamViewController.h"
#import "Player.h"
#import "PlayerCell.h"
#interface MyTeamViewController ()
#end
#implementation MyTeamViewController
#synthesize players;
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
// Override point for customization after application launch.
players = [NSMutableArray arrayWithCapacity:20];
Player *player = [[Player alloc] init];
player.name = #"Bill Evans";
player.game = #"Tic-Tac-Toe";
player.rating = 4;
[players addObject:player];
player = [[Player alloc] init];
player.name = #"Oscar Peterson";
player.game = #"Spin the Bottle";
player.rating = 5;
[players addObject:player];
player = [[Player alloc] init];
player.name = #"Dave Brubeck";
player.game = #"Texas Hold’em Poker";
player.rating = 2;
[players addObject:player];
[super viewDidLoad];
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#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.
NSUInteger numberCells = players.count;
return numberCells;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"PlayerCell";
PlayerCell *cell = (PlayerCell *)[tableView
dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSLog(#"Cell is NIL");
//I don't know what put here <-- MY PROBLEM!!! :(
}
Player *player = [self.players objectAtIndex:indexPath.row];
cell.nameLabel.text = player.name;
cell.pointsLabel.text = player.game;
cell.clubImageView.image = [self
imageForRating:player.rating];
// Configure the cell...
return cell;
}
- (UIImage *)imageForRating:(int)rating
{
switch (rating)
{
case 1: return [UIImage imageNamed:#"1StarSmall.png"];
case 2: return [UIImage imageNamed:#"2StarsSmall.png"];
case 3: return [UIImage imageNamed:#"3StarsSmall.png"];
case 4: return [UIImage imageNamed:#"4StarsSmall.png"];
case 5: return [UIImage imageNamed:#"5StarsSmall.png"];
}
return nil;
}
/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
*/
/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
// Delete the row from the data source
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject: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
}
}
*/
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
/*
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:#"<#Nib name#>" bundle:nil];
// ...
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
*/
}
#end
The other files:
Player.h
#import <Foundation/Foundation.h>
#interface Player : NSObject
#property (nonatomic, copy) NSString *name;
#property (nonatomic, copy) NSString *game;
#property (nonatomic, assign) int rating;
#end
Player.m
#import "Player.h"
#implementation Player
#synthesize name;
#synthesize game;
#synthesize rating;
#end
PlayerCell.h
#import <UIKit/UIKit.h>
#interface PlayerCell : UITableViewCell
#property (nonatomic, strong) IBOutlet UILabel *nameLabel;
#property (nonatomic, strong) IBOutlet UILabel *pointsLabel;
#property (nonatomic, strong) IBOutlet UIImageView
*clubImageView;
#end
PlayerCell.m
#import "PlayerCell.h"
#implementation PlayerCell
#synthesize nameLabel;
#synthesize pointsLabel;
#synthesize clubImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Thank you so much!!
EDIT: I solved it with: players = [[NSMutableArray arrayWithCapacity:20] retain];
I would try to remove the
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
In the PlayerCell.m - u don't need them.
I have a table based off Sam's Teach Yourself iOS Development's FlowerViewController, that, under didSelectRowAtIndesPath it goes to a website in a new nib (I tweaked part of the passing data).
MY QUESTION: I would like to update this to, instead of going to a nib, to segue within a storyboard. I know that instead of using didSelectRow... I use prepareForSegue...but I can't figure out the details...
my I have ViewController.m with the following:
- (void)viewDidLoad {
[self movieData];
[super viewDidLoad];
self.title = #"Movies";
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [movieSections count];
}
// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[movieData objectAtIndex:section] count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleSubtitle
reuseIdentifier:CellIdentifier];
}
// Configure the cell.
[[cell textLabel]
setText:[[[movieData
objectAtIndex:indexPath.section]
objectAtIndex: indexPath.row]
objectForKey:#"name"]];
[[cell imageView]
setImage:[UIImage imageNamed:[[[movieData
objectAtIndex:indexPath.section]
objectAtIndex: indexPath.row]
objectForKey:#"picture"]]];
[[cell detailTextLabel]
setText:[[[movieData
objectAtIndex:indexPath.section]
objectAtIndex: indexPath.row]
objectForKey:#"detail"]];
cell.detailTextLabel.numberOfLines = 0;
cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
WebViewController *webViewController =
[[WebViewController alloc] initWithNibName:
#"WebViewController" bundle:nil];
webViewController.detailURL=
[[NSURL alloc] initWithString:
[[[movieData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"url"]];
webViewController.title=
[[[movieData objectAtIndex:indexPath.section] objectAtIndex:
indexPath.row] objectForKey:#"name"];
[self.navigationController pushViewController:
webViewController animated:YES];
}
#pragma mark -
#pragma mark Table view delegate
#pragma mark -
#pragma mark Memory management
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Relinquish ownership any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
// For example: self.myOutlet = nil;
}
- (void)movieData {
NSMutableArray *myMovies;
movieSections=[[NSMutableArray alloc] initWithObjects:
#"Movies",nil];
myMovies=[[NSMutableArray alloc] init];
[myMovies addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Movie1",#"name",
#"1.png",#"picture",
#"http://www.url1.com",#"url",#"Some information",#"detail",nil]];
[myMovies addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Movie2",#"name",
#"2.png",#"picture",
#"http://www.url2.com",#"url",#"Some information 2",#"detail",nil]];
[myMovies addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Movie3",#"name",
#"3.png",#"picture",
#"http://www.url3.com",#"url",#"Some information 3",#"detail",nil]];
[myMovies addObject:[[NSMutableDictionary alloc]
initWithObjectsAndKeys:#"Movie4",#"name",
#"4.png",#"picture",
#"http://www.url4.com",#"url",#"Some information 4",#"detail",nil]];
movieData=[[NSMutableArray alloc] initWithObjects:
myMovies,nil];
}
I attempted to comment out the didSelectRowAtIndexPath and add the following for the segue, but the cell highlights and nothing happens (thankfully it doesn't freeze/crash, but there's nothing positive)
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([[segue identifier] isEqualToString:#"movieSegue"]) {
NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
WebViewSegue *_webViewSegue = [segue destinationViewController];
_webViewSegue.detailURL =
[[NSURL alloc] initWithString:[[[movieData objectAtIndex:selectedRowIndex.section] objectAtIndex:
selectedRowIndex.row] objectForKey:#"url"]];
}
}
Then I want it to pass to WebViewSegue
WebViewSegue.h:
#interface WebViewSegue : UIViewController {
IBOutlet UIWebView *detailWebView;
NSURL *detailURL;
IBOutlet UIActivityIndicatorView *activity;
NSTimer *timer;
}
#property (nonatomic, weak) NSURL *detailURL;
#property (nonatomic, weak) UIWebView *detailWebView;
#property (nonatomic, weak) UIActivityIndicatorView *activity;
#end
WebViewSegue.m:
#synthesize detailWebView =_detailWebView;
#synthesize detailURL = _detailURL;
#synthesize activity =_activity;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
[detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];
timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0)
target:self
selector:#selector(tick)
userInfo:nil
repeats:YES];
}
-(void)tick {
if (!detailWebView.loading)
[activity stopAnimating];
else
[activity startAnimating];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void)wevView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:#"Cannot connect"
message:#"Please check your connection and try again"
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
#end
I've answered your question in another post on the site. See my answer here.
Specifically on how to pass data from a table to the next storyboard segue, first create a property for the data in the next storyboard segue (i.e. the destination view controller). Then set that property in the prepareForSegue method of the table (the source view controller).
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// needed if you have multiple segues
if ([[segue identifier] isEqualToString:#"changeNameAndDate"])
{
[[segue destinationViewController] setDataProperty:self.tableData];
// where dataProperty is the property in the designation view controller
// and tableData is the data your are passing from the source
{
}
This is a lot of code to digest; you should try to simplify. But if I've read it correctly, your basic approach seems correct.
First, put a breakpoint on prepareForSegue:sender:, and make sure it's being called, and that the identifier is what you expect it to be.
Then put a breakpoint on viewDidLoad and make sure it's called when you think it should be.
I would pull the loadRequest: out into its own method and call it both in viewDidLoad and in setDetailURL:. It's likely that setDetailURL: is being called after viewDidLoad if all of this is in a single storyboard.
EDIT What I'm saying is that prepareForSegue:sender: is likely correct. You're problem is in the presented view controller.
- (void)reloadWebView { // Pull the loadRequest: out...
[self.detailWebView loadRequest:[NSURLRequest requestWithURL:self.detailURL]];
}
- (void)viewDidLoad {
[super viewDidLoad];
[self reloadWebView]; // ...and call it both in viewDidLoad...
...
}
- (void)setDetailURL:(NSURL *)URL {
[URL retain];
[detailURL release];
detailURL = URL;
[self reloadWebView]; // ...and in setDetailURL:
}
Also note that there is no reason for your timer. Just turn on your progress indicator in reloadWebView and turn it off in webViewDidFinishLoad and webView:didFailLoadWithError:. Your current approach makes it impossible to deallocate this view controller because the timer retains it forever.