How to use UITableView in Multi View application - ios

i have some other viewcontroller which triggers another viewcontroller which have UItableView as subView my code is following two file .h , .m
when i use single view based application with this code it works fine but when i use two view for examlpe 1st is logingscreen n call 2nd nib file using this
Viewcomment *b=[[SimpleTableViewController alloc] initWithNibName:#"SimpleTableViewController" bundle:nil];
[self presentModalViewController:b animated:YES];
or
self.window= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[Viewcomment alloc] initWithNibName:#"Viewcomment" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
it shows "SIGABRT" !!!
Where i am wrong ?
.h file:
#import <UIKit/UIKit.h>
#interface SimpleTableViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>{
NSArray *listData;
}
#property(nonatomic, retain) NSArray *listData;
#end
.m file
#import "SimpleTableViewController.h"
#implementation SimpleTableViewController
#synthesize listData;
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:#"iPhone", #"iPod", #"iPad",nil];
self.listData = array;
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)viewDidUnload {
}
- (void)dealloc {
}
#pragma mark -
#pragma mark Table View Data Source Methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.listData count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *SimpleTableIdentifier = #"SimpleTableIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SimpleTableIdentifier];
if (cell == nil) { cell = [[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SimpleTableIdentifier] ;
}
NSUInteger row = [indexPath row]; cell.textLabel.text = [listData objectAtIndex:row]; return cell;
}
#end

Your missing:
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
from your UITableViewDataSource which will cause a crash.
If thats doesn't work make sure the view is connecting in the nib file of your view controller and make sure your UITableView is connected correctly to the data source.

Related

select a viewcontroller programatically at didiSelectRowAtIndexpath in ios

I have a tableview with dynamic prototypes content. I want to call for a specific ViewController programatically when a dynamic cell tap. I used SWRevealViewController calls for my project.
When I tap on cell one with #"l1" identifier I want to load main view controller with sw_front , used in SWRevealViewController. I cannot use controll+drag becaue in her I have use tabviewcontrller. When I do that it disappear the tab bar.
#import "ListViewController.h"
#interface ListViewController ()
#end
#implementation ListViewController
{
NSArray *menuItems;
}
- (void)viewDidLoad {
[super viewDidLoad];
menuItems = [NSArray arrayWithObjects:#"l1",#"l2",#"l3",#"l6",#"l4",#"l5",#"l7", nil];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [menuItems count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = [menuItems objectAtIndex:indexPath.row];
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
if ([cell.reuseIdentifier isEqualToString:#""]) {
UIViewController *mainviewcontroller = [[UIViewController alloc] initWithNibName:#"MainViewController" bundle:nil];
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:mainviewcontroller];
[self.navigationController presentViewController:navi animated:YES completion:nil];
}
}
If you already set storyboard ID then you can push programmatically using following code.
Here viewController1, viewController2 etc are storyboard Id of individual UIViewController.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *pushViewController = [self.storyboard instantiateViewControllerWithIdentifier:[NSString stringWithFormat:#"viewController%ld",indexPath.row]];
[self.navigationController pushViewController:pushViewController animated:YES];
}

UITableView from separate class

Case is following: I want to create UITableView from separate class.
Currently I have following:
// Menu.h
#interface Menu : UITableViewController <UITableViewDelegate, UIAlertViewDelegate> {
UITableView *tableView;
}
#property (nonatomic,retain) NSMutableArray *navigationItems;
- (void)initMenu:(UIView *)view;
#end;
Then
// Menu.m
#import <Foundation/Foundation.h>
#import "Menu.h"
#implementation Menu
- (void) initMenu:(UIView *)view {
self.navigationItems = [[NSMutableArray alloc] initWithObjects:#"One",#"Two",#"Three",#"Four",#"Five",#"Six",#"Seven",#"Eight",#"Nine",#"Ten",nil];
UIView *mainmenu=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 420)];
[mainmenu setBackgroundColor:[UIColor yellowColor]];
[view addSubview:mainmenu];
UITableView *menutableView = [[UITableView alloc] initWithFrame:view.bounds style:UITableViewStylePlain];
menutableView.backgroundColor = [UIColor whiteColor];
menutableView.delegate = self;
menutableView.dataSource = self;
[mainmenu addSubview:menutableView];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableViewi cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableViewi dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.navigationItems objectAtIndex:indexPath.row];
return cell;
}
#end
And in different .m file I call method:
...
#import "Menu.h"
...
- (void)viewDidLoad
{
[super viewDidLoad];
...
Menu *menu = [[Menu alloc] init];
[menu initMenu: self.view];
}
Running this will crash application and Xcode won't give any detailed report. However, if I combine Menu.m to the .m file where I'm calling "initMenu" it won't crash.
Also if I comment out menutableView.dataSource = self; it will run with our crash (no rows in table of course...).
Passing a view into an init method and then creating/adding subviews in said init method is an odd design pattern. Change your Menu class to a viewcontroller, then move your UIView and UITableView declarations to the viewDidLoad method. The crash is likely happening because the tableview is trying to display data before its parent view has finished loading.

