首页 文章 精选 留言 我的

精选列表

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

IOS文件沙盒

在进行IPhone开发的时候,常常需要将简单的数据保存到文件中,以便下次再重新打开的时候能够读取文件中保存的数据。 下面就来做一个简单的demo: 步骤: 1.创建一个SingleView项目,带有xib文件,在xib面板上拖放两个文本框,和一个按钮 小提示:如何设置文本框编辑的时候能够点击键盘上的return键退出键盘? 右击文本框,发现他的Did End On Exit事件,然后拖放到.h文件中,创建一个click事件 - (IBAction)click:(id)sender { [senderresignFirstResponder]; } 2.修改ViewController.m: #import "DXWViewController.h" @interface DXWViewController () @end @implementation DXWViewController - (void)viewDidLoad { [super viewDidLoad]; //获得NSUserDefaults NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; //对NSUserDefaults进行赋值 // [user setObject:@"小明" forKey:@"name"]; // [user setInteger:20 forKey:@"age"]; // [user synchronize];//从缓存写入文件 //从NSUserDefaults通过key获取值 if ([user objectForKey:@"name"] != nil) { self.txtName.text = [user objectForKey:@"name"]; self.txtAge.text = [NSString stringWithFormat:@"%d",[user integerForKey:@"age"]]; } } - (void)dealloc { [_txtName release]; [_txtAge release]; [super dealloc]; } - (IBAction)click:(id)sender { NSUserDefaults *user = [NSUserDefaults standardUserDefaults]; [user setObject:self.txtName.text forKey:@"name"]; [user setInteger:[self.txtAge.text intValue] forKey:@"age"]; [user synchronize];//从缓存写入文件 } 如何寻找沙盒文件? 点击mac系统上最上面一行的前往功能,然后按住alt键会显示出资源文件,该文件一般是隐藏的,前往->资源库->Application Support->iPhone Simulator->6.1->Applications-> 选择自定义的文件->Library或者Documents(存储大数据,自定义文件名的文件都在这里面) 如何创建自定义文件名文件? //获取沙盒文件路径 -(NSString*)getPath { //用来获得Document地址 NSArray*arr =NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);//注意:这里是NSDocument不是NSDocumentation,特别注意 NSLog(@"%@",arr); //在地址上增加文件 NSString*path = [arr[0]stringByAppendingPathComponent:@"abc.plist"]; NSLog(@"%@",path); returnpath; } //创建自定义文件,并存储或者获取文件中数据 - (void)viewDidLoad { [superviewDidLoad]; NSArray*arr =@[@"aaa",@"bbb",@"ccc"]; [arrwriteToFile:[selfgetPath]atomically:YES]; //判断是否有文件 if([[NSFileManagerdefaultManager]fileExistsAtPath:[selfgetPath]]) { arr = [NSArrayarrayWithContentsOfFile:[selfgetPath]]; NSLog(@"%@",arr); } } 当开发一个应用要适应使用者还没点击保存,但一个电话打过来,要先接电话,所以就要让系统自动先保存当前的数据,适合实现? //获取应用 UIApplication*app = [UIApplicationsharedApplication]; //在通知中心添加一个观察者,当符合UIApplicationWillResignActiveNotification条件时,调用方法 [[NSNotificationCenterdefaultCenter]addObserver:selfselector:@selector(save:)name:UIApplicationWillResignActiveNotificationobject:app]; -(void)save:(id)sender { NSArray*arr =@[self.text1.text1,self.text2.text]; [arrwriteToFile:[selfgetPath]atomically:YES]; } 本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366423,如需转载请自行联系原作者

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

iOS ViewController添加Segue

有时候需要判断逻辑再弹出View,这样子的话就不能直接通过按钮的Segue来弹出,就需要添加代码来控制segue。此时segue的添加(那个箭头),需要在viewcontroller之间建立。需要注意的是!源viewcontroller需要在左侧栏右击!给segue添加好id,直接用这一句话可以搞定。 [self performSegueWithIdentifier:@"segue_register2main" sender:self]; 本文转自 卓行天下 51CTO博客,原文链接:http://blog.51cto.com/9951038/1772526,如需转载请自行联系原作者

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

ios协议和委托

