设置(自定义)委托进入无限循环

时间:2013-08-05 06:06:44

标签: ios objective-c

我试图编写一个简单的自定义委托来显示多个选择列表(在引用各种在线教程,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

我正在尝试在名为View_Exporter

的UIViewController中使用该委托
#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 个答案:

答案 0 :(得分:2)

您已为listViewDelegate编写了自定义setter,在此方法结束时您执行此操作:

self.listViewDelegate = delegate;

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

_delegate = delegate;

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