delegate

oc NSDate、类的扩展 、代理(家庭-保姆)

自作多情 提交于 2019-12-04 16:48:48
⼀、 NSDate 1、⺴络请求中的时间戳 计算当前时间距离 1970年1⽉1⽇ 的 秒 数 2、关于时区 24 个时区 北京 东八区 NSDate 输出的永远是 0 时区的时间 英国 格林尼治时间 GMT Greenwich Mean Time 3、NSDate使用 // 创建日期:表示当前的时间 NSDate * nowDate = [ NSDate date ]; // 创建日期:以当前时间为参考点,创建表示昨天这个时间的日期对象 double oneDay = 24 * 3600 ; NSDate * yesterday = [ NSDate dateWithTimeIntervalSinceNow :-oneDay]; NSDate * since2001 = [ NSDate dateWithTimeIntervalSinceReferenceDate : 0 ]; NSDate * since1970 = [ NSDate dateWithTimeIntervalSince1970 : 0 ]; NSDate * sinceAnyDay = [ NSDate dateWithTimeInterval :oneDay sinceDate :since2001]; // NSTimeInterval 是 double 类型 NSTimeInterval interval

Protocol & Delegate

拟墨画扇 提交于 2019-12-04 16:44:59
Protocol 在Objective-C 中protocol 就是一個很多 method 的宣告集合的地方,感覺上和interface 有點像而和interface的差別在於protocol 裡面不會有變數的宣告。我們直接來看一個例子。 @protocol Omniprinter -(void) printInt:(int ) intVar; -(void) printObj:(NSString *) obj; @end 這個例子就是,我們宣告了一個 protocol 叫 Omniprinter 前面需要加上關鍵字@protocol,這個protocol 裡面就只有 printInt: 和 printObj: 這兩個 method。Protocol 就這樣簡單地宣告完成了。如果是要接受或叫遵循這個 protocol 的 class就要在@interface這樣寫。 @interface Hello: NSObject <Omniprinter> { } @end 這個就代表 Hello 這個 Class 有採用 Omniprinter 所定義的 method 。如果一次要採用很多個 protocol 就用 , 逗點分開每個 protocol 的名字,比如。 @interface Hello: NSObject <Omniprinter, protocol1, protocol2

IOS 如何选择delegate、notification、KVO?

僤鯓⒐⒋嵵緔 提交于 2019-12-04 03:55:51
前面分别讲了delegate、notification和KVO的实现原理,以及实际使用步骤,我们心中不禁有个疑问,他们的功能比较类似,那么在实际的编程中,如何选择这些方式呢? 在网上看到一个博客上详细的分析了三者之间的区别以及各自的优势,博文地址为 http://blog.shinetech.com/2011/06/14/delegation-notification-and-observation/ ,因为博文是用英文写的,下面将其翻译成中文。 在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信。在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation; 2.通知中心Notification Center; 3.键值观察key value observing,KVO 因此,那为什么我们需要这些模式以及什么时候用它以及什么时候不用它。 下面完全根据我的开发经验来讨论这三中模式。我将讨论为什么我觉得某种模式要好于另外一种模式以及为什么我觉得在一定的环境下某中模式比较好。我给出的这些原因并不是圣经,而仅仅是个人观点。如果你有什么不同的观点或者还可以进行补充的地方,可以联系我,一起讨论。 上面的三种模式是什么? 三种模式都是一个对象传递事件给另外一个对象,并且不要他们有耦合

iOS 如何选择delegate、notification、KVO?

跟風遠走 提交于 2019-12-04 03:55:39
delegate、notification和KVO的功能比较类似,那么在实际的编程中,如何选择这些方式呢? 在网上看到一个博客上详细的分析了三者之间的区别以及各自的优势,博文地址为 http://blog.shinetech.com/2011/06/14/delegation-notification-and-observation/ ,因为博文是用英文写的,下面将其翻译成中文。 在开发ios应用的时候,我们会经常遇到一个常见的问题:在不过分耦合的前提下,controllers间怎么进行通信。在IOS应用不断的出现三种模式来实现这种通信: 1.委托delegation; 2.通知中心Notification Center; 3.键值观察key value observing,KVO 因此,那为什么我们需要这些模式以及什么时候用它以及什么时候不用它。 下面完全根据我的开发经验来讨论这三中模式。我将讨论为什么我觉得某种模式要好于另外一种模式以及为什么我觉得在一定的环境下某中模式比较好。我给出的这 些原因并不是圣经,而仅仅是个人观点。如果你有什么不同的观点或者还可以进行补充的地方,可以联系我,一起讨论。 上面的三种模式是什么? 三种模式都是一个对象传递事件给另外一个对象,并且不要他们有耦合。三种模式都是对象来通知某个事件发生了的方法,或者更准确的说,是允许其他的对象收到 这种事件的方法

ios delegate你必須知道的事情

人盡茶涼 提交于 2019-12-03 16:43:57
當你開始寫iOS程式不久,應該開始面對到很多的delegate, 不管是用別人的library或是自己寫library,可能都逃不了delegate。 為了怕有些人不知道什麼是delegate,在這邊簡單的介紹一下, delegate中文叫做委託,通常會用在class內部把一些事件處理"委託"給別人去完成。 舉個例子,XML Parser可能他知道怎麼parse xml,但是parse到的東西要怎麼處理xml parser可能不知道。 所以NSXMLParser就提供了一個NSXMLParserDelegate給client去實作, 當parse到某個element的時候,就callback delegate所定義的message, 讓他client自己去決定怎麼去處理這個element。 好吧,我承認我解釋的很模糊,不過我這篇本來就不是要你搞懂什麼是delegate, 而是針對使用或是設計delegate的時候,可能會要注意的事情。 在我們的class中設計delegate的時候,我們通常會有幾個注意事項。 假設我的class叫做MyClass,那我們可能會有定義一個MyClassDelegate這個protocol當作我的delegate protocol。 而MyClass中我們可能是這樣寫。 @protocol MyClassDelegate <NSObject> -

Generate a C# delegate method stub

匿名 (未验证) 提交于 2019-12-03 10:24:21
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Anyone know how to automatically create a delegate stub method? In WPF it seems im constantly having to pass delegates around. i would like to be able to type a method name that doesnt exist and have a method stub automatically generated...currently i'am having to constantly reference the docs to get the delegate signature and then manually create method with matching signature. 回答1: Use an IDE plugin like Refactor Pro! It also allows you to convert your delegates to instance methods, or if its a one-liner, into a lambda. I tend to

Custom Delegate Example in Objective-c

匿名 (未验证) 提交于 2019-12-03 09:18:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I understand using delegates in associated with other objects (i.e. UITextField etc.) But I am trying to find a simple example of setting up / using a custom delegate independently. Any help / pointers would be much appreciated. My understanding so far is: #import <Foundation/Foundation.h> @class TheBox; @protocol TheBoxDelegate <NSObject> -(void)theBoxDelegateWillDoSomething:(TheBox *)theBox; @end @interface TheBox : NSObject { id <TheBoxDelegate> delegate; } @property(assign) id <TheBoxDelegate> delegate; @end . #import "TheBox.h"

NSURLConnection Delegate Methods Not Called In Generic Class

匿名 (未验证) 提交于 2019-12-03 09:18:39
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have a test class that tries to reach to google with a NSURLConnection . If I try to make it generic, the NSURLConnectionDataDelegate methods are never called. class Remote < T : NSObject >: NSObject , NSURLConnectionDelegate , NSURLConnectionDataDelegate { //class Remote: NSObject, NSURLConnectionDelegate, NSURLConnectionDataDelegate { var data = NSMutableData () func connect ( query : NSString ) { var url = NSURL ( string : "http://www.google.com" )! var request = NSURLRequest ( URL : url ) var conn = NSURLConnection ( request

C# Catching Exception From Invoked Delegate on another Thread

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I have some code as follows. This is running on "Thread 2" WebBrowser browser = this.webBrowser browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("somebutton").InvokeMember("click"); })); Thread.Sleep(500); browser.Invoke(new MethodInvoker(delegate { browser.Document.GetElementById("username").SetAttribute("value", username); })); //folowed by several more similar statments Essentially I am Invoking some methods on a WebBrowser control created on a different thread, "Thread 1". If the element on the current page

“Delegate to an instance method cannot have null 'this' ” while forwarding an event

匿名 (未验证) 提交于 2019-12-03 09:02:45
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Im trying to forward an event OnClientMessage from my class Client over the class Server to outside my libary. Client.cs public class Client { private TcpClient tcpClient ; private StreamWriter writer ; private Boolean alive = true ; private int id ; public delegate void OnClientMessageHandler ( Client sender , String message ); public delegate void OnClientDisconnectHandler ( Client sender ); public event OnClientMessageHandler OnClientMessage ; public event OnClientDisconnectHandler OnClientDisconnect ; public Client ( TcpClient