在iPhone开发协议和委托是常接触到的东西,到底什么是协议什么是委托,他们什么关系? 一 协议 (1)协议相当于没有与类相关联的接口,他申明一组方法,列出他的参数和返回值,共享给其他类使用,然后不进行实现,让用它的类来实现这些方法 (2)在任何一个类中,只有声明了协议,都可以实现协议里的方法。 (3)协议不是一个类,更没有父类了。 (3)协议里面的方法经常都是一些委托方法, 二 委托 委托,故名思议就是托别人办事。打个比方: 张三迫切需要一分工作,但是不知道去哪找。于是他就拜托(委托)李四给帮找一份合适工作,但是托人办事得给被人好处啊,于是张三给李四塞了一个红包(协议),于是李四通过自己关系在某公司找了一份文秘的工作(实现协议里面委托方法),于然后他把文秘这份工作给了张三,张三就找到工作了; 三 我们来看一个比较常用的表格单元实现委托和协议 UITableViewDataSource协议和他的委托方法 @protocol UITableViewDataSource<NSObject> @required - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section; // Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier: // Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls) - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath; @optional - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView; // Default is 1 if not implemented - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section; // fixed font style. use custom view (UILabel) if you want something different - (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section; // Editing // Individual rows can opt out of having the -editing property set for them. If not implemented, all rows are assumed to be editable. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows the reorder accessory view to optionally be shown for a particular row. By default, the reorder control will be shown only if the datasource implements -tableView:moveRowAtIndexPath:toIndexPath: - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath; // Index - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView; // return list of section titles to display in section index view (e.g. "ABCD...Z#") - (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index; // tell table which section corresponds to section title/index (e.g. "B",1)) // Data manipulation - insert and delete support // After a row has the minus or plus button invoked (based on the UITableViewCellEditingStyle for the cell), the dataSource must commit the change - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath; // Data manipulation - reorder / moving support - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath; @end 这是一个完整协议定义 @protocol 协议名 声明方法 @end 但是我们还看到两个特殊关键字 @required 和 @optional @required 表示我们用到这个协议的时候必须实现这个协议的方法 @optional 表示我们可选择性实现这些方法,看那个需要我们就去实现,不需要的就不实现 UITableViewDelegate协议和委托方法 @protocol UITableViewDelegate<NSObject, UIScrollViewDelegate> @optional // Display customization - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath; // Variable height support - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath; - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section; - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section; // Section header & footer information. Views are preferred over title should you decide to provide both - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; // custom view for header. will be adjusted to default or specified header height - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section; // custom view for footer. will be adjusted to default or specified footer height // Accessories (disclosures). - (UITableViewCellAccessoryType)tableView:(UITableView *)tableView accessoryTypeForRowWithIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_BUT_DEPRECATED(__MAC_NA,__MAC_NA,__IPHONE_2_0,__IPHONE_3_0); - (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath; // Selection // Called before the user changes the selection. Return a new indexPath, or nil, to change the proposed selection. - (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (NSIndexPath *)tableView:(UITableView *)tableView willDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // Called after the user changes the selection. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // Editing // Allows customization of the editingStyle for a particular cell located at 'indexPath'. If not implemented, all editable cells will have UITableViewCellEditingStyleDelete set for them when the table has editing property set to YES. - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath; - (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0); // Controls whether the background is indented while editing. If not implemented, the default is YES. This is unrelated to the indentation level below. This method only applies to grouped style table views. - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath; // The willBegin/didEnd methods are called whenever the 'editing' property is automatically changed by the table (allowing insert/delete/move). This is done by a swipe activating a single row - (void)tableView:(UITableView*)tableView willBeginEditingRowAtIndexPath:(NSIndexPath *)indexPath; - (void)tableView:(UITableView*)tableView didEndEditingRowAtIndexPath:(NSIndexPath *)indexPath; // Moving/reordering // Allows customization of the target row for a particular row as it is being moved/reordered - (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath; // Indentation - (NSInteger)tableView:(UITableView *)tableView indentationLevelForRowAtIndexPath:(NSIndexPath *)indexPath; // return 'depth' of row for hierarchies // Copy/Paste. All three methods must be implemented by the delegate. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0); - (BOOL)tableView:(UITableView *)tableView canPerformAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0); - (void)tableView:(UITableView *)tableView performAction:(SEL)action forRowAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_5_0); @end 在用的时候,我们现在声明协议 #import <UIKit/UIKit.h> @interface BIDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @property (strong, nonatomic) NSDictionary *names; @property (strong, nonatomic) NSArray *keys; @end 实现UITableViewDataSource UITableViewDelegate协议里面的委托方法 #pragma mark - #pragma mark Table View Data Source Methods - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [keys count]; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { NSString *key = [keys objectAtIndex:section]; NSArray *nameSection = [names objectForKey:key]; return [nameSection count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { NSUInteger section = [indexPath section]; NSUInteger row = [indexPath row]; NSString *key = [keys objectAtIndex:section]; NSArray *nameSection = [names objectForKey:key]; static NSString *SectionsTableIdentifier = @"SectionsTableIdentifier"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: SectionsTableIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:SectionsTableIdentifier]; } cell.textLabel.text = [nameSection objectAtIndex:row]; return cell; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *key = [keys objectAtIndex:section]; return key; } - (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView { return keys; } 这就就是实现一些里面的委托方法过程运行改程序运行结果 该程序源码http://download.csdn.net/detail/duxinfeng2010/4695666 本文转自新风作浪 51CTO博客,原文链接:http://blog.51cto.com/duxinfeng/1208715,如需转载请自行联系原作者

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

