iOS學習之UINavigationController詳解與使用(二)頁面切換和segmentedController
我們接著講UINavigationController的重要作用,頁面的管理和切換。
1、RootView 跳到SecondView
首先我們需要新一個View。新建SecondView,按住Command鍵然後按N,彈出新建頁面,我們新建SecondView
2、為Button 新增點選事件,實現跳轉
在RootViewController.xib中和RootViewController.h檔案建立連線
在RootViewController.m中實現程式碼,alloc一個SecondViewController,用pushViewController到navigationController中去,併為
SecondViewController這是title為 secondView.title =@"Second View"; 預設情況下,titie為下個頁面返回按鈕的名字。
- - (IBAction)gotoSecondView:(id)sender {
- SecondViewController *secondView = [[SecondViewController alloc] init];
- [self.navigationController pushViewController:secondView animated:YES];
- secondView.title = @"Second View";
- }
這就是SecondView了。
3、新增segmentedController
在nav bar這樣的效果是如何實現的呢?
這就是segmentedController。
3.1在RootViewController.m的viewDidLoad新增如下程式碼:
- NSArray *array = [NSArray arrayWithObjects:@"雞翅",@"排骨", nil];
- UISegmentedControl *segmentedController = [[UISegmentedControl alloc] initWithItems:array];
- segmentedController.segmentedControlStyle = UISegmentedControlSegmentCenter;
- [segmentedController addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
- self.navigationItem.titleView = segmentedController;
3.2[segmentedController addTarget:selfaction:的實現
- -(void)segmentAction:(id)sender
- {
- switch ([sender selectedSegmentIndex]) {
- case 0:
- {
- UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點選了雞翅" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
- [alter show];
- }
- break;
- case 1:
- {
- UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你點選了排骨" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
- [alter show];
- }
- break;
- default:
- break;
- }
- }
4、自定義backBarButtonItem
左上角的返回上級View的barButtonitem的名字是上級目錄的Title,如果title或者適合做button的名字,怎麼辦呢?我們可以自己定義
程式碼如下:(注意:以下程式碼是加在RootViewContoller中的gotoSecondView方法中!不然放在SecondViewChontoller中不會生效。)
- UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"根檢視" style:UIBarButtonItemStyleDone target:nil action:nil];
- self.navigationItem.backBarBu
6、自定義title
UINavigationController的title可以用別view替代,比如用UIButton UILable等,下面我用UIButton.
在SecondViewController.m中新增下面如下。
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
- [button setTitle: @"自定義title" forState: UIControlStateNormal];
- [button sizeToFit];
- self.navigationItem.titleView = button;}
下篇檔案講下Navigation 的Toobar如何顯示和如何自己定義。
相關文章
- 【IOS開發初學者】UINavigationController詳解iOSUINavigationController
- iOS 封裝頁數控制,點選NavigationTabBar切換頁面iOS封裝NavigationtabBar
- 學習和配置頁面轉換模型模型
- linux學習之使用者的切換Linux
- iOS 8 之後UINavigationController新特性iOSUINavigationController
- iOS學習筆記04 檢視切換iOS筆記
- iOS之runtime詳解api(二)iOSAPI
- vue 獲取頁面詳情後 切換頁面時 如何監聽使用者是否修改過資訊Vue
- 使用Bootstrap tab頁切換的使用boot
- WPF手動實現切換頁面
- 切換電腦頁面、視窗快捷鍵
- flutter學習日記(二)————flutter的佈局和頁面元件Flutter元件
- .NET 5學習筆記(10)——Entity Framework Core之切換SQLServer和SQLite筆記FrameworkServerSQLite
- iOS開發-檢視控制器UINavigationController的介紹與基本使用iOSUINavigationController
- 雲端計算學習教程,web頁面更新指令碼詳解Web指令碼
- iOS 切換鍵盤iOS
- 微信小程式頁面功能-----標籤切換微信小程式
- Git 學習之命令詳解Git
- Vue 全家桶仿原生App切換效果和頁面快取實踐VueAPP快取
- iOS開發之APP內部切換語言iOSAPP
- Android頁面跳轉與返回機制詳解Android
- jQuery 圖片垂直切換效果詳解jQuery
- 利用ViewPager和Fragment實現頁卡切換ViewpagerFragment
- 切換頁面主題樣式研究及less教程
- flutter實戰6:TAB頁面切換免重繪Flutter
- [提問交流]切換頁面,左側選單消失
- CSS實現頁面切換時的滑動效果CSS
- Git 分支的建立與切換 —— Git 學習筆記 14Git筆記
- 028、Vue3+TypeScript基礎,使用路由功能實現頁面切換效果VueTypeScript路由
- 手機直播原始碼,使用ViewPager2實現頁面滑動切換原始碼Viewpager
- [iOS]終極橫豎屏切換解決方案iOS
- iOS UIButton之UIButtonType詳解iOSUI
- iOS UIButton之UIEdgeInsets詳解iOSUI
- [iOS]UINavigationController 全屏 pop 之為控制器新增左滑 pushiOSUINavigationController
- 機器學習之Xgboost詳解機器學習
- Linux學習之iostat命令詳解LinuxiOS
- Hadoop 學習系列(二)之 HDFS 詳細解析Hadoop
- Django學習(二) 之 模板的使用Django
- openGauss主備切換之switchover與failoverAI