Xcode4.2 TabbedApplication created by default looks like this. In addition, the class is named XXFirstViewController and XXXSecondViewController, which makes us uncomfortable. In many cases, we need more meaningful class names and more tab options. The following shows how to define these items. Assume that
The Tabbed Application created by default in Xcode 4.2 looks like this. In addition, the class is named XXFirstViewController and XXXSecondViewController, which makes us uncomfortable. In many cases, we need more meaningful class names and more tab options. The following shows how to define these items. Assume that
The Tabbed Application created by default in Xcode 4.2 looks like this.
In addition, the class is named XXFirstViewController and XXXSecondViewController, which makes us uncomfortable. In many cases, we need more meaningful class names and more tab options. The following shows how to define these items.
Suppose we create a project with three options, and the three related viewcontrollers are common, table View, navigation mode with table view, and named FViewController, SViewController, TViewController.
Create a new Tab Application and name the project tTabApp.
The created project directory is as follows:
Delete the preceding six files: tcFirstViewController. h, tcFirstViewController. m, tcSecondeViewController. h, tcSecondViewController. m, tcFirstViewController. xib, and tcSecondViewController. xib.
Right-click tTabApp and add FViewController.
Select UIViewController subclass
The name of the input class is FViewController, and Subclass of: UIViewController must be selected below.
You also need to select With XIB for user interface.
Then, let us assume the other two View controllers. Note that the following two must be in the Subclass of: UITableViewController on the last interface.
The project directory is as follows:
Modify a function in FViewController. m.
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
// Self. title = @ "first ";
Self. tabBarItem. title = @ "first ";
Self. tabBarItem. image = [UIImageimageNamed: @ "first"];
}
Returnself;
}
Modify several functions in SViewController as follows:
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
Self. tabBarItem. title = @ "second ";
Self. tabBarItem. image = [UIImageimageNamed: @ "second"];
}
Returnself;
}
# Pragma mark-Table view data source
-(NSInteger) numberOfpsInTableView :( UITableView *) tableView
{
# Warning Potentially incomplete method implementation.
Delete the above. This line will always generate a warning during compilation.
// Return the number of ps.
Return 1;
Change the value of 0 in the preceding row to 1.
}
-(NSInteger) tableView :( UITableView *) tableView numberofrowsindium :( NSInteger) p
{
# Warning Incomplete method implementation.
// Return the number of rows in the p.
Return 1;
}
-(UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
{
StaticNSString * CellIdentifier = @ "Cell ";
UITableViewCell * cell = [tableViewdequeueReusableCellWithIdentifier: CellIdentifier];
If (cell = nil ){
Cell = [[[UITableViewCellalloc] initWithStyle: UITableViewCellStyleDefaultreuseIdentifier: CellIdentifier] autoreler];
}
Cell. textLabel. text = @" 1111 ";
The above line is added,
// Configure the cell...
Return cell;
}
Make the same changes in TViewController, and note that cell. textLabel. text = @ "1111"; change to cell. textLabel. text = @ "222222 ";. To distinguish.
-(Id) initWithNibName :( NSString *) nibNameOrNil bundle :( NSBundle *) nibBundleOrNil
{
Self = [superinitWithNibName: nibNameOrNil bundle: nibBundleOrNil];
If (self ){
// Custom initialization
Self. title = @ "third ";
Self. tabBarItem. image = [UIImageimageNamed: @ "second"];
}
Returnself;
}
Add import to the tcAppDelegate. m file as follows:
# Import "FViewController. h"
# Import "SViewController. h"
# Import "TViewController. h"
Modify the function in the tcAppDelegate. m file.
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
As follows:
-(BOOL) application :( UIApplication *) application didfinishlaunchingwitexceptions :( NSDictionary *) launchOptions
{
Self. window = [[[uiappswalloc] initWithFrame: [[UIScreenmainScreen] bounds] autorelease];
// Override point for customization after application launch.
UIViewController * viewController1 = [[FViewControlleralloc] initWithNibName: @ "FViewController" bundle: nil];
UIViewController * viewController2 = [[SViewControlleralloc] initWithNibName: @ "SViewController" bundle: nil];
UIViewController * viewController3 = [[TViewControlleralloc] initWithNibName: @ "TViewController" bundle: nil];
UINavigationController * navigationController = [[UINavigationControlleralloc] initWithRootViewController: viewController3];
Self. tabBarController = [[UITabBarControlleralloc] init] autorelease];
Self. tabBarController. viewControllers = [NSArrayarrayWithObjects: viewController1, viewController2, navigationController, nil];
[ViewController1 release];
[ViewController2release];
[ViewController3release];
[NavigationControllerrelease];
Self. window. rootViewController = self. tabBarController;
[Self. windowmakeKeyAndVisible];
ReturnYES;
}
The final effect is.