2013-08-05 53 views
0

我试图编写一个简单的自定义委托来显示多个选择列表(在引用各种在线教程,stackoverflow,Apple doc之后),但是在我想使用委托的类中,当我运行它时,我设置代理的行会进入无限循环。设置(自定义)委托运行到无限循环

我分享了这里的源代码 https://bitbucket.org/ikosmik/uilistviewcontroller/src/ddfcd140b52e6e59d84e58d34d601f8f850145a1/UIList?at=master

UIListViewController(这里我声明协议) https://bitbucket.org/ikosmik/uilistviewcontroller/src/ddfcd140b52e6e59d84e58d34d601f8f850145a1/UIList/UIListViewController.h?at=master

,我试图使用委托在一个UIViewController称为View_Exporter

#import <UIKit/UIKit.h> 
#import "UIListViewController.h" 

@interface View_Exporter : UIViewController <UIListViewDelegate, UIListViewDataSource> 

    @property (nonatomic, strong) IBOutlet UIView *viewForList; 
    @property (nonatomic, strong) UIListViewController *listViewController; 

@end 

View_Exporter.m

#import "View_Exporter.h" 


@implementation View_Exporter 

@synthesize arraySelectedList; 
@synthesize viewForList; 
@synthesize listViewController; 



#pragma mark - UIListViewController Methods 

-(NSArray *) itemsForList { 
    NSLog(@"View_Exporter itemsForList"); 
    NSArray *array = [NSArray arrayWithObjects:@"Server", @"Memory", nil]; 
    return array; 
} 


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{ 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    self.listViewController = [[UIListViewController alloc] initWithNibName:@"UIListViewController" bundle:nil]; 
    self.listViewController.listViewDelegate = self; 
    //[self.viewForList addSubview:self.listViewController.view]; 
    self.listViewController.listViewDataSource = self; 

} 

@end 

但是这条线在viewDidLoad中似乎无限循环,当我运行代码:

self.listViewController.listViewDelegate = self; 

为什么会这样无限循环?从昨天开始,这件事让我感到头痛。不知道哪里出错。有人可以帮忙吗?

+1

如果您的代码处于无限循环中,请使用调试器的暂停按钮或断点停止它,然后遍历代码以查看发生了什么。而且你无法命名自己的以UI开头的类,这是Apple的保留前缀,你可能会与私有API冲突。 – jrturton

+0

让我用@jrturton调试器进行检查!谢谢你的评论。你说的话很有道理 - 我会改变用户界面的前缀。 – Jean

回答

2

你已经写了一个定制的setter为listViewDelegate,在这个方法的末尾,你这样做:

self.listViewDelegate = delegate; 

这只是再次调用setter方法。通过self.访问属性只是一种调用[self setXX:xxx]的方式。在你的访问方法,你需要的实例变量直接设置,在正常情况下,这只是

_delegate = delegate; 

(该_delegate实例变量自动为您创建)。您可以安全地删除所有的合成语句,它们不再需要。

+0

感谢一吨,@ jrturton!有用!我没有意识到self.listViewDelegate = delegate之间的区别;和_delegate =委托; !我完全忘记了我已经明确添加了setter!再次感谢! – Jean

+0

非常感谢! –