以上就是导航栏的效果,导航栏在项目中应用很广泛,需要熟练掌握。
新建项目,选择“Empty Application”,项目命名为:NavigationControllerTest
新建一个UIViewController视图,命名为HomeViewConroller
修改AppDeledate.h和AppDolegate.m源代码
思路: 将home"push到”navigationController中,再将navigationController.View 添加到window中
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UINavigationController *navigationController;
}
@property (strong, nonatomic) UIWindow *window;
@end
#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方法。
{
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的代码:
{
[super viewDidLoad];
UIBarButtonItem *leftBarBtn = [[UIBarButtonItem alloc] initWithTitle:@"触摸" style:UIBarButtonItemStyleBordered target:self action:@selector(leftBarBtnClicked:)];
self.navigationItem.leftBarButtonItem = leftBarBtn;
[leftBarBtn release];}
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"左边的BarButton被点击!" delegate:self cancelButtonTitle:@"确认" otherButtonTitles:nil];
[alert show];
[alert release];
}
效果如下图:
下面的和上面的原理一样,还是看代码吧,代码胜于一切华丽的言语。
代码如下:
{
[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];
//导航到另一个视图后,修改返回的按钮的显示文字,默认是当前视图的导航标题,如“备忘录”
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;
} }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库