UI----4
UI----4一、分栏控制器UITabBarController1. 作用管理多个平级界面底部显示标签栏点击切换不同页面类似微信底部首页、通讯录、我。2. 核心特点是容器控制器不自己显示内容只管理子控制器底部tabBar高度固定系统自动管理子控制器一般是UINavigationController带导航栏3.代码创建// 1. 创建分栏控制器UITabBarController*tabVC[[UITabBarController alloc]init];// 2. 创建多个子界面FirstViewController*firstVC[[FirstViewController alloc]init];SecondViewController*secondVC[[SecondViewController alloc]init];// 3. 给子界面设置 tab 样式标题图标firstVC.tabBarItem.title首页;firstVC.tabBarItem.image[UIImage imageNamed:home];secondVC.tabBarItem.title我的;secondVC.tabBarItem.image[UIImage imageNamed:mine];// 4. 包装导航控制器推荐UINavigationController*nav1[[UINavigationController alloc]initWithRootViewController:firstVC];UINavigationController*nav2[[UINavigationController alloc]initWithRootViewController:secondVC];// 5. 添加到分栏控制器tabVC.viewControllers[nav1,nav2];// 6. 设置为窗口根控制器self.window.rootViewControllertabVC;4.代码实例SceneDelegate//.h#importUIKit/UIKit.hinterfaceSceneDelegate:UIResponderUIWindowSceneDelegate,UITabBarControllerDelegateproperty(strong,nonatomic)UIWindow*window;end//.m#importSceneDelegate.h#importVCF.h#importVCS.h#importVCT.h#importVCFo.h#importVCFi.h#importVCSi.hinterfaceSceneDelegate()endimplementationSceneDelegate-(void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)connectionOptions{VCF*vc1[[VCF alloc]init];vc1.titleFirst;VCS*vc2[[VCS alloc]init];vc2.titlesecond;VCT*vc3[[VCT alloc]init];vc3.titlethird;VCFo*vc4[[VCFo alloc]init];vc4.title4;VCFi*vc5[[VCFi alloc]init];vc5.title5;VCSi*vc6[[VCSi alloc]init];vc6.title6;vc2.view.backgroundColor[UIColor blueColor];vc3.view.backgroundColor[UIColor redColor];UITabBarController*tbc[[UITabBarController alloc]init];NSArray*arrvc[NSArray arrayWithObjects:vc1,vc2,vc3,vc4,vc5,vc6,nil];tbc.viewControllersarrvc;self.window.rootViewControllertbc;tbc.selectedIndex2;if(tbc.selectedViewControllervc3){NSLog(2的视图控制器);}tbc.tabBar.translucentNO;//透明tbc.tabBar.backgroundColor[UIColor colorWithRed:0green:0blue:0alpha:0.5];tbc.delegateself;}-(void)tabBarController:(UITabBarController*)tabBarController willBeginCustomizingViewControllers:(NSArray__kindof UIViewController**)viewControllers{NSLog(编辑前);}-(void)tabBarController:(UITabBarController*)tabBarController willEndCustomizingViewControllers:(NSArray__kindof UIViewController**)viewControllers changed:(BOOL)changed{NSLog(结束前);}-(void)tabBarController:(UITabBarController*)tabBarController didEndCustomizingViewControllers:(NSArray__kindof UIViewController**)viewControllers changed:(BOOL)changed{if(changedYES){NSLog(顺序发生变化);}NSLog(已结束);}-(void)tabBarController:(UITabBarController*)tabBarController didSelectViewController:(UIViewController*)viewController{NSLog(选中控制器对象);}VCF//.h#importUIKit/UIKit.hNS_ASSUME_NONNULL_BEGINinterfaceVCF:UIViewControllerendNS_ASSUME_NONNULL_END//.m#importVCF.hinterfaceVCF()endimplementationVCF-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.UITabBarItem*tabBarItem1[[UITabBarItem alloc]initWithTitle:探索image:nil tag:101];tabBarItem1.badgeValue78;self.tabBarItemtabBarItem1;self.view.backgroundColor[UIColor greenColor];}二、多界面传值1. 代理传值DelegateOC 经典传值方式系统控件全用代理。步骤B 定义协议A 遵守协议B 调用代理方法A 实现方法接收值2.代码演示SceneDelegate#importSceneDelegate.h#importVCFirst.h#importVCSecond.h#importVCThird.hinterfaceSceneDelegate()endimplementationSceneDelegate-(void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)connectionOptions{// Use this method to optionally configure and attach the UIWindow window to the provided UIWindowScene scene.// If using a storyboard, the window property will automatically be initialized and attached to the scene.// This delegate does not imply the connecting scene or session are new (see application:configurationForConnectingSceneSession instead).self.window.frame[UIScreen mainScreen].bounds;[self.window makeKeyAndVisible];VCFirst*vcFirst[[VCFirst alloc]init];vcFirst.title视图一;vcFirst.view.backgroundColor[UIColor whiteColor];UINavigationController*nav[[UINavigationController alloc]initWithRootViewController:vcFirst];VCThird*vcThird[[VCThird alloc]init];vcThird.title视图三;vcThird.view.backgroundColor[UIColor greenColor];UINavigationController*nav3[[UINavigationController alloc]initWithRootViewController:vcThird];NSArray*array[[NSArray alloc]initWithObjects:nav,vcThird,nil];UITabBarController*tabVC[[UITabBarController alloc]init];tabVC.viewControllersarray;self.window.rootViewControllertabVC;}VCFirst//.h#importUIKit/UIKit.h#importVCSecond.hNS_ASSUME_NONNULL_BEGINinterfaceVCFirst:UIViewControllerVCSecondDelegate-(void)changeColor:(UIColor*)color;end//.m#importVCFirst.h#importVCSecond.hinterfaceVCFirst()endimplementationVCFirst-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.}-(void)touchesBegan:(NSSetUITouch**)touches withEvent:(UIEvent*)event{VCSecond*vc[[VCSecond alloc]init];vc.view.backgroundColor[UIColor orangeColor];[self.navigationController pushViewController:vc animated:YES];vc.delegateself;}-(void)changeColor:(UIColor*)color{self.view.backgroundColorcolor;}VCSecond//.m#importUIKit/UIKit.hNS_ASSUME_NONNULL_BEGINprotocolVCSecondDelegateNSObject-(void)changeColor:(UIColor*)color;endinterfaceVCSecond:UIViewControllerproperty(assign,nonatomic)NSInteger tag;//定义一个代理对象执行协议函数property(assign,nonatomic)idVCSecondDelegatedelegate;endNS_ASSUME_NONNULL_END//.m#importVCSecond.hinterfaceVCSecond()endimplementationVCSecond-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view.self.view.backgroundColor[UIColor purpleColor];UIBarButtonItem*btnChange[[UIBarButtonItem alloc]initWithTitle:改变颜色style:UIBarButtonItemStyleDone target:selfaction:selector(pressChange)];self.navigationItem.rightBarButtonItembtnChange;}-(void)pressChange{[_delegate changeColor:[UIColor redColor]];}三、UITableView表格视图1. 核心概念必须设置dataSource数据源和delegate代理用复用机制节省内存滑动不卡顿组成UITableViewUITableViewCell2. 必须实现的 3 个方法// 1. 每组有多少行-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{returnself.dataArray.count;}// 2. 每行显示什么内容核心-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{// 复用标识符staticNSString*IDcellID;// 从复用池取 cellUITableViewCell*cell[tableView dequeueReusableCellWithIdentifier:ID];// 没有就创建if(!cell){cell[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];}// 赋值cell.textLabel.textself.dataArray[indexPath.row];returncell;}// 3. 行高-(CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath{return60;}3.创建 UITableView 步骤// 1. 创建UITableView*tableView[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];// 2. 设置数据源和代理tableView.dataSourceself;tableView.delegateself;// 3. 添加到视图[self.view addSubview:tableView];4.常用代理方法// 点击 cell 触发-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{NSLog(点击了第%ld行,indexPath.row);// 取消选中[tableView deselectRowAtIndexPath:indexPath animated:YES];}// 设置组头、组尾-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{return我是组头;}5.刷新数据// 刷新整个表格[self.tableView reloadData];// 刷新某一行[self.tableView reloadRowsAtIndexPaths:[indexPath]withRowAnimation:UITableViewRowAnimationNone];6.代码SceneDelegate#importSceneDelegate.h#importViewController.hinterfaceSceneDelegate()endimplementationSceneDelegate-(void)scene:(UIScene*)scene willConnectToSession:(UISceneSession*)session options:(UISceneConnectionOptions*)connectionOptions{// Use this method to optionally configure and attach the UIWindow window to the provided UIWindowScene scene.// If using a storyboard, the window property will automatically be initialized and attached to the scene.// This delegate does not imply the connecting scene or session are new (see application:configurationForConnectingSceneSession instead).self.window.frame[UIScreen mainScreen].bounds;UINavigationController*nav[[UINavigationController alloc]initWithRootViewController:[[ViewController alloc]init]];self.window.rootViewControllernav;[self.window makeKeyAndVisible];}ViewController#importUIKit/UIKit.hinterfaceViewController:UIViewControllerUITableViewDelegate,UITableViewDataSource{UITableView*_tableview;NSMutableArray*_arraydata;UIBarButtonItem*_btnedit;UIBarButtonItem*_btnfinish;UIBarButtonItem*_btndelete;BOOL _isedit;}end#importViewController.hinterfaceViewController()endimplementationViewController-(void)viewDidLoad{[superviewDidLoad];// Do any additional setup after loading the view._tableview[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStylePlain];_tableview.autoresizingMaskUIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;_tableview.delegateself;_tableview.dataSourceself;//数据视图的头部视图的设定_tableview.tableHeaderViewnil;_tableview.tableFooterViewnil;[self.view addSubview:_tableview];_arraydata[[NSMutableArray alloc]init];for(inti1;i20;i){NSString*str[NSString stringWithFormat:A %d,i];[_arraydata addObject:str];[selfcreatbtn];}}-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{return_arraydata.count;}-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{return1;}-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{NSString*strIDID;UITableViewCell*cell[_tableview dequeueReusableCellWithIdentifier:strID];if(cellnil){cell[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strID];}cell.textLabel.text_arraydata[indexPath.row];//单元格文字赋值cell.detailTextLabel.text子标题;returncell;}-(void)creatbtn{_iseditNO;_btnedit[[UIBarButtonItem alloc]initWithTitle:编辑style:UIBarButtonItemStylePlain target:selfaction:selector(pressedit)];_btnfinish[[UIBarButtonItem alloc]initWithTitle:完成style:(UIBarButtonItemStylePlain)target:selfaction:selector(pressfinish)];_btndelete[[UIBarButtonItem alloc]initWithTitle:删除style:(UIBarButtonItemStylePlain)target:selfaction:selector(pressdelete)];self.navigationItem.rightBarButtonItem_btnedit;}-(void)pressedit{_iseditYES;self.navigationItem.rightBarButtonItem_btnfinish;[_tableview setEditing:YES];self.navigationItem.leftBarButtonItem_btndelete;}-(void)pressfinish{_iseditNO;self.navigationItem.rightBarButtonItem_btnedit;[_tableview setEditing:NO];self.navigationItem.leftBarButtonItemnil;}//单元格显示效果协议-(UITableViewCellEditingStyle)tableView:(UITableView*)tableView editingStyleForRowAtIndexPath:(NSIndexPath*)indexPath{returnUITableViewCellEditingStyleDelete;}//可以显示编辑状态当手指滑动-(void)tableView:(UITableView*)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath*)indexPath{[_arraydata removeObjectAtIndex:indexPath.row];[_tableview reloadData];NSLog(delete);}-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{NSLog(选中单元格%d,indexPath.row1);}-(void)tableView:(UITableView*)tableView didDeselectRowAtIndexPath:(NSIndexPath*)indexPath{NSLog(取消选中%d,indexPath.row1);}end
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2557917.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!