首页 文章 精选 留言 我的

精选列表

搜索[ios],共10000篇文章
优秀的个人博客,低调大师

iOS:CoreData数据库的使用四(数据库和UITableViewController以及NSFetchedResultsControl...

CoreData数据库虽然可以和tableview或者UITableViewController一起使用将数据显示在表格上,但是在准备数据的时候,这种方式需要用一个可变数组来装从数据库一次性取出来的所有数据,然后通过操作这个数组来显示数据再表格上,从内存的优化和性能上来说并不是很好;这里,介绍一种新的抓取数据的方式,苹果公司为了进一步优化内存,创建了一个从数据库抓取数据的控制器NSFetchedResultsController,它能从CoreData中一批一批的抓取数据并存放起来,然后通过操作它来将数据显示在表格中,极大的优化了内存;与此同时,这个控制器还提供了一个<NSFetchedResultsControllerDelegate>协议,通过该控制器的代理可以实现数据的实时动画更新(删除、插入、修改)。CoreData数据库和UITableViewController以及NSFetchedResultsController一起使用,具有很强大的功能,是一个很不错的方式,推荐掌握这门技术。以下就具体的例子来介绍: 要求:创建一个实体对象Book数据库表,多次设置它的属性,并将它显示在表格中,显示后进行的操作有:(1)仅仅进行删除对象、(2)在删除时同时又插入两条对象信息、(3)删除时修改它的属性并再从新显示该对象 前期的创建数据库表的具体步骤: 1、创建新项目时,勾选Use Core Data,生成CoreData____NSFetchResultController.xcdatamodel文件,此时AppDelegate类中自动封装了sqlite的大量方法和需要的managedObjectContext、managedObjectModel、persistentStoreCoordinator三个对象等; 2、点击该文件,进入设置面板,点击左下角的Add Entity,创建实体对象并设置它的类名,同时在Attribute处添加该实体对象的属性; 3、选中该实体对象,点击模拟器菜单栏的Editor下的create NSManagedObjectSubclass..,自动生成实体对象的类Book.h和Book.m文件,类中自动生成了实体对象的所有属性; 4、再一次进入设置面板,点击右下角的style,可以查看成功创建的数据库表。 5、进入故事板Storyboard中,删除viewController控制器和它的类,重新拖入一个新的视图控制器和一个表格视图TableView; 6、新建一个新的类为BookTableViewController继承自UITableViewController,并将上面新拖入的视图控制器关联这个类即可。好了,前期工作完成。 接下来即时具体的代码操作了: 在AppDelegate类的- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法中奖数据存储到数据库: 1 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 2 3 //读取偏好设置,使测试数据只插入一次 4 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 5 self.isInserted = [userDefaults boolForKey:@"isInserted"]; 6 7 if(!self.isInserted) 8 { 9 [self addBookWithTitle:@"《红楼梦》" andPrice:@50.5]; 10 [self addBookWithTitle:@"《西游记》" andPrice:@48.5]; 11 [self addBookWithTitle:@"《水浒传》" andPrice:@67.5]; 12 [self addBookWithTitle:@"《三国演义》" andPrice:@70.0]; 13 [self addBookWithTitle:@"《资治通鉴》" andPrice:@79.0]; 14 [self addBookWithTitle:@"《本草纲目》" andPrice:@59.0]; 15 [self addBookWithTitle:@"《二十四史》" andPrice:@98.0]; 16 17 } 18 19 //设置偏好设置 20 [userDefaults setBool:YES forKey:@"isInserted"]; 21 [userDefaults synchronize]; 22 23 return YES; 24 } //将设置上面对象属性的方法封装起来 1 -(void)addBookWithTitle:(NSString*)title andPrice:(NSNumber*)price 2 { 3 //取出实体对象 4 Book *book = [NSEntityDescription insertNewObjectForEntityForName:NSStringFromClass([Book class]) inManagedObjectContext:self.managedObjectContext]; 5 6 //设置属性 7 book.title = title; 8 book.price = price; 9 10 //保存数据到持久层 11 [self saveContext]; 12 13 } 在BookTableViewController.m文件中: //导入头文件,实现协议和定义属性 1 #import "BookTableViewController.h" 2 #import "AppDelegate.h" 3 #import "Book.h" 4 5 @interface BookTableViewController ()<NSFetchedResultsControllerDelegate> 6 @property (strong,nonatomic)NSFetchedResultsController *fetchResultVC; 7 @property (strong,nonatomic)NSManagedObjectContext *managedObjectContext; //NSFetchedResultsController从数据库抓取数据 1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 4 //创建应用程序对象 5 AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; 6 7 self.managedObjectContext = appDelegate.managedObjectContext; 8 9 //fetchResultVC获取CoreData中的数据 10 NSError *error = nil; 11 [self.fetchResultVC performFetch:&error]; 12 if(error) 13 { 14 NSLog(@"获取数据失败"); 15 } 16 17 } //懒加载创建NSFecthedResultsController控制器对象 1 -(NSFetchedResultsController*)fetchResultVC 2 { 3 if(!_fetchResultVC) 4 { 5 //创建请求对象 6 NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Book class])]; 7 8 //批处理,每次从数据库中抓取的数量 9 fetchRequest.fetchBatchSize = 10; 10 11 //设置排序对象 12 NSSortDescriptor *priceSort = [NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]; 13 [fetchRequest setSortDescriptors:@[priceSort]]; 14 15 //创建fetchResultVC 16 _fetchResultVC = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:@"book"]; 17 18 //设置fetchResultVC的代理 19 _fetchResultVC.delegate = self; 20 } 21 return _fetchResultVC; 22 } #pragma mark - Table view data source//显示表格内容 //组数 1 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 2 { 3 return self.fetchResultVC.sections.count; 4 } //行数 1 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 2 { 3 id<NSFetchedResultsSectionInfo> sectionInfo = [self.fetchResultVC.sections objectAtIndex:section]; 4 5 return [sectionInfo numberOfObjects]; 6 } //将设置行内容的方法封装,调用它设置cell内容 1 -(void)configCell:(UITableViewCell*)cell indexPath:(NSIndexPath*)indexPath 2 { 3 Book *book = [self.fetchResultVC objectAtIndexPath:indexPath]; 4 cell.textLabel.text = book.title; 5 cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",book.price]; 6 } 1 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 //1.根据reuseIdentifier,先到对象池中去找重用的单元格对象 4 static NSString *reuseIdentifier = @"bookCell"; 5 UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:reuseIdentifier]; 6 //2.如果没有找到,自己创建单元格对象 7 if(cell == nil) 8 { 9 cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseIdentifier]; 10 } 11 //3.设置单元格对象的内容 12 [self configCell:cell indexPath:indexPath]; 13 return cell; 14 } //设置单元格的编辑与否 1 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath 2 { 3 return YES; 4 } //只进行删除数据 1 Book *book = [self.fetchResultVC objectAtIndexPath:indexPath];//获取实体对象 2 NSError *error = nil; 3 4 //删除数据 5 //从CoreData数据库删除数据 6 [self.managedObjectContext deleteObject:book];//从上下文是删除该实体对象 7 [self.managedObjectContext save:&error];//保存上下文 //删除数据时,同时新插入插入数据 1 AppDelegate *appDelegate = [[UIApplication sharedApplication]delegate]; 2 [appDelegate addBookWithTitle:@"《狂人日记》" andPrice:@20.0]; 3 [appDelegate addBookWithTitle:@"《洗冤集录》" andPrice:@24.5]; //删除数据时,修改数据并显示 1 //在删除数据时,修改它的数据并更新 2 book.price = @([book.price doubleValue] + 10); 3 [self.managedObjectContext save:&error]; 4 if(error) 5 { 6 NSLog(@"删除失败"); 7 } #pragma mark -<NSFetchedResultsControllerDelegate>代理方法,更新变化后的表格信息 1 //表格开始更新 2 -(void)controllerWillChangeContent:(NSFetchedResultsController *)controller 3 { 4 [self.tableView beginUpdates]; 5 } 6 //尽心更新处理 7 -(void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath 8 { 9 if(type == NSFetchedResultsChangeDelete) //删除数据 10 { 11 [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 12 } 13 else if(type == NSFetchedResultsChangeInsert) //插入数据 14 { 15 [self.tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 16 } 17 else if(type == NSFetchedResultsChangeUpdate) //更新(修改)数据 18 { 19 20 UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; 21 [self configCell:cell indexPath:indexPath]; 22 23 [self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; 24 } 25 } 26 //表格更新结束 27 -(void)controllerDidChangeContent:(NSFetchedResultsController *)controller 28 { 29 [self.tableView endUpdates]; 30 } 演示结果如下: 没有进行任何操作时: 选中单元格往左滑动,删除数据时 删除数据时,同时新插入两个对象 没删除一次,对象的属性价格就发生了改变 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4833422.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS:切换视图的第三种方式:UITabBarController标签栏控制器

UITabBarController:标签栏控制器 •通过设置viewControllers属性或者addChildViewController方法可以添加子控制器 –NSArray*viewControllers –- (void)addChildViewController:(UIViewController*)childController •UIViewController的tabBarItem属性决定了UITabBar显示的内容 –使用系统自带的UITabBarItem样式: controller.tabBarItem = [[[UITabBarItem alloc]initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1] autorelease]; –自己设置UITabBarItem的标题和图片: controller.tabBarItem.title = @”More”; // controller.title也可以用于设置标题 controller.tabBarItem.image = [UIImage imageNamed:@”more”]; –设置UITabBarItem右上角的红色数字 controller.tabBarItem.badgeValue = @”10”; 例如在storyboard中拖入一个UITabBarController,它首先自带着两个控制器,并以manual方式连接着,然后可以再拖入两个控制器,在第一个控制器TabBarController中单击右键,仍以manual方式拖到目的控制器连接起来。最后设置每一个控制器视图颜色、标签栏标题、tabBarItem样式即可。标签栏最多显示5个视图,如果多于5个,剩下的控制器会变成more标签,点击more后,可以进去查看剩余不能显示的控制器,只不过它们被放到了表格中。 演示结果: 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4809401.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

iOS:视图切换的第二种方式:UINavigationController导航栏控制器

UINavigationController:一个以栈的形式管理多视图的容器,负责子控制器之间的跳转。由于以栈的方式管理视图,各个视图的切换就是压栈和出栈操作,所以出栈后的视图会立即销毁。 介绍: <1> UINavigationController也是容器的一种,负责子控制器之间的跳转 <2> UINavigationController以栈的形式保存子控制器,这里的栈其实是个NSArray对象,只有位于栈顶的子控制器的view才是可见的 <3> 常用属性: –UIViewController*topViewController处于栈顶的子控制器 –NSArray*viewControllers栈,存放着所有的子控制器 <4> UINavigationController的view包括了两大部分: 上面是UINavigationBar导航栏,里面放着导航栏标题和navigationItem导航条。 下面是topViewController的view,topViewController的navigationItem属性决定了UINavigationBar显示的内容 <5>除了导航栏中的title,UINavigationItem还有一些属性能够影响UINavigationBar的显示内容 –UIBarButtonItem*backBarButtonItem左上角的返回按钮 –UIView*titleView中间的标题视图 –UIBarButtonItem*leftBarButtonItem左上角的视图 –UIBarButtonItem*rightBarButtonItem右上角的视图 导航栏标题Title设置 导航条navigationItem中属性的(左按钮、右按钮、标题视图)设置 <6> UINavigationController的pushViewController:animated:方法可以将一个控制器压入栈中 子控制器都有个navigationController属性,可以获取当前的UINavigationController对象 假如从子控制器c1跳转到子控制器c2,可以这样做: [c1.navigationController pushViewController:c2 animated:YES]; <7> 纯代码创建多视图的步骤: 1、首先将项目中的storyboard删除,并将项目栏General中Main interface的设置取消。 2、在应用程序代理类中创建window并设置它的frame,一般将window的大小设置为单例对象屏幕的大小,然后给window设置根控制器。 3、创建UINavigationController控制器,它是一个容器,用- (instancetype)initWithRootViewController:(UIViewController*)rootViewController方法初始化,传入一个UIViewController实例作为根控制器,这个根控制器永远处于栈底(如果栈中只有一个对象,那么根控制器也处于栈顶),将某个UIViewController压入栈时,控制器的视图会从窗口右侧滑入;出栈时,栈顶的控制器会被移除,其下的控制器的视图会从窗口左侧滑入 3、如果还要创建其他多个控制器,创建后,必须要获取当前栈中的根控制器,根控制器用- (void)pushViewController:(UIViewController*)viewController animated:(BOOL)animate方法将这些控制器依次推入栈中。 4、针对每一个控制器,在它们的类中分别设置它们的导航栏标题和导航条navigationItem中的属性,例如添加按钮、设置标题视图等,添加按钮时,给按钮初始化时可以设置类型,并添加事件,在事件中用方法:- (NSArray*)popToRootViewControllerAnimated:(BOOL)animated推出栈中它的前一个控制器。 纯代码创建举例如下: 显示结果为: 文件截图为: 取消general中的Main interface设置 代码如下: 应用程序代理类中创建window和根控制器 1 #import "AppDelegate.h" 2 #import "ViewController.h" 3 4 @interface AppDelegate () 5 6 @end 7 8 @implementation AppDelegate 9 10 11 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 12 //创建window 13 self.window = [[UIWindow alloc]init]; 14 15 //设置window的大小 16 CGSize screenSize = [[UIScreen mainScreen]bounds].size; 17 self.window.frame = CGRectMake(0, 0, screenSize.width, screenSize.height); 18 19 //为window设置根控制器 20 ViewController *VC = [[ViewController alloc]init]; 21 UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:VC]; 22 23 self.window.rootViewController = naVC; 24 25 //接收用户输入并显示 26 [self.window makeKeyAndVisible]; 27 28 return YES; 29 } 30 31 - (void)applicationWillResignActive:(UIApplication *)application { 32 // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 33 // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 34 } 35 36 - (void)applicationDidEnterBackground:(UIApplication *)application { 37 // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 38 // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 39 } 40 41 - (void)applicationWillEnterForeground:(UIApplication *)application { 42 // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 43 } 44 45 - (void)applicationDidBecomeActive:(UIApplication *)application { 46 // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 47 } 48 49 - (void)applicationWillTerminate:(UIApplication *)application { 50 // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 51 } 52 53 @end 在设置的根控制器类中设置导航栏属性,并将新的视图控制器推入栈中 1 #import "ViewController.h" 2 #import "secondViewController.h" 3 4 @interface ViewController () 5 6 @end 7 8 @implementation ViewController 9 10 - (void)viewDidLoad { 11 [super viewDidLoad]; 12 [self.view setBackgroundColor:[UIColor yellowColor]]; 13 14 //设置导航栏 15 //设置标题 16 self.navigationItem.title = @"First"; 17 18 //设置返回按钮 19 self.navigationItem.backBarButtonItem.title = @"first"; 20 21 //设置右边的按钮 22 self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addClicked:)]; 23 } 24 25 -(void)addClicked:(UIBarButtonItem*)sender 26 { 27 secondViewController *secondVC = [[secondViewController alloc]init]; 28 29 //将新的控制器压入导航控制器 30 [self.navigationController pushViewController:secondVC animated:YES]; 31 } 32 33 @end 在新的视图控制器类中设置导航栏属性,将当前控制器弹出栈中 1 #import "secondViewController.h" 2 3 @interface secondViewController () 4 5 @end 6 7 @implementation secondViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 self.view.backgroundColor = [UIColor redColor]; 12 13 //设置导航栏 14 //1、设置标题 15 self.navigationItem.title = @"Second"; 16 17 //2.设置左边的按钮 18 self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Return" style:UIBarButtonItemStylePlain target:self action:@selector(backClicked:)]; 19 } 20 21 -(void)backClicked:(UIBarButtonItem*)sender 22 { 23 //将当前的控制器弹出导航控制器 24 [self.navigationController popToRootViewControllerAnimated:YES]; 25 } 26 @end 程序猿神奇的手,每时每刻,这双手都在改变着世界的交互方式! 本文转自当天真遇到现实博客园博客,原文链接:http://www.cnblogs.com/XYQ-208910/p/4801186.html,如需转载请自行联系原作者

优秀的个人博客,低调大师

苹果强制 iOS 应用使用 HTTP ?为了探寻真相,我给苹果打了个电话

“苹果爸爸一张嘴,程序员们跑断腿” ,看来此话挺有道理。 电梯里,宅客频道编辑听到两位程序员在聊天: 甲:“新闻说元旦之后,没上 HTTPS 的应用就不能提交AppStore了,元旦前得赶紧了!” 乙:“你早说啊,年底事这么多,离元旦就剩几天了。” 甲:“Deadline是第一生产力,我这是给你增加工作动力。” 乙:“嗯,似乎很有道理的样子……谢谢哥。” 宅客频道编辑回去一搜索,发现确实有许多相关新闻。 正当雷锋网宅客频道打算写一篇有关苹果ATS安全功能的科普文时,却发现一个奇怪之处: 外面“传闻遍地”,但苹果商店官方并没有给开发者下发邮件通知,全面禁用HTTP这可是件大事,按照苹果的行事风格,不可能到现在还下发公告。 宅客频道预感:此事必有蹊跷! 苹果强制HTTPS,到底怎么回事? 根据雷锋网宅客频道的调查,事情还得从几个月前说起…… 这

优秀的个人博客,低调大师

《企业级ios应用开发实战》一第3章 Objective-C 语法简介

第3章 Objective-C 语法简介 本书不是一本关于Objective-C编程语言的专著,但仍然会介绍一些Objective-C语言的语法基础和有趣特性。这对于刚刚接触到iPhone编程的人来说,会是一个很好的开始。Objective-C兼具C语言和面向对象语言的特性。因此本章会从两个方面来介绍Objective-C(简称O-C)。首先,由于Objective-C具有C语言背景(它仍然是一种C),它从C语言中继承了一些C语言特性,例如:Objective-C全面支持C的数据类型(包括简单类型和复合类型)、常量/变量/宏、包含头文件(import/include指令)、函数、条件和循环控制语句。其次,Objective-C在C语言的基础上进行了扩展,加入了面向对象的内容。例如:对象和类(方法及属性的集合)、Objective-C的消息机制、内存管理(包括对象生命周期和对象的创建和释放)、类别和协议、反射、谓词,以及Cocoa对一些常见模式(Pattern)的Objective-C实现(如MVC、KVO)。在本书后续的一些章节中,使用了块语法。因此本章最后单独对Objective-C的块编程进行了介绍。 块是现代C语言标准的一部分,类似C++中的inline函数。Objective-C以面向对象的方式对C的块进行了封装。你可以有针对性地阅读这些内容。比如对于有C基础的程序员,应该跳过基本语法部分(主要是C语言特性),重点阅读Objective-C在C语言基础上进行的扩展(如NS类、类别和协议),以及Cocoa提供的一些优秀特性(比如MVC、KVO)。

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册
修改密码
忘记密码