传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)
本文转载至 http://blog.csdn.net/learnios/article/details/8442201
1. iOS开发中使用[[UIApplication sharedApplication] openURL:] 加载其它应用
在iOS开发中,经常需要调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App。
通过openURL方法可以调用如下应用:
调用浏览器(Safari Browser)
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]];
调用谷歌地图(Google Maps)
- NSString *addressText = @"7 Hanover Square, New York, NY 10004";
- //stringByAddingPercentEscapesUsingEncoding为数据转换,7%20Hanover%20Square%20New%20York%20%NY%2010004
- addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
- NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];
调用邮件客户端(Apple Mail)
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];
拨号(Phone Number)
- [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel://6463777303"]];
调用短信(SMS)
调用应用商店(AppStore)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8"]];
2. NSUserDefaults读取和写入自定义对象
- NSString *string = [NSString stringWithString @"data is here"];
- NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
- [data setObject:string forKey:@"key"];
- NSString *value;
- value = [data objectForKey:"key"];
但是并不是所有的东西都能往里放的。NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary.
3. protocol 和 delegate 回调函数传值
一、说明
1.协议声明了可以被任何类实现的方法
2.协议不是类,它是定义了一个其他对象可以实现的接口
3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
5:委托,就是调用自己定义方法,别的类来实现。
6.新特性说明
@optional预编译指令:表示可以选择实现的方法
@required预编译指令:表示必须强制实现的方法
二、定义
.h
- @protocol ContactCtrlDelegate
- - (void)DismissContactsCtrl;
- - (void)CallBack:(NSString *)str; //回调传值
- @end
- @interface ContactsCtrl : UIViewController {
- id <ContactCtrlDelegate> delegate;
- }
- @property (nonatomic, assign) id <ContactCtrlDelegate> delegate;
.m
- @synthesize delegate;
三、Demo
二级窗口(子窗口)UIViewController subclass
- 1 Textfield
- 1 Button
1、ContactsCtrl.h
- #import <UIKit/UIKit.h>
- //定义协议
- @protocol ContactCtrlDelegate
- - (void)DismissContactsCtrl; //回调关闭窗口
- - (void)CallBack:(NSString *)str; //回调传值
- @end
- @interface ContactsCtrl : UIViewController
- {
- __weak IBOutlet UITextField *passData; //textfield
- id <ContactCtrlDelegate> delegate; //开放delegate
- NSString *passedVal; //从主窗口获取传值
- }
- @property(nonatomic,retain)id <ContactCtrlDelegate> delegate;
- @property(nonatomic,retain)NSString *passedVal;
- - (IBAction)cancelBtn:(id)sender;
- @end
2、ContactsCtrl.m
- @implementation ContactsCtrl
- @synthesize delegate;
- @synthesize passedVal;
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view from its nib.
- passData.text = passedVal;
- }
- //调用协议中的方法
- - (IBAction)cancelBtn:(id)sender
- {
- [delegate CallBack:[NSString stringWithFormat:@"%@",passData.text]];
- [delegate DismissContactsCtrl];
- }
一级窗口(父窗口)
- 1 Textfield
- 1 Button
3、ViewController.h
- #import <UIKit/UIKit.h>
- #import "ContactsCtrl.h" //引入二级文件
- @interface ViewController : UIViewController <ContactCtrlDelegate>
- {
- ContactsCtrl *contactsView; //定义
- __weak IBOutlet UITextField *textfield;
- }
- @property(nonatomic,retain) ContactsCtrl *contactsView;
- - (IBAction)addContactsView:(id)sender;
- @end
4、ViewController.m
- @implementation ViewController
- @synthesize contactsView;
- - (IBAction)addContactsView:(id)sender
- {
- ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:nil bundle:nil];
- self.contactsView = contactView;
- contactsView.delegate = self; //设置委托
- contactsView.passedVal = textfield.text;
- [self presentModalViewController:contactsView animated:YES];
- }
- //实现ContactCtrlDelegate协议中的方法
- - (void)DismissContactsCtrl
- {
- [contactsView dismissModalViewControllerAnimated:YES];
- }
- - (void)CallBack:(NSString *)str
- {
- textfield.text = str;
- }
传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)的更多相关文章
- 学习chrome 插件 DHC ,http请求传参方法
DHC的简介 DHC是一款可以帮助用户使用chrome插件模拟HTTP客户端发送测试数据到服务器的谷歌浏览器插件,在chrome中安装了DHC插件以后,就可在服务器端代码初步完成的时候,使用DHC进行 ...
- jquery-uploadify传参方法
jquery-uploadify传参方法$(document).ready(function () { $("#uploadify").uploadify({ 'uploader' ...
- js方法之间的调用之——传参方法
在最近项目需求中发现,完成一些功能的时候总是要调很多结构类似的方法,写起来很繁琐,所以就想写一个“万能”方法,是的代码更简洁.即:把一个方法作为参数传给这个“万能”方法,让它去执行你给定的方法,就类似 ...
- 定时器setTimeout()的传参方法
更具体的代码:http://www.cnblogs.com/3body/p/5416830.html // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获 ...
- AngularJS中页面传参方法
1.基于ui-router的页面跳转传参 (1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的 ...
- TKinter当Label绑定bind事件时传参方法
记录下tkinter的 当在label绑定bind事件时,遇到需要传参时的解决方法(因为有event存在 所以不能直接传参) https://www.cnblogs.com/liyuanhong/ar ...
- 不用Ajax时的传参方法
不用Ajax时的怎么传参 创建一个form表单 function test(){ var params = { "参数名": "参数值" }; postExce ...
- unittest改写传参方法
Python主要讲究简洁简单使用,所以它不像junit一样支持参数化测试,需要改装一下也可以传参.直接上代码实例 import unittest class ParametrizedTestCase( ...
- tp5闭包子查询传参方法
在channel表中查询status,channel_id,channel_name,account_level这些字段,且这些字段的channel_id不在adv_id为$id的表adv_chann ...
随机推荐
- TocControl控件图层无法显示问题
在窗口里的层层嵌套SplitContainer后,出现最内层SplitContainer内部TocControl控件图层无法显示问题:加载完mxd后代后加上axTOCControl1.SetBuddy ...
- MFC总结之CListCtrl用法及技巧(一)
本文根据本人在项目中的应用,来谈谈CListCtrl的部分用法及技巧.当初学习时,查了很多资料,零零碎碎的作了些记录,现在主要是来做个总结,方便以后查阅.主要包括以下十三点内容:基本操作.获取选中行的 ...
- 计算机的OSI和TCP/IP网络模型
1.计算机的网络模型分为两种OSI模型和TCP/IP模型,它们的对应关系如下: 2.针对OSI模型,每一层都有各自的功能. 应用层 应用层是OSI模型中最靠近用户的一层,负责为用户的应用程序提供网 ...
- tp请求和响应
一.请求参数 use think\Request; 1.获取方法如下: http://w.tp.com/index/index/index/user/AAA $this->request-> ...
- ansible 提示安装sshpass
之前用ansible一直用的root身份.机器之间又早早的做好了ssh信任.所以一直也没有出现什么问题.今天想想自己不能这么浪了,还是用回普通用户吧: 然而马上就遇到了第一个问题,ansible提示安 ...
- Mysql中count(*),DISTINCT的使用方法和效率研究
在处理一个大数据量数据库的时候 突然发现mysql对于count(*)的不同处理会造成不同的结果 比如执行 SELECT count(*) FROM tablename 即使对于千万级别的数据mysq ...
- 5.1 Zend_Log_Writer
22.2.2. 写入到数据库 22.2.3. 踩熄Writer 22.2.4. 測试 Mock 22.2.5. 组合Writers
- C#之用XmlWriter保存XML数据
http://book.51cto.com/art/201012/241202.htm https://blog.csdn.net/hongkaihua1987/article/details/790 ...
- [转]T-SQL_面试题
[转]T-SQL_面试题 2015-05-19 1 创建表插入数据 Student(S#,Sname,Sage,Ssex) 学生表 Course(C#,Cname,T#) 课程表 SC(S#,C#,s ...
- FreeRTOS 任务计数信号量,任务二值信号量,任务事件标志组,任务消息邮箱
以下基础内容转载自安富莱电子: http://forum.armfly.com/forum.php 本章节为大家讲解 FreeRTOS 计数信号量的另一种实现方式----基于任务通知(Task Not ...