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

转载于:https://www.cnblogs.com/rollrock/archive/2013/01/04/2843760.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值