一、基于IB实现
首先建立一个空工程。然后新建一个带XIB的TitleViewController的头文件和实现文件。再新建一个mainwindow.xib。
在AppDelegate.h中
#import <UIKit/UIKit.h>
@class TitleViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (retain, nonatomic) IBOutlet UIWindow *window;
@property (retain, nonatomic) IBOutlet TitleViewController *viewController;
@end
在AppDelegate.m中
#import "AppDelegate.h"
#import "TitleViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController =self.viewController;
[self.windowmakeKeyAndVisible];
return YES;
}
@end
在TitleViewController.h中
#import <UIKit/UIKit.h>
@interface TitleViewController : UIViewController
-(void) showDialog:(NSString*) str;
@end
在TitleViewController.m中
- (void)viewDidLoad
{
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
UINavigationBar *navigationBar = [[UINavigationBaralloc]initWithFrame:CGRectMake(0,0, 320,44)];
UINavigationItem *navigationItem = [[UINavigationItemalloc]initWithTitle:nil];
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithTitle:@"left"style:UIBarButtonItemStyleBorderedtarget:selfaction:@selector(left)];
UIBarButtonItem *rightButton = [[UIBarButtonItemalloc]initWithTitle:@"right"style:UIBarButtonItemStyleDone target:self action:@selector(right)];
[navigationItem setTitle:@"hello"];
[navigationBar pushNavigationItem:navigationItemanimated:NO];
[navigationItem setLeftBarButtonItem:leftButton];
[navigationItem setRightBarButtonItem:rightButton];
[self.view addSubview:navigationBar];
}
-(void) left
{
[selfshowDialog:@"点击了导航栏左边按钮"];
}
-(void) right
{
[selfshowDialog:@"点击了导航栏右边按钮"];
}
-(void) showDialog:(NSString *)str
{
UIAlertView * alert= [[UIAlertViewalloc] initWithTitle:@"这是一个对话框" message:strdelegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil];
[alert show];
}
然后根据需要在mainwindow.xib中添加控件,并且建立之间的连接。
二、基于代码实现
首先建立一个空工程,然后新建一个ViewController类但是不要建立xib文件。就是现在工程里面一个xib文件也没有。
在AppDelegate.h中
#import <UIKit/UIKit.h>
@class TitleViewController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) TitleViewController *viewController;
@end
在AppDelegate.m中
#import "AppDelegate.h"
#import "TitleViewController.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[TitleViewController alloc]init];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
在ViewController.h中#import <UIKit/UIKit.h>
@interface TitleViewController : UIViewController
-(void) showDialog:(NSString*) str;
@end
在ViewController.m中- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"left" style:UIBarButtonItemStyleBordered target:self action:@selector(left)];
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"right" style:UIBarButtonItemStyleDone target:self action:@selector(right)];
[navigationItem setTitle:@"welcome"];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[navigationItem setLeftBarButtonItem:leftButton];
[navigationItem setRightBarButtonItem:rightButton];
[self.view addSubview:navigationBar];
}
-(void) left
{
[self showDialog:@"点击了导航栏左边按钮"];
}
-(void) right
{
[self showDialog:@"点击了导航栏右边按钮"];
}
-(void) showDialog:(NSString *)str
{
UIAlertView * alert= [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
[alert show];
}
这样就可以直接编译,运行了。效果和上面使用IB的效果一样。