ios27--kvo

// // ViewController.h // 14-KVO的使用 #import <UIKit/UIKit.h> @interface ViewController : UIViewController @end // // ViewController.m // 14-KVO的使用 // /* KVO: Key Value Observing (键值监听)--->当某个对象的属性值发生改变的时候(用KVO监听) */ #import "ViewController.h" #import "XMGPerson.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; XMGPerson *person = [[XMGPerson alloc] init]; person.name = @"zs"; XMGPerson *person1 = [[XMGPerson alloc] init]; person.name = @"zs"; /* 作用:给对象绑定一个监听器(观察者) - Observer 观察者 - KeyPath 要监听的属性 - options 选项(方法方法中拿到属性值,拿到新值和旧值) */ [person addObserver:self forKeyPath:@"name" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil]; person.name = @"ls"; person1.name = @"ls"; person.name = @"ww"; // 移除监听 [person removeObserver:self forKeyPath:@"name"];//移除哪个对象的哪个属性的监听 } /** * 当监听的属性值发生改变 * * @param keyPath 要改变的属性 * @param object 要改变的属性所属的对象 * @param change 改变的内容 * @param context 上下文 */ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{ NSLog(@"%@------%@------%@------%@", keyPath, object, change,context); /* name------<XMGPerson: 0x6080000102d0>------{kind = 1;new = ls;old = zs;}------(null) */ } @end // // XMGPerson.h // 14-KVO的使用 #import <Foundation/Foundation.h> @interface XMGPerson : NSObject /** 姓名 */ @property (nonatomic, copy) NSString *name; //string用copy @end // // XMGPerson.m // 14-KVO的使用 #import "XMGPerson.h" @implementation XMGPerson @end 本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7486376.html,如需转载请自行联系原作者

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

ios22--动画

