IOS创建标题栏的两种方法

一、基于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的效果一样。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值