UINavigationController 例子一
关于UINavigationController 的原理这里就不介绍了,今天主要是利用一个例子来告诉大家如何使用一个UINavigationController。
本文转自 http://www.999dh.net/article/iphone_ios_art/46.html 转载请注明谢谢!
1.首先建立一个 Emtpy Application 命名为UINavigationController_Demo
图1
2.新建一个CRootViewController,步骤为 File-New-New File-IOS-Cocoa Touch-UIViewController subclass,名字为 CRootViewController sub of class 为 UiViewController ,并选择 with XIB for User Interface
图2
3.同步骤2一样,建立一个 CSecondViewController
4.打开 CRootViewController.xib,拖一个Button到上面
5.在 CRootViewController.h文件中,修改如下,
buttonPressed:函数主要是点击的时候跳转到下一个view的作用
@interface CRootViewController : UIViewController
-(IBAction)buttonPressed:(id)sender;
@end
在CRootViewController.m文件中,导入#import "CSecondViewController.h"
然后实现按钮点击函数
-(IBAction)buttonPressed:(id)sender
{
CSecondViewController * secView = [[CSecondViewController alloc] init];
secView.title = @"The Sec View";
[self.navigationController pushViewController:secView animated:YES];
[secView release];
}
别忘记将buttonPressed: 与button 的 touch up inside进行关联
6.在 XYZAppelegate.h中实现如下:
#import <UIKit/UIKit.h>
@interface XYZAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (retain,nonatomic) UINavigationController * navController;
@end
7.对 XYZAppelegate.m 修改如下
#import "CRootViewController.h"
@implementation XYZAppDelegate
@synthesize window = _window;
@synthesize navController;
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
CRootViewController * rootView =[[CRootViewController alloc] init];
rootView.title = @"The root View";
self.navController = [[UINavigationController alloc] init];
[self.navController pushViewController:rootView animated:NO];
[self.window addSubview:self.navController.view];
[self.window makeKeyAndVisible];
[rootView release];
return YES;
}
保存运行,效果如下
图3
跟一般我们见到的程序相比是不是缺少点什么呢?
是的,那就是在顶部缺少一些按钮,这个名字其实就是 UIBarButtonItem
在CRootViewController.m 文件里面修改如下
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem * leftButton = [[ UIBarButtonItem alloc] initWithTitle:@"AA" style:UIBarButtonItemStyleDone target:self action:@selector(leftPressed:)];
self.navigationItem.leftBarButtonItem = leftButton;
[leftButton release];
}
-(void)leftPressed:(id)sender
{
UIAlertView * alert=[[UIAlertView alloc] initWithTitle:@"aaa" message:@"bbbb" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
具体关于UIBarButtonItem的其他使用方法,大家可以参考文档
相关知识链接 http://blog.csdn.net/duxinfeng2010/article/details/7707054