2013-08-05 54 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

非常感謝! –