Switching UIViewControllers in iOS not working

I am trying to show a UIViewController on top of other UIViewcontroller using addChildViewController functionality.The childViewController is a tableView which shows up on top of my MainViewController however I do not see the table view that it has.If I execute the childViewController separately , the tableView works fine , so what am I missing here.
Here's how I am adding a childVC:
#implementation Test2ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (IBAction)showChildVC:(id)sender
{
TestTableViewController *tVC = [[TestTableViewController alloc]init];
tVC.view.frame = CGRectMake(50, 50, 200, 200);
[self addChildViewController:tVC];
[self.view addSubview:tVC.view];
[tVC didMoveToParentViewController:self];
}
And this is the childVC that I want to show: .h
#import <UIKit/UIKit.h>
#interface TestTableViewController : UIViewController<UITableViewDataSource>
{
NSArray *array;
}
#property (weak, nonatomic) IBOutlet UITableView *tableView;
#end
And: .m
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
array = [NSArray arrayWithObjects:#"One",#"Two",#"Three",#"Four", nil];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = [array objectAtIndex:indexPath.row];
return cell;
}
I see your table view in the second view controller is an IBOutlet, so you are placing it in Storyboard.
Then when you instantiate it, you can't do: [[TestTableViewController alloc]init]; you have to do:
[storyBoard instantiateViewControllerWithIdentifier:#"tVCStoryBoardID"];

Adding an Array to a UITableView, using a UINavigationController on a Tab Bar based application

I have made simple cocoa touch apps before but I have never used UINavigationControllers, any advice would be greatly appreciated.
I'm trying to add an array of a list of store names to a UITableView. The UITableView is accessed through a UINavigation controller by a tab on a tab bar.
I have a TabBarController.xib file that holds the tab bar.
I also have a AtoZNavigationController.xib that holds the UINavigationController.
And I have a AtoZTableController.xib file that holds the UITableView.
This is my AppDelegate.h:
#import <UIKit/UIKit.h>
#class AtoZNavigationController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) IBOutlet UITabBarController *rootController;
#property (strong, nonatomic) IBOutlet AtoZNavigationController *navController;
#end
The AppDelegate.m
#import "AppDelegate.h"
#import "AtoZNavigationController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize rootController;
#synthesize navController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
[[NSBundle mainBundle] loadNibNamed:#"TabBarController" owner:self options:nil];
[self.window addSubview:rootController.view];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
#end
The AtoZNavigationController.h
#import <UIKit/UIKit.h>
#interface AtoZNavigationController : UINavigationController
#end
The AtoZNavigationController.m
#import "AtoZNavigationController.h"
#interface AtoZNavigationController ()
#end
#implementation AtoZNavigationController
-(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.
}
-(void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
The AtoZTableController.h
#import <UIKit/UIKit.h>
#interface AtoZTableController : UITableViewController <UITableViewDelegate, UITableViewDataSource>
{
IBOutlet UITableView *AtoZTableView;
NSMutableArray *AtoZArray;
}
#property (nonatomic, retain) IBOutlet UITableView *AtoZTableView;
#end
The AtoZTableController.h
#import "AtoZTableController.h"
#interface AtoZTableController ()
#end
#implementation AtoZTableController
#synthesize AtoZTableView;
-(id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void)viewDidLoad
{
[super viewDidLoad];
self.title = NSLocalizedString(#"A to Z", #"An A to Z List of Stores");
AtoZArray = [[NSMutableArray alloc] init];
[AtoZArray addObject:#"Apple"];
[AtoZArray addObject:#"Boots"];
[AtoZArray addObject:#"Topman"];
}
-(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
{
// Return the number of sections.
return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
// Return the number of rows in the section.
return [AtoZArray count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [AtoZArray objectAtIndex:row];
return cell;
}
#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
In your AtoZTableController.h, you have a problem.
The problem is in your 'tableView:cellForRowAtIndexPath:' method.
Here's what you have:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [AtoZArray objectAtIndex:row];
return cell;
}
The problem is that you never handle for a return value of nil from dequeueReusableCellWithIdentifier:CellIdentifier.
Try this out:
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
// What happens if you don't get a cell to use?
// This is the way to create a new, default UITableViewCell
if (!cell) {
// You can look at the UITableViewCell class reference to see the 4 available styles
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSInteger row = [indexPath row];
cell.textLabel.text = [AtoZArray objectAtIndex:row];
return cell;
}
Edit/Update:
OK, so it's a little bit difficult to know exactly where your error is, so I'll set up/describe for you a typical situation (or how I'd do it in your shoes).
If you create a new app and select the "Tabbed Application" template in Xcode, you get the following method in your app delegate (more or less; I condensed it a little bit and "fixed" Apple's poor choice to use dot notation):
Note: I believe the problem you're having with pushing a new view controller will be fixed below now...End Note
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self setWindow:[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]];
// Override point for customization after application launch.
UIViewController *vc1 = [[FirstViewController alloc] initWithNibName:#"FirstVC" bundle:nil];
// New line...
UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:vc1];
UIViewController *vc2 = [[SecondViewController alloc] initWithNibName:#"SecondVC" bundle:nil];
[[self setTabBarController:[[UITabBarController alloc] init]];
// Change here, too...
[[self tabBarController] setViewControllers:[NSArray arrayWithObjects:navC, vc2, nil]];
[[self window] setRootViewController:[self tabBarController]];
[[self window] makeKeyAndVisible];
return YES;
}
This method sets up all you need to launch your app with 2 UIViewControllers created and set as tab 1 and tab 2 inside of a UITabBarController.
Now, you can make FirstViewController and SecondViewController be whatever you want. For purposes of this question, we'll assume that you want to alter FirstViewController to host a UITableView, which will push a detail UIViewController when a user selects a row on the screen.
Requirements
EITHER FirstViewController must be a subclass of UITableViewController (this is not what the default template provides) OR you must add a UITableView onto FirstViewController's view and set up all of the connections.
Let's assume you're going to keep FirstViewController as a standard UIViewController subclass and that you'll add a UITableView onto its view. (I'd probably change it to a UITableViewController subclass, but that might be more confusing at this point.)
First, in FirstViewController.h, change this:
#interface MMFirstViewController : UIViewController
#end
to this:
#interface MMFirstViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
UITableView *TableView;
}
#property (strong, nonatomic) IBOutlet UITableView *TableView;
#end
Next, in FirstViewController.m, synthesize the TableView property (#synthesize TableView).
Next, click on FirstViewController.xib in Xcode to have it load up in Interface Builder (I'm assuming here that you're using Xcode 4).
Now, drag a UITableView from the controls panel onto your UIViewController's view.
Make the following connections in Interface Builder:
Right click on File's Owner and connect the TableView property to the UITableView you dropped on the view of FirstViewController.
Right click on the UITableView and connect BOTH the datasource AND delegate properties to File's Owner.
Now, the code you posted initializing and populating AtoZArray should work fine. Don't forget to copy in the 3 UITableView methods you previously had, numberOfSectionsInTableView:, tableView:numberOfRowsInSection: and tableView:cellForRowAtIndexPath:.
Those steps should get you working and should also let you see where you perhaps went wrong in your setup. Please note, you'll still have to figure out tableView:didSelectRowAtIndexPath: on your own in order to push in your new UIViewController.
Here's a teaser to get you started:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
ThirdViewController *detailVC = [[ThirdViewController alloc] initWithNibName:#"ThirdViewController" bundle:nil];
[[self navigationController] pushViewController:detailVC animated:YES];
}
I had to create an instance of the navigationController and pass that into the subview in my appDelegate.m along with the tabBarController. It can then later be referenced in my UITableViewController.
Here's the code I added:
navigationController = [[UINavigationController alloc] initWithRootViewController:_tabBarController];
[self.window addSubview:_tabBarController.view];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
Where navigationController is simply an instance of a subclass of either UITableViewController or UIViewController, depending on what type of screen you want to display.

Data not loading in Detail View

I'm trying to get my data to load in my detail view. Can any one take a look and see why it isnt showing? It loads fine in my rootviewcontroller, just not the detail view.
DetailViewController.m
#import "DetailViewController.h"
#implementation DetailViewController
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)setIcon:(UIImage *)newIcon
{
[super setIcon:newIcon];
iconView.image = newIcon;
}
- (void)setPublisher:(NSString *)newPublisher
{
[super setPublisher:newPublisher];
publisherLabel.text = newPublisher;
}
- (void)setName:(NSString *)newName
{
[super setName:newName];
nameLabel.text = newName;
}
- (void)dealloc
{
[iconView release];
[publisherLabel release];
[nameLabel release];
[priceLabel release];
[super dealloc];
}
#end
detailviewcontroller.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#interface DetailViewController : UIViewController {
IBOutlet UIImageView *iconView;
IBOutlet UILabel *publisherLabel;
IBOutlet UILabel *nameLabel;
IBOutlet UILabel *priceLabel;
}
#end
RootViewControllerPoints.m
#import "RootViewControllerPoints.h"
#import "DetailViewController.h"
#define USE_INDIVIDUAL_SUBVIEWS_CELL 1
#define DARK_BACKGROUND [UIColor colorWithRed:151.0/255.0 green:152.0/255.0 blue:155.0/255.0 alpha:1.0]
#define LIGHT_BACKGROUND [UIColor colorWithRed:172.0/255.0 green:173.0/255.0 blue:175.0/255.0 alpha:1.0]
#implementation RootViewController
#synthesize tmpCell, data;
#pragma mark View controller methods
- (void)viewDidLoad
{
[super viewDidLoad];
// Configure the table view.
self.tableView.rowHeight = 73.0;
self.tableView.backgroundColor = DARK_BACKGROUND;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
// Load the data.
NSString *dataPath = [[NSBundle mainBundle] pathForResource:#"Data" ofType:#"plist"];
self.data = [NSArray arrayWithContentsOfFile:dataPath];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
switch (toInterfaceOrientation) {
case UIInterfaceOrientationPortrait:
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
return YES;
default:
return NO;
}
}
#pragma mark Table view methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [data count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"ApplicationCell";
ApplicationCell *cell = (ApplicationCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
#if USE_INDIVIDUAL_SUBVIEWS_CELL
[[NSBundle mainBundle] loadNibNamed:#"IndividualSubviewsBasedApplicationCell" owner:self options:nil];
cell = tmpCell;
self.tmpCell = nil;
#endif
}
// Display dark and light background in alternate rows -- see tableView:willDisplayCell:forRowAtIndexPath:.
cell.useDarkBackground = (indexPath.row % 2 == 0);
// Configure the data for the cell.
NSDictionary *dataItem = [data objectAtIndex:indexPath.row];
cell.icon = [UIImage imageNamed:[dataItem objectForKey:#"Icon"]];
cell.publisher = [dataItem objectForKey:#"Publisher"];
cell.name = [dataItem objectForKey:#"Name"];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailViewController. = [data objectAtIndex:indexPath.row];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController release];
}
#end
This has been bugging me for quite a while, I've looked at numerous examples, tutorials and even asked other iphone devs. Everything source seems to say something different.
First problem is that the setXXX methods in DetailViewController try to make calls to super setXXX but since DetailViewController is a subclass of UIViewController those calls to super will fail because UIViewController doesn't have such methods. Remove the calls to super in the setXXX methods.
Second problem is that the setXXX methods are setting the controls on the DetailViewController directly but the controls won't be accessible until the view is loaded so it won't work if the methods are called before the pushViewController call.
If you change the code in didSelectRowAtIndexPath as follows it should work:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
[self.navigationController pushViewController:detailViewController animated:YES];
[detailViewController setName:#"name here"];
[detailViewController setPublisher:#"publisher here"];
[detailViewController setIcon:yourImageVariableHere];
[detailViewController release];
}
Although the above change should work, you might want to consider creating ivars to hold the values in DetailViewController (instead of using the ui controls themselves to hold the data). Then create properties for them using #property and #synthesize. The properties can be set immediately after DetailViewController is created and in the viewDidLoad of the view, you can set the ui controls to the property values. This will give the DetailViewController more control over how its ui is updated, allow you to change the ui without affecting callers, and it doesn't need to be displayed to set its properties.

Resources