Get to the point: Tab Bar Application – Adding an UITableView to a Navigation Controller
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.
No trackbacks yet.
Get to the point: Detect Internet Connection
about 6 months ago - No comments
We will be using Apple’s reachability class to detect the state of the Internet connection. Reachability is freely available from Apple’s website: http://developer.apple.com/iphone/library/samplecode/Reachability/
1- Create a View-based Application and name it InternetConnectionState
2- Download Apple’s Reachability App source code and copy reachability.h & reachability.m files to your application’s Classes folder.
3- Add these files to your XCode project. [...]
Get to the point – Code Snippets: Dialing a number, sending SMS & Email from within your app
about 7 months ago - No comments
These code snippets will allow you to dial a number, send an SMS & send an e-mail from within your application. They will all open up the selected application and close yours.
Dialing a number
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://123456"]];
Sending an SMS
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"sms:123456"]];
Sending an E-mail
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:amasso@amasso.info?subject=Hello&body=Testing your code snippet."]];
Get to the point – Code Snippets: NSString to NSInteger & vice versa
about 7 months ago - No comments
NSString to NSInteger code snippet:
NSString *strTest = [NSString stringWithFormat:@"%d", 10];
NSInteger to NSString code snippet:
NSInteger iTest = [strTest integerValue];
Get to the point – iPhone Development: Embed a Navigation controller to a Tab Bar Application
about 7 months ago - 1 comment
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 [...]
Get to the point – iPhone Development: Tab Bar Application
about 7 months ago - 2 comments
Get to the point, iPhone development tutorials for those programmers who want concise step by step tutorials, but do not have the time to watch a 30 min video tutorial.
Get to the point – iPhone Development: Tab Bar Application (Adding new views)
1- Open Xcode and create a new “Tab Bar Application” (see Figure 1). Name [...]


about 4 months ago
Hi Antoni!
Thanks a lot! This is exactly what I needed.
Great tut.
Best Regards,
Adam
about 3 months ago
great tut! really liked it!
could you direct me how to change the item links in the tableview so when i click item1 for example i open a webview with a particular url? with the option to go back?
thanks again this is awesome!
about 1 month ago
Very Very Helpful to me.
Thanks