控制器: // // ViewController.m // 07-渐变动画 // // Created by xiaomage on 15/12/30. // Copyright © 2015年 小码哥. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (weak, nonatomic) IBOutlet UIView *animationView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; } /** * 平移 */ - (IBAction)translate { // 渐变动画 // 方式一 /* // 1. 开始动画 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0]; // 2.动画代码 CGRect frame = self.animationView.frame; frame.origin.y -= 50; self.animationView.frame = frame; // 3.提交动画 [UIView commitAnimations]; */ // 方式二: /* [UIView animateWithDuration:2.0 animations:^{ // 1.动画代码 CGRect frame = self.animationView.frame; frame.origin.y -= 50; self.animationView.frame = frame; }]; */ /* [UIView animateWithDuration:1.0 animations:^{ // 执行动画 CGRect frame = self.animationView.frame; frame.origin.y -= 50; self.animationView.frame = frame; } completion:^(BOOL finished) { // 动画完成做什么事情 self.animationView.backgroundColor = [UIColor blackColor]; }]; */ /* UIViewAnimationOptionCurveEaseInOut 动画开始/结束比较缓慢,中间相对较快 UIViewAnimationOptionCurveEaseIn 动画开始比较缓慢 UIViewAnimationOptionCurveEaseOut 动画结束比较缓慢 UIViewAnimationOptionCurveLinear 线性---> 匀速 */ [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseInOut animations:^{ CGRect frame = self.animationView.frame; frame.origin.y += 50; self.animationView.frame = frame; } completion:^(BOOL finished) { self.animationView.backgroundColor = [UIColor greenColor]; }]; } /** * 缩放 */ - (IBAction)scale { [UIView animateWithDuration:1.0 delay:1.0 options:UIViewAnimationOptionCurveEaseIn animations:^{ CGRect frame = self.animationView.frame; frame.size = CGSizeMake(10, 15); self.animationView.frame = frame; } completion:^(BOOL finished) { NSLog(@"动画完成"); }]; } /** * 透明度动画 */ - (IBAction)alpha { [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveEaseOut animations:^{ self.animationView.alpha -= 0.9; } completion:^(BOOL finished) { [UIView animateWithDuration:2.0 animations:^{ self.animationView.alpha += 0.9; }]; }]; } @end 本文转自农夫山泉别墅博客园博客,原文链接:http://www.cnblogs.com/yaowen/p/7469583.html,如需转载请自行联系原作者

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

iOS:view.frame

我们知道view.frame 是在当前view在父容器坐标空间中的。我在ipad的一个ViewController中只放一个view,View里面只放了个webView(界面Landscape),本想达到webview的contentSize,所以我在代码中添加: View Code 但是输出确为: self.view.frame is {{0, 20}, {768, 1004}} self.webView.frame is {{0, 0}, {768, 1004}} webView.scrollView.contentSize is {1024, 1004} webView.scrollView.frame.size is {100, 100} 完全不和逻辑。 原来我把上面代码放在了viewDidLoad中了,系统价值玩nib或StoryBoard中的视图后就立即调用viewDidLoad了,这时系统根本还没加载进去里面的内容,方向什么的也都还不知道。 当我把代码放在加载之后的地方之后,正确的应该是: self.view.frame is {{20, 0}, {748, 1024}} self.webView.frame is {{0, 0}, {1024, 748}} webView.scrollView.contentSize is {5944, 748} webView.scrollView.frame.size is {1024, 748} 另外注意,UIWindow的坐标体统一直都是竖立的。 check more:http://stackoverflow.com/questions/9539676/uiviewcontroller-returns-invalid-frame 本文转自老Zhan博客园博客,原文链接:http://www.cnblogs.com/mybkn/archive/2012/11/30/2796175.html,如需转载请自行联系原作者

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

IOS 日志输出控制

用以下内容可以控制在debug版本中打印日志,而在release版本中不打印 #ifdef DEBUG # define DBLog(format,...) NSLog((@"[%s][%s][%d]" format), __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__); #else # define DBLog(...); #endif 备注: 1.ANSI C标准中有几个标准预定义宏(也是常用的): __LINE__:在源代码中插入当前源代码行号; __FILE__:在源文件中插入当前源文件名; __DATE__:在源文件中插入当前的编译日期 __TIME__:在源文件中插入当前编译时间; __STDC__:当要求程序严格遵循ANSI C标准时该标识被赋值为1; __cplusplus:当编写C++程序时该标识符被定义。 2.可变参数宏 ...和_ _VA_ARGS_ _ __VA_ARGS__ 是一个可变参数的宏,很少人知道这个宏,这个可变参数的宏是新的C99规范中新增的,目前似乎只有gcc支持(VC6.0的编译器不支持)。 实现思想就是宏定义中参数列表的最后一个参数为省略号(也就是三个点)。这样预定义宏_ _VA_ARGS_ _就可以被用在替换部分中,替换省略号所代表的字符串。 参考了http://www.cnblogs.com/lixiaohui-ambition/archive/2012/08/21/2649052.html 本文转自leipei博客园博客,原文链接:http://www.cnblogs.com/leipei2352/p/3222000.html,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

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

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

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

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

用户登录
用户注册