Posts tagged navigation controller
Get to the point: Tab Bar Application – Adding an UITableView to a Navigation Controller
Feb 7th
Posted by admin in iPhone Development
Get to the point: Tab Bar Application – Adding an UITableView to a Navigation Controller
1- Open the previous tutorial sources (Tab Bar Application – Navigation Controller Embedded).
2- Edit the NavigationContentsViewController.h file. Change the source code so it looks like this:
#import <UIKit/UIKit.h>
@interface NavigationContentsViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
NSMutableArray *itemsList;
UITableView *myTableView;
}
@property(nonatomic,retain)NSMutableArray *itemsList;
@property(nonatomic,retain)UITableView *myTableView;
@end
Notice the <UITableViewDelegate, UITableViewDataSource> protocols we’ve added.
3- Edit the NavigationContnensViewController.m file and add the following code after the @implementation line:
@synthesize itemsList; @synthesize myTableView;
4- Add the TableView functions to the NavigationContnensViewController.m file.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [itemsList count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}
// Set up the cell...
NSString *cellValue = [itemsList objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedCellItem = [NSString stringWithFormat:@"%d", indexPath.row];
TableViewController *fvController = [[TableViewController alloc] initWithNibName:@"TableViewController" bundle:[NSBundle mainBundle]];
fvController.selectedCellItem = selectedCellItem;
[self.navigationController pushViewController:fvController animated:YES];
[fvController release];
fvController = nil;
}
5- Add these lines of code below the previous added code.
-(void)loadView{
myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
myTableView.delegate = self;
myTableView.dataSource = self;
myTableView.autoresizesSubviews = YES;
itemsList = [[NSMutableArray alloc] init];
[itemsList addObject:@"Item1"];
[itemsList addObject:@"Item2"];
self.navigationItem.title = @"Test List";
self.view = myTableView;
}
6- Remember to release the itemsList array. Do so by adding [itemsList release]; to the dealloc function.
7- We will need a View Controller so go ahead and create it (File->New File ->Cocoa Touch Class->UIViewController subclass) and name it TableViewController (Figure 1). Make sure the “With XIB for user interface” option is marked.
8- Open the TableViewController.h file and change the source code so it looks like this:
#import <UIKit/UIKit.h>
@interface TableViewController : UIViewController {
NSString *selectedCellItem;
}
@property (nonatomic, retain) NSString *selectedCellItem;
@end
9- Now open the TableViewController.m file and add this line of code below the @implementation:
@synthesize selectedCellItem;</pre> 10- Uncomment the viewDidLoad function and add these lines of code: [sourcecode language="objc"]self.navigationItem.title = @"Selected Item"; CGRect frame = CGRectMake(0, 0, 320, 180); self.view = [[UIView alloc] initWithFrame:frame]; self.view.backgroundColor = [UIColor whiteColor]; frame = CGRectMake(100, 20, 100, 50); UILabel *label = [[UILabel alloc] initWithFrame:frame]; label.text = selectedCellItem; [self.view addSubview:label]; [label release]; [super viewDidLoad];
11- Go back to the NavigationContentsViewController.m file and add this code below the #import “NavigationContentsViewController.h”:
#import "TableViewController.h"
12- That’s it, if you have followed all the previous steps, your project should compile & run without errors.
Get to the point – iPhone Development: Embed a Navigation controller to a Tab Bar Application
Jan 31st
Posted by admin in iPhone Development
Get to the point – iPhone Development: Embed a Navigation controller to a Tab Bar Application
This tutorial is a continuation of the previous Tab Bar Application tutorial.
1- Open the tabbartest Xcode project. Create a new UIViewController subclass (File->New File) and name it TabBarNavigationController (see Figure 1). Make sure the “With XIB for user interface” option is marked. Three new files will be added to your Xcode project: TabBarNavigationController.h, TabBarNavigationController.m and TabBarNavigationController.xib.
2- Open the TabBarNavigationController.h file and replace the text UIViewController with UINavigationController.
3- We need to add a view controller to display contents. Create a new UIViewController subclass named NavigationContentsViewController (File->New File->UIViewController subclass).
4- Create a new view to add to the newly created UIViewController (File->New file->View XIB). This is not a Cocoa Touch Class file, be sure to select User Interface. Name the view FirstNavigationView. Open the file FirstNavigationView.xib and select File’s Owner. In the Identity tab of the Inspector, change the class to NavigationContentsViewController. Go to the “View Connection” tab, click on the view outlet, then drag & release it on the view canvas.
5- Open the MainWindow.xib file and add a new Tab Bar Item. Be sure the new Tab Bar Item is selected. Open the Inspector window (Tools->Inspector if it’s not already opened). On the Identity tab change the selected Class to TabBarNavigationController (see Figure 2).
6- Select the Tab Bar Controller (see Figure 3). Select the attributes tab from the Inspector Window. Select the second item in the View Controllers list. Choose the Navigation Controller class (see Figure 4).
7- Expand the “Tab Bar Controller” and expand the “Selected Navigation Controller (Item)”. Select the “View Controller (Item)” and change the class to NavigationContentsViewController (see Figure 5). You’ll see the name has changed to “Navigation Contents View Controller (Item)”. Click on the Attributes tab and set the NIB Name to FirstNavigationView in the Inspector window.
8- Save all files, click on the “Build and Run” button from Xcode, your app will be executed (see Figure 6).







