解决TTThumbsViewController设置delegate为当前class导致回退按钮消失
今天在做一个照片墙的功能的时候突然发现Three20的一个问题: 我的一个ViewController继承自TTThumbsViewController,这个Controller是由一个UINavigationController通过pushViewController: animated: 方法push出来的。 一开始,一切正常。但是后来我突然间发现UINavigationController的导航按钮消失不见了。 因为在最开始的时候我并没有发现这个问题,直到我把这个功能做的大概七七八八后,在过一次流程的时候才突然发现这个的。 发现UINavigationController的导航按钮消失后,通过注释代码的方法发现了当把TTThumbsViewController的delegate设置为self的时候,导航按钮就消失了。 一开始不得其门道,想不通为嘛当把delegate设置为self后会发生这样的问题,在google半天未见结果的后,决定看下TTThumbsViewController的实现。 在TTThumbsViewController的implementation中发现了以下代码: – (void)setDelegate:(id)delegate { _delegate = delegate; if (_delegate) { self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:[[[UIView alloc] init] autorelease]] autorelease]; self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:TTLocalizedString(@”Done”, @”") style:UIBarButtonItemStyleBordered target:self action:@selector(removeFromSupercontroller)] autorelease]; } } 看到这里就完全明白为什么UINavigationBar的导航按钮会消失不见了,那么要解决这个问题也就简单了,初步想到可以有两种解决方法: 注释掉self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:[[[UIView alloc] init] autorelease]] autorelease];这句。 把TTThumbsViewController的delegate设置为一个其他的class。 因为我的很多代码直接写到了Controller中,所以我就直接采用了第一种方法了。注释掉那句代码后,可爱的UINavigationBar的导航按钮又回来了。 –EOF
Delegate In Csharp 委托(C#)
今天就来说说委托。 委托其实就相当于C/C++中的指向函数的指针,不过C#认为使用委托会更加安全些!MSDN中对于委托的描述是: 委托是一种引用方法的类型。 一旦为委托分配了方法,委托将与该方法具有完全相同的行为。 委托方法的调用可以像其他任何方法一样,具有参数和返回值。 定义一个委托: public delegate int PerformCalculation(int x, int y); 好了,看了前面的,我们来看看怎么使用委托:假如我们有X和Y2个数据,通过不同的运算方法可以得出不同的值,比如: using System; using System.Collections.Generic; using System.Text; class Op { public void result(int x, int y) { Console.WriteLine(“Result is:{0}”,this.op_add(x,y)); } public int op_add(int x, int y) { return x + y; } } class Program { static void Main() { Op op [...]