本文转载至  http://blog.csdn.net/learnios/article/details/8442201

分类: 功能模块2012-12-27 10:22 109人阅读 评论(0) 收藏 举报

1. iOS开发中使用[[UIApplication sharedApplication] openURL:] 加载其它应用

在iOS开发中,经常需要调用其它App,如拨打电话、发送邮件等。UIApplication:openURL:方法是实现这一目的的最简单方法,该方法一般通过提供的url参数的模式来调用不同的App。

通过openURL方法可以调用如下应用:

调用浏览器(Safari Browser)

  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http:google.com"]];

调用谷歌地图(Google Maps)

  1. NSString *addressText = @"7 Hanover Square, New York, NY 10004";
  2. //stringByAddingPercentEscapesUsingEncoding为数据转换,7%20Hanover%20Square%20New%20York%20%NY%2010004
  3. addressText = [addressText stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
  4. NSString* urlText = [NSString stringWithFormat:@"http://maps.google.com/maps?q=%@", addressText];
  5. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlText]];

调用邮件客户端(Apple Mail)

  1. [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto://devprograms@apple.com"]];

拨号(Phone Number)

  1. [[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读取和写入自定义对象

  1. NSString *string = [NSString stringWithString @"data is here"];
  2. NSUserDefaults *data = [NSUserDefaults standardUserDefaults];
  3. [data setObject:string forKey:@"key"];
  4. NSString *value;
  5. value = [data objectForKey:"key"];

但是并不是所有的东西都能往里放的。NSUserDefaults只支持: NSString, NSNumber, NSDate, NSArray, NSDictionary.

3. protocol 和 delegate 回调函数传值

一、说明
  1.协议声明了可以被任何类实现的方法
  2.协议不是类,它是定义了一个其他对象可以实现的接口
  3.如果在某个类中实现了协议中的某个方法,也就是这个类实现了那个协议。
  4.协议经常用来实现委托对象。一个委托对象是一种用来协同或者代表其他对象的特殊对象。
  5:委托,就是调用自己定义方法,别的类来实现。
  6.新特性说明
    @optional预编译指令:表示可以选择实现的方法
    @required预编译指令:表示必须强制实现的方法

二、定义

.h

  1. @protocol ContactCtrlDelegate
  2. - (void)DismissContactsCtrl;
  3. - (void)CallBack:(NSString *)str; //回调传值
  4. @end
  5. @interface ContactsCtrl : UIViewController {
  6. id <ContactCtrlDelegate> delegate;
  7. }
  8. @property (nonatomic, assign) id <ContactCtrlDelegate> delegate;

.m

  1. @synthesize delegate;

三、Demo

二级窗口(子窗口)UIViewController subclass

  • 1 Textfield
  • 1 Button

1、ContactsCtrl.h

  1. #import <UIKit/UIKit.h>
  2. //定义协议
  3. @protocol ContactCtrlDelegate
  4. - (void)DismissContactsCtrl;      //回调关闭窗口
  5. - (void)CallBack:(NSString *)str; //回调传值
  6. @end
  7. @interface ContactsCtrl : UIViewController
  8. {
  9. __weak IBOutlet UITextField *passData; //textfield
  10. id <ContactCtrlDelegate> delegate;     //开放delegate
  11. NSString *passedVal;                   //从主窗口获取传值
  12. }
  13. @property(nonatomic,retain)id <ContactCtrlDelegate> delegate;
  14. @property(nonatomic,retain)NSString *passedVal;
  15. - (IBAction)cancelBtn:(id)sender;
  16. @end

2、ContactsCtrl.m

  1. @implementation ContactsCtrl
  2. @synthesize delegate;
  3. @synthesize passedVal;
  4. - (void)viewDidLoad
  5. {
  6. [super viewDidLoad];
  7. // Do any additional setup after loading the view from its nib.
  8. passData.text = passedVal;
  9. }
  10. //调用协议中的方法
  11. - (IBAction)cancelBtn:(id)sender
  12. {
  13. [delegate CallBack:[NSString stringWithFormat:@"%@",passData.text]];
  14. [delegate DismissContactsCtrl];
  15. }

一级窗口(父窗口)

  • 1 Textfield
  • 1 Button

3、ViewController.h

  1. #import <UIKit/UIKit.h>
  2. #import "ContactsCtrl.h"  //引入二级文件
  3. @interface ViewController : UIViewController <ContactCtrlDelegate>
  4. {
  5. ContactsCtrl *contactsView;  //定义
  6. __weak IBOutlet UITextField *textfield;
  7. }
  8. @property(nonatomic,retain) ContactsCtrl *contactsView;
  9. - (IBAction)addContactsView:(id)sender;
  10. @end

4、ViewController.m

  1. @implementation ViewController
  2. @synthesize contactsView;
  3. - (IBAction)addContactsView:(id)sender
  4. {
  5. ContactsCtrl *contactView = [[ContactsCtrl alloc] initWithNibName:nil bundle:nil];
  6. self.contactsView = contactView;
  7. contactsView.delegate = self;  //设置委托
  8. contactsView.passedVal = textfield.text;
  9. [self presentModalViewController:contactsView animated:YES];
  10. }
  11. //实现ContactCtrlDelegate协议中的方法
  12. - (void)DismissContactsCtrl
  13. {
  14. [contactsView dismissModalViewControllerAnimated:YES];
  15. }
  16. - (void)CallBack:(NSString *)str
  17. {
  18. textfield.text = str;
  19. }

传参方法:sharedApplication, NSUserDefaults, protocol 和 delegate(实例)的更多相关文章

  1. 学习chrome 插件 DHC ,http请求传参方法

    DHC的简介 DHC是一款可以帮助用户使用chrome插件模拟HTTP客户端发送测试数据到服务器的谷歌浏览器插件,在chrome中安装了DHC插件以后,就可在服务器端代码初步完成的时候,使用DHC进行 ...

  2. jquery-uploadify传参方法

    jquery-uploadify传参方法$(document).ready(function () { $("#uploadify").uploadify({ 'uploader' ...

  3. js方法之间的调用之——传参方法

    在最近项目需求中发现,完成一些功能的时候总是要调很多结构类似的方法,写起来很繁琐,所以就想写一个“万能”方法,是的代码更简洁.即:把一个方法作为参数传给这个“万能”方法,让它去执行你给定的方法,就类似 ...

  4. 定时器setTimeout()的传参方法

    更具体的代码:http://www.cnblogs.com/3body/p/5416830.html // 由于setTimeout()的延迟执行特性,所以在执行的函数中直接使用外部函数的变量是无法获 ...

  5. AngularJS中页面传参方法

    1.基于ui-router的页面跳转传参 (1) 用ui-router定义路由,比如有两个页面,一个页面(producers.html)放置了多个producers,点击其中一个目标,页面跳转到对应的 ...

  6. TKinter当Label绑定bind事件时传参方法

    记录下tkinter的 当在label绑定bind事件时,遇到需要传参时的解决方法(因为有event存在 所以不能直接传参) https://www.cnblogs.com/liyuanhong/ar ...

  7. 不用Ajax时的传参方法

    不用Ajax时的怎么传参 创建一个form表单 function test(){ var params = { "参数名": "参数值" }; postExce ...

  8. unittest改写传参方法

    Python主要讲究简洁简单使用,所以它不像junit一样支持参数化测试,需要改装一下也可以传参.直接上代码实例 import unittest class ParametrizedTestCase( ...

  9. tp5闭包子查询传参方法

    在channel表中查询status,channel_id,channel_name,account_level这些字段,且这些字段的channel_id不在adv_id为$id的表adv_chann ...

随机推荐

  1. mysql root用户密码个性

    对名为“mysql”数据库下的表“user”进行操作如下语句:update user set password=PASSWORD("your_password") where us ...

  2. 使用rman备份异机恢复数据库

    一.RMAN备份源库注意点: 最好保留rman备份日志 $rman target / log=backup.log RMAN>run { allocate channel t1 type dis ...

  3. Lintcode---把排序树组转换为高度最小的二叉树

    给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树. 注意事项 There may exist multiple valid solutions, return any of them. ...

  4. SQL相关路径查询脚本

    --1.查询机器名 SELECT @@servername AS 机器名称 --查询已安装的SQL实例名 SELECT * FROM Sys.Servers --2.查询SQL安装路径 DECLARE ...

  5. (void)0和0的区别及用法

    (void)0相当于宏NULL,NULL本身的含义为“空”,在c语言代表“不存在.不确定”的含义. 0不能简单的理解为“没有”的意思,在c语言及二进制中,0和1代表的是“一件事物的正反两个方面“,0是 ...

  6. 输入LPCWSTR类型字符串

    LPCWSTR tmp = L"xxx"; char*转到LPCWSTR LPCSTR(charTmp)

  7. Java线程中断理解(interrupte)

    Java线程之中,一个线程的生命周期分为:初始.就绪.运行.阻塞以及结束.当然,其中也可以有四种状态,初始.就绪.运行以及结束. 一般而言,可能有三种原因引起阻塞:等待阻塞.同步阻塞以及其他阻塞(睡眠 ...

  8. 关于C与python交互设想及文章汇总

    gui: gtk by c 后台: python Python实例浅谈之三Python与C/C++相互调用 Python 与 C/C++ 交互的几种方式 C语言 + GTK3+ Visual Stud ...

  9. Android——Activity恢复用户用EditText输入的数据

    说明: 在横屏输入的内容,在Activity销毁后,即横屏后,获取用户输入的内容 步骤: 1.在xml页面定义EditText的id 2.用onSaveInstanceState保存用户输入的数据 ( ...

  10. crontab中运行python程序出错,提示ImportError: No module named解决全过程

    将一个python脚本放入crontab执行时,提示如下错:ImportError: No module named hashlib但是在shell中直接执行时没有任何问题,google之后,得到线索 ...