ios开发实例,ios开发 单例

iOS 开发 delegate详解

首先,让我们看看为什么要使用delegate。

10余年的涟源网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。网络营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整涟源建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联建站从事“涟源网站设计”,“涟源网站推广”以来,每个客户项目都认真落实执行。

一个典型的ios应用程序会有各种类型的对象:windows,tables,buttons,input boxes等等。在一个特定的应用程序中,你会让每个对象做特定的事情。比如说当用户点击一个按钮的时候,会执行一个特定的操作或者使用一个table显示特定的数据。

每个对象实例会有处理 特定 的事情。比如说我们有一个UITableView的实例,我们可能会让它以特定的方式来显示特定的 数据,同样的,我们对table的每一行进行tapping或者swiping操作的时候,它们也会做出自己特有的事件处理方式。为了避免为达到某一个特定的目的(actions or events)而创建一个子类(如果有多种不同的特定目的就会创建多个子类),你可以写出响应这些操作或者事件的方法,它们会在这些操作或者事件发生的时候被调用,为了达到此目的,你只需要给这个对象一个delegate(一个对象)。你可以使一个类作为其他一个对象或者多个对象的delegate。

简单的说代理就是在类外,让特定的人对该类做特定的操作的一种实现方法。

iOS开发——Dump一个OC类的所有方法

需求如题,如果是你,你一定首先想到看看JavaScriptCore有没有暴露出对应的 delegate 方法可以直接用,抱歉,还真没有。那该怎么办?怎么办?么办?办?......

记得之前有一篇关于 JSBridge 的文章有提到,直接 NSObject 类有一个方法,webView:didCreateJavaScriptContext:forFrame:,从语义上看,应该就满足上面的需求。

好,写个 NSObject 的分类实现下看看。

测试很简单,在 web 页面的 js 脚本里,调用 printHello(); 即可。

实践证明:好使!

熟悉 runtime 的话,这个很简单,调用 class_copyMethodList 把类里所有的方法取出来打印就好。有的时候,为了分析一个需求如何实现,可能要递归打印几个类的方法,从当前类,到其父类,到其爷爷类,...直到 NSObject 类。

感谢 oc 语言的创始人和开发者,每个类的类方法、实例方法都几乎可以做到望文生义,才让大家能有这一快乐!

最后,enjoy yourself!

ios开发中类方法与实例方法区别 以及self有什么不同

类方法不为某个对象所拥有,其方法内一般都写一些通用的,不变更的功能,使用时用[类名称 方法名称]调用

实例方法:实例方法是被对象所拥有,一个对象就会产生自己的实例方法,不通的对象可能执行的结果都不一样,[对象 方法名称] 调用,

相对于类方法和对象方法中,self 也是不同,类方法中,self指代的是类,对象方法中,self指代的是对象,总言之,谁调用方法,其中的self就知道什么

ios开发cookie的使用

1.cookie的概念

Cookie是由服务器端生成,发送给User-Agent(一般是浏览器或者客户端),浏览器会将Cookie的key/value保存到某个目录下的文本文件内,下次请求同一网站地址时就发送该Cookie给服务器在使用UIWebView访问网站时,经常遇到的一个问题是需要登录。比如访问新浪微博,或者访问某个个人账号,但是需要直接访问url,却会被重定向到登录页进行登录。那么真正的浏览器是如何做到的直接登录?一般来说,答案都是cookies。在你第一次登录时,网站设置了cookie,浏览器保存这些携带了登录信息的cookies,以后就可以直接登录。所以我们要做的就是在url request中带上cookies。

2.苹果系统中的cookie机制

因为NSHTTPCookieStorage的默认策略是:NSHTTPCookieAcceptPolicyAlways。当你访问一个网站时,NSURLRequest都会帮你主动记录下来你访问的站点设置的cookie,如果 Cookie 存在的话,会把这些信息放在 NSHTTPCookieStorage 容器中共享,

当你下次再访问这个站点时,NSURLRequest会拿着上次保存下来了的cookie继续去请求。

同样适用于ASIHTTPRequest,AFNetworking, Webview等,cookie常用于一些基于认证的网络请求

NSHTTPCookieStorage 实现了一个管理cookie的单例对象(只有一个实例),每个cookie都是NSHTTPCookie类的实例,最为一个规则,cookie在所有应用之间共享并在不同进程之间保持同步。

Session cookie(一个isSessionOnly方法返回YES的cookie)只能在单一进程中使用。

1.获取cookie

2、如果当前cookie为空,只要重新请求一个url

3、清空cookie

4、设置指定的cookie

6.UIWebView访问网站时,经常遇到的一个问题是需要登录。比如访问新浪微博,或者访问某个个人账号,但是需要直接访问服务器返回的某个url,却会被重定向到登录页进行登录。

补充

关于ios中cookie的使用案列: ios中Cookie的使用

iOS开发中遇到的小问题-----总结

1、统一收键盘的方法

[[[UIApplication sharedApplication] keyWindow] endEditing:YES];

2、提示框

BBAlertView *alert = [[BBAlertView alloc] initWithStyle:BBAlertViewStyleDefault

Title:@"删除订单"

message:@"是否删除订单,"

customView:nil

delegate:self

cancelButtonTitle:L(@"取消")

otherButtonTitles:L(@"确认")];

[alert setCancelBlock:^{

}];

[alert setConfirmBlock:^{

[self orderDidRemovePressDown:tempDic Index:index.section];

}];

[alert show];

3、图片的自适应功能

self.brandImage.contentMode = UIViewContentModeScaleAspectFit;

4、cocoaPods清除缓存问题

$ sudo rm -fr ~/.cocoapods/repos/master

$ pod setup

5、设置显示键盘的样式

textView.keyboardType =UIKeyboardTypeDefault;

//设置键盘右下角为完成(中文输入法下)

textView.returnKeyType=UIReturnKeyDone;

6、输出当前时间

NSDateFormatter * dateFormatter=[[NSDateFormatter alloc]init];

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];

NSLog(@"当前毫秒时间1==%@",[dateFormatter stringFromDate:[NSDate date]]);

7、显示两秒然后消失

UILabel * lab=[[UILabel alloc]initWithFrame:CGRectMake(60,Main_Screen_Height-64-49-60, Main_Screen_Width-120, 50)];

lab.backgroundColor=[UIColor grayColor];

ViewRadius(lab, 20);

lab.textAlignment=NSTextAlignmentCenter;

lab.text=@"请先进行实名制验证";

[self.view addSubview:lab];

[UILabel animateWithDuration:2 animations:^{

lab.alpha=0;

}completion:^(BOOL finished) {

[lab removeFromSuperview];

}];

8、设置placeholder属性的大小和颜色

[_phoneFie setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];

[_phoneFie setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];

_phoneFie.returnKeyType=UIReturnKeyDone;

9、设置cell的交互完全不可以使用

//[cellTwo setUserInteractionEnabled:NO];

//设置cell不可以点击,但是上面的子控件可以交互

cellTwo.selectionStyle=UITableViewCellSelectionStyleNone;

10、将textField的placeholder 属性的字体向右边移动5

_field.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10*Width_375, _field.frame.size.height)];

_field.leftViewMode = UITextFieldViewModeAlways;

11、开新线程使按钮上的时间变化

-(void)startTime{

__block int timeout=60; //倒计时时间

UIButton * btn=(UIButton *)[self.view viewWithTag:1000];

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);

dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行

dispatch_source_set_event_handler(_timer, ^{

if(timeout=0){

dispatch_source_cancel(_timer);

dispatch_async(dispatch_get_main_queue(), ^{

[btn setTitle:@"发送验证码" forState:UIControlStateNormal];

btn.enabled = YES;

});

}else{

//  int minutes = timeout / 60;

int miao = timeout % 60;

if (miao==0) {

miao = 60;

}

NSString *strTime = [NSString stringWithFormat:@"%.2d", miao];

dispatch_async(dispatch_get_main_queue(), ^{

[btn setTitle:[NSString stringWithFormat:@"剩余%@秒",strTime] forState:UIControlStateNormal];

btn.enabled = NO;

});

timeout--;

}

});

dispatch_resume(_timer);

}

12、隐藏TableView 中多余的行

UIView * view=[[UIView alloc]initWithFrame:CGRectZero];

[_tabelView setTableFooterView:view];

13、UIView添加背景图片

UIImage * image=[UIImage imageNamed:@"friend750"];

headSeV.layer.contents=(id)image.CGImage;

14、UITableView取消选中状态

[tableView deselectRowAtIndexPath:indexPath animated:YES];// 取消选中

15、带属性的字符串

NSFontAttributeName  字体

NSParagraphStyleAttributeName  段落格式

NSForegroundColorAttributeName  字体颜色

NSBackgroundColorAttributeName  背景颜色

NSStrikethroughStyleAttributeName 删除线格式

NSUnderlineStyleAttributeName      下划线格式

NSStrokeColorAttributeName        删除线颜色

NSStrokeWidthAttributeName 删除线宽度

NSShadowAttributeName  阴影

1.  使用实例

UILabel *testLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 100, 320, 30)];

testLabel.backgroundColor = [UIColor lightGrayColor];

testLabel.textAlignment = NSTextAlignmentCenter;

NSMutableAttributedString *AttributedStr = [[NSMutableAttributedString alloc]initWithString:@"今天天气不错呀"];

[AttributedStr addAttribute:NSFontAttributeName

value:[UIFont systemFontOfSize:16.0]

range:NSMakeRange(2, 2)];

[AttributedStr addAttribute:NSForegroundColorAttributeName

value:[UIColor redColor]

range:NSMakeRange(2, 2)];

testLabel.attributedText = AttributedStr;

[self.view addSubview:testLabel];

16、加大按钮的点击范围

把UIButton的frame 设置的大一些,然后给UIButton设置一个小些的图片

[tmpBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 5, 5, 5)];

// 注意这里不能用setBackgroundImage

[tmpBtn setImage:[UIImage imageNamed:@"testBtnImage"] forState:UIControlStateNormal];

17、//避免self的强引用

__weak ViewController *weakSelf = self;

18、//类别的创建

command +n ——Objective-C File———(File Type  选择是类别还是扩展)———(Class  选择为哪个控件写类别)

19、修改UITableview 滚动条颜色的方法

self.tableView.indicatorStyle=UIScrollViewIndicatorStyleWhite;

20、利用UIWebView显示pdf文件

webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];

[webView setDelegate:self];

[webView setScalesPageToFit:YES];

[webViewsetAutoresizingMask:UIViewAutoresizingFlexibleWidth |UIViewAutoresizingFlexibleHeight];

[webView setAllowsInlineMediaPlayback:YES];

[self.view addSubview:webView];

NSString *pdfPath = [[NSBundle mainBundle]pathForResource:@"ojc" ofType:@"pdf"];

NSURL *url = [NSURLfileURLWithPath:pdfPath];

NSURLRequest *request = [NSURLRequestrequestWithURL:url

cachePolicy:NSURLRequestUseProtocolCachePolicy

timeoutInterval:5];

[webView loadRequest:request];

21、将plist文件中的数据赋给数组

NSString *thePath = [[NSBundle mainBundle]pathForResource:@"States" ofType:@"plist"];

NSArray *array = [NSArrayarrayWithContentsOfFile:thePath];

22、隐藏状态栏

[[UIApplication shareApplication]setStatusBarHidden: YES animated:NO];

23、给navigation  Bar  设置title颜色

UIColor *whiteColor = [UIColor whiteColor];

NSDictionary *dic = [NSDictionary dictionaryWithObject:whiteColor forKey:NSForegroundColorAttributeName];

[self.navigationController.navigationBar setTitleTextAttributes:dic];

24、使用AirDrop 进行分享

NSArray *array = @[@"test1", @"test2"];

UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:array applicationActivities:nil];

[self presentViewController:activityVC animated:YES

completion:^{

NSLog(@"Air");

}];

25、把tableview里面Cell的小对勾的颜色改成别的颜色

_mTableView.tintColor = [UIColor redColor];

26、UITableView去掉分割线

_tableView.separatorStyle = NO;

27、正则判断手机号码地址格式

- (BOOL)isMobileNumber:(NSString *)mobileNum {

//    电信号段:133/153/180/181/189/177

//    联通号段:130/131/132/155/156/185/186/145/176

//    移动号段:134/135/136/137/138/139/150/151/152/157/158/159/182/183/184/187/188/147/178

//    虚拟运营商:170

NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|7[06-8])\\d{8}$";

NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];

return [regextestmobile evaluateWithObject:mobileNum];

}

28、控制交易密码位数

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

if (textField.text.length =6){

[MBProgressHUD showMessage:@"密码为6位" afterDelay:1.8];

return NO;

}

return YES;

}

29、判断是不是空

if ([real_name isKindOfClass:[NSNull class]] ) {

return NO;}

30、点击号码拨打电话

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://400966220"]];

31、控制UITabbar的选择哪一个

[self.tabBarController setSelectedIndex:1];

32、获取当前App的版本号

NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];

CFShow(infoDictionary);

// app名称

NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];

// app版本

NSString *app_Version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];

// app build版本

NSString *app_build = [infoDictionary objectForKey:@"CFBundleVersion"];

33、苹果app权限NSPhotoLibraryUsageDescriptionApp需要您的同意,才能访问相册NSCameraUsageDescriptionApp需要您的同意,才能访问相机NSMicrophoneUsageDescriptionApp需要您的同意,才能访问麦克风NSLocationUsageDescriptionApp需要您的同意,才能访问位置NSLocationWhenInUseUsageDescriptionApp需要您的同意,才能在使用期间访问位置NSLocationAlwaysUsageDescriptionApp需要您的同意,才能始终访问位置NSCalendarsUsageDescriptionApp需要您的同意,才能访问日历NSRemindersUsageDescriptionApp需要您的同意,才能访问提醒事项NSMotionUsageDescriptionApp需要您的同意,才能访问运动与健身NSHealthUpdateUsageDescriptionApp需要您的同意,才能访问健康更新NSHealthShareUsageDescriptionApp需要您的同意,才能访问健康分享NSBluetoothPeripheralUsageDescriptionApp需要您的同意,才能访问蓝牙NSAppleMusicUsageDescriptionApp需要您的同意,才能访问媒体资料库

34、控件设置边框

_describText.layer.borderColor = [[UIColor colorWithRed:215.0 / 255.0 green:215.0 / 255.0 blue:215.0 / 255.0 alpha:1] CGColor];

_describText.layer.borderWidth = 1.0;

_describText.layer.cornerRadius = 4.0;

_describText.clipsToBounds = YES;

35、//隐藏电池条的方法

-(BOOL)prefersStatusBarHidden{

return YES;

}

36、延时操作

[NSThread sleepForTimeInterval:2];

方法二:

[self performSelector:@selector(delayMethod) withObject:nil afterDelay:1.5];

37、系统风火轮:

[UIApplication sharedApplication].networkActivityIndicatorVisible = NO; //隐藏

38、//didSelectRowAtIndexPath:方法里面找到当前的Cell

AssessMentCell * cell = [tableView cellForRowAtIndexPath:indexPath];

39、navigation上返回按钮的颜色以及返回按钮后面文字去掉

//返回按钮后边文字去掉

[[UIBarButtonItem appearance] setBackButtonTitlePositionAdjustment:UIOffsetMake(0, -60)

forBarMetrics:UIBarMetricsDefault];

//设置左上角返回按钮的颜色

self.navigationController.navigationBar.tintColor = UIColorFromRGB(0x666666);

40、lineBreakMode //设置文字过长时的显示格式

label.lineBreakMode = NSLineBreakByCharWrapping;以字符为显示单位显

示,后面部分省略不显示。

label.lineBreakMode = NSLineBreakByClipping;剪切与文本宽度相同的内

容长度,后半部分被删除。

label.lineBreakMode = NSLineBreakByTruncatingHead;前面部分文字

以……方式省略,显示尾部文字内容。

label.lineBreakMode = NSLineBreakByTruncatingMiddle;中间的内容

以……方式省略,显示头尾的文字内容。

label.lineBreakMode = NSLineBreakByTruncatingTail;结尾部分的内容

以……方式省略,显示头的文字内容。

label.lineBreakMode = NSLineBreakByWordWrapping;以单词为显示单位显

示,后面部分省略不显示。

当前文章:ios开发实例,ios开发 单例
转载来源:https://www.cdcxhl.com/article22/dsddecc.html

成都网站建设公司_创新互联,为您提供电子商务小程序开发商城网站营销型网站建设建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

成都做网站