效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController : UIViewController 4 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 @end 5 6 @implementation ViewController 7 8 - (id)init { 9 if (self = [super init]) { 10 //self.title = @"Hello"; 11 self.navigationItem.title = @"Hello"; 12 } 13 return self; 14 } 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 UILabel* lblMessage = [[UILabel alloc] initWithFrame:self.view.bounds]; 20 lblMessage.text = @"Hello, world!"; 21 lblMessage.textAlignment = NSTextAlignmentCenter; 22 lblMessage.textColor = [UIColor blackColor]; 23 lblMessage.backgroundColor = [UIColor whiteColor]; 24 lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 25 [self.view addSubview:lblMessage]; 26 } 27 28 - (void)didReceiveMemoryWarning { 29 [super didReceiveMemoryWarning]; 30 // Dispose of any resources that can be recreated. 31 } 32 33 @end
ViewController2.h
1 #import <UIKit/UIKit.h> 2 3 @interface ViewController2 : UIViewController 4 @end
ViewController2.m
1 #import "ViewController2.h" 2 3 @interface ViewController2 () 4 @end 5 6 @implementation ViewController2 7 8 - (id)init { 9 if (self = [super init]) { 10 //self.title = @"您好"; 11 self.navigationItem.title = @"您好"; 12 } 13 return self; 14 } 15 16 - (void)viewDidLoad { 17 [super viewDidLoad]; 18 19 UILabel* lblMessage = [[UILabel alloc] initWithFrame:self.view.bounds]; 20 lblMessage.text = @"您好,世界!"; 21 lblMessage.textAlignment = NSTextAlignmentCenter; 22 lblMessage.textColor = [UIColor whiteColor]; 23 lblMessage.backgroundColor = [UIColor blackColor]; 24 lblMessage.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 25 [self.view addSubview:lblMessage]; 26 } 27 28 - (void)didReceiveMemoryWarning { 29 [super didReceiveMemoryWarning]; 30 // Dispose of any resources that can be recreated. 31 } 32 33 @end
TopMenuController.h
1 #import <UIKit/UIKit.h> 2 3 @interface TopMenuController : UITableViewController 4 @property (nonatomic, strong) NSMutableArray* mArrItem; 5 6 @end
TopMenuController.m
1 #import "TopMenuController.h" 2 3 @implementation TopMenuController 4 - (id)init { 5 if (self = [super initWithStyle:UITableViewStylePlain]) { 6 self.title = @"主菜单"; 7 // 初始化显示用的数组 8 _mArrItem = [[NSMutableArray alloc] initWithObjects:@"ViewController", @"ViewController2", nil]; 9 } 10 return self; 11 } 12 13 #pragma mark ----- UITableViewDataSource Methods ----- 14 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { 15 return [_mArrItem count]; 16 } 17 18 - (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 19 UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectZero]; 20 cell.textLabel.text = [_mArrItem objectAtIndex:indexPath.row]; 21 return cell; 22 } 23 24 #pragma mark ----- UITableViewDelegate Methods ----- 25 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 26 Class class = NSClassFromString([_mArrItem objectAtIndex:indexPath.row]); 27 id viewController = [[class alloc] init]; 28 if (viewController) { 29 [self.navigationController pushViewController:viewController animated:YES]; 30 } 31 } 32 33 @end
AppDelegate.h
1 #import <UIKit/UIKit.h> 2 3 @interface AppDelegate : UIResponder <UIApplicationDelegate> 4 @property (strong, nonatomic) UIWindow *window; 5 @property (strong, nonatomic) UINavigationController *navigationController; 6 7 @end
AppDelegate.m
1 #import "AppDelegate.h" 2 #import "TopMenuController.h" 3 4 @interface AppDelegate () 5 @end 6 7 @implementation AppDelegate 8 9 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 10 TopMenuController *topMenuController = [[TopMenuController alloc] init]; 11 _navigationController = [[UINavigationController alloc] initWithRootViewController:topMenuController]; 12 13 _window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 14 _window.rootViewController = topMenuController; 15 [_window addSubview:_navigationController.view]; 16 [_window makeKeyAndVisible]; 17 return YES; 18 } 19 20 - (void)applicationWillResignActive:(UIApplication *)application { 21 } 22 23 - (void)applicationDidEnterBackground:(UIApplication *)application { 24 } 25 26 - (void)applicationWillEnterForeground:(UIApplication *)application { 27 } 28 29 - (void)applicationDidBecomeActive:(UIApplication *)application { 30 } 31 32 - (void)applicationWillTerminate:(UIApplication *)application { 33 } 34 35 @end
时间: 2024-10-07 14:22:00