【发布时间】:2012-08-20 21:26:06
【问题描述】:
我有一个奇怪的问题,我试图将自定义 UIView 对象设置为具有一个委托,该委托是一个自定义选项卡栏控制器。选项卡栏控制器被设置为代表,我很肯定它不是零,而且它实际上已设置!但是稍后当自定义 UIView 执行其任务时,委托为 nil。
我正在使用 ARC,并且我确保对象很强大,即使有问题的标签栏位于 UINavBar 内,所以它不应该被释放...
我开始认为 UITabBar 不可能真正被释放,因为它显然仍然存在......我也很确定我正在以适当的方式将它设置为委托。
我将委托设置在 UITabViewController 持有的其中一个 ViewController 的 ViewDidLoad 中。 这是我设置委托的方式:
- (void)viewDidLoad
{
[super viewDidLoad];
customSearchBar.delegate = (id<SearchResultReceiver>)self.tabBarController;
//customSearchBar.delegate IS SET, btw tabBarController implements SearchResultReceiver
...
}
正如我所说,id 委托具有属性(强、非原子),即使我认为它不需要强。似乎它应该很弱,因为 TabBarController 已经由 NavigationBarController 在其他地方拥有,但当我看到这个问题时,我仍然希望安全。
这里是delegate为nil的代码,现在我们在CustomSearchBar.m 不确定它是否重要,当然您已经猜到了,但是... CustomSearchBar.m 是一个 UITextFieldDelegate。
以下代码由处理 url 请求并将传入/传出 jsons 转换为字典的 Communicator 对象调用,它工作正常。假设以下是由 -(void)connectionDidFinishLoading:(NSURLConnection *)connection 调用的...
-(void)receiveData:(NSDictionary *)dict forMode:(int)mode
{
[self.delegate receiveSearchResult:dict];
[spinner stopAnimating];
[searchConnection cancel];
}
现在委托是零。我没有在代码中的其他地方请求 customSearchBar 的委托。
附加信息:
CustomSearchBar 扩展 UIView 并且是拥有它的 ViewController 的 IBOutlet。该 ViewController 归我尝试设置为委托的 TabBarController 所有。
感谢您抽出宝贵时间,如果我能提供更多详细信息,请告诉我。不知道为什么会出现问题会导致表现不佳:'( 抱歉!
这里有一些关于委托合成的更多信息......
#import <UIKit/UIKit.h>
#import "Communicator.h"
@protocol SearchResultReceiver <NSObject>
@optional
-(void) receiveSearchResult:(NSDictionary *) dict;
@end
@interface CustomSearchBar : UIView <UITextFieldDelegate, DataReceiver>
{
__strong id <SearchResultReceiver> delegate;
}
@property (strong, nonatomic) id<SearchResultReceiver> delegate;
...
在 .m 中我做 @synthesize 委托;简单的
【问题讨论】:
-
请包括您的委托属性的定义和任何综合语句。请注意,在访问属性时,您应该使用点语法,例如 self.delegate,而不是委托。
-
谢谢。我更新了点符号,发现在 receiveData 阶段它仍然为零。其余信息我将很快提供。
标签: iphone xcode delegates uitabbarcontroller automatic-ref-counting