Gavin.han

致力于移动开发 技术改变生活
随笔 - 133, 文章 - 0, 评论 - 46, 阅读 - 42万

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

ios开发 NavigationController的使用

Posted on   gavin.han  阅读(13430)  评论(2编辑  收藏  举报

以上就是导航栏的效果,导航栏在项目中应用很广泛,需要熟练掌握。

 

新建项目,选择“Empty Application”,项目命名为:NavigationControllerTest

  新建一个UIViewController视图,命名为HomeViewConroller

修改AppDeledate.h和AppDolegate.m源代码

思路: 将home"push到”navigationController中,再将navigationController.View 添加到window中


复制代码
//  AppDelegate.m 

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
    UINavigationController *navigationController;
}

@property (strong, nonatomic) UIWindow *window;

@end 

复制代码

 

复制代码
//  AppDelegate.m
#import "AppDelegate.h"
#import "HomeViewController.h"

@implementation AppDelegate

@synthesize window = _window;



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    navigationController = [[UINavigationController alloc] init];
    HomeViewController *home = [[HomeViewController alloc] init];
    
    home.title = @"备忘录";
    
    [navigationController pushViewController:home animated:NO];
    [self.window addSubview:navigationController.view];
    [home release];
    
    [self.window makeKeyAndVisible];
    return YES;
}

- (void)dealloc
{
    [navigationController release];
    [_window release];
    [super dealloc];
}


@end
复制代码

效果如下图:

 

上面的只是一个页面,下面新建一个UIViewController视图“SecondViewController”,使项目在两个页面间切换。 

        在 HomeViewController.xib上添加button “进入第二个视图”

 HomeViewController中添加 - (IBAction)displaySecondView:(id)sender方法。


复制代码
 - (void)displaySecondView:(id)sender

{
    SecondViewController *secondViewConroller = [[SecondViewController alloc] init];
    //向视图询问它的导航控制器,因为在AppDelegate.m中我们已经在navigationController中添加了home,
    
//所以这里我们询问home的导航控制器就会返回先前navigationController指针,如果没有就返回空
    [self.navigationController pushViewController:secondViewConroller animated:YES];
    secondViewConroller.title = @"第二个视图";
    [secondViewConroller release];
}

复制代码

效果如下:

 

 当切换到SencondViewController,导航栏自动显示返回第一个视图的按钮。

 

在导航栏上实现左右两个按钮。

这个任务主要由UINavigationController上面的UINavigationItem来实现。
在 UINavigationItem上添加UIBarButtonItem。

 在HomeViewController.m中修改- (void)viewDidLoad的代码:

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"触摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
  self.navigationItem.leftBarButtonItem = leftBarBtn;   
 [leftBarBtn release];}
复制代码

 

- (void)leftBarBtnClicked:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左边的BarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

 

  效果如下图:

 

 

还有一些按钮按钮和行为,系统已经帮我们定义好了,Edit和Done按钮行为,我们只要实现它就好了 。

 

下面的和上面的原理一样,还是看代码吧,代码胜于一切华丽的言语。 

 代码如下:

 

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"触摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
    self.navigationItem.leftBarButtonItem = leftBarBtn;    
       [leftBarBtn release];
    
    UIBarButtonItem *addBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addBarBtnClicked:)];
    self.navigationItem.rightBarButtonItem = addBarBtn; 
    [addBarBtn release];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    NSArray *segmentButtons = [NSArray arrayWithObjects:@"升序"@"降序", nil];
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentButtons];
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView = segmentedControl;
    [segmentedControl release];
    //导航到另一个视图后,修改返回的按钮的显示文字,默认是当前视图的导航标题,如“备忘录”
复制代码
复制代码
    UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:nil action:nil];
    self.navigationItem.backBarButtonItem = backBarBtn;
    [backBarBtn release];
                                   
}

- (void)leftBarBtnClicked:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左边的BarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)addBarBtnClicked:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"右边的addBarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
    [alert show];
    [alert release];
}

- (void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    if (editing)
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"edit" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"done" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
}

- (void)segmentAction:(id)sender
{
    switch ([sender selectedSegmentIndex])
    {
        case 0:
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"升序" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alert show];
            [alert release];
            break;
        }
        case 1:
        {
            UIAlertView *alert1 = [[UIAlertView alloc] initWithTitle:@"提示" message:@"降序" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
            [alert1 show];
            [alert1 release];
            break;
        }
        default:
            break;
    } }
复制代码

 

 

 

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示