2012-04-22 58 views
0

我正在啓動應該繪製兩個按鈕(添加和刪除按鈕)的UIPopoverViewController。 UIPopover的ContentViewController具有一個名爲outputJackView的屬性,它在UIPopoverViewController啓動之前設置。該屬性對於按鈕正確繪製是必需的。在將第一個按鈕添加爲子視圖後,問題就是正確的,outputJackView設置爲null。添加子視圖後屬性爲空

這裏是UIPopoverViewController的ContentViewController:

CableConnectionMenuController.h

#import <UIKit/UIKit.h> 
@class JackView; 

@interface CableConnectionMenuController : UIViewController 
{ 
JackView *outputJackView; 
} 

@property (nonatomic, weak) id <CableConnectionDelegate> delegate; 
@property (nonatomic, strong) JackView *outputJackView; 

- (void)setButtonTextWithOutputJack:(JackView *)outputJack withInputArray:(NSMutableArray *)inputArray; 
- (void)createAddConnectionButton; 
- (void)createDeleteConnectionButton; 
@end 

CableConnectionMenuController.m

#import "CableConnectionMenuController.h" 
#import "JackView.h" 
#import "CableDisconnectButton.h" 

@implementation CableConnectionMenuController 

@synthesize delegate; 
@synthesize outputJackView; 

...

- (void)viewDidLoad 
{ 
[super viewDidLoad]; 

//alloc output jack view 
self.outputJackView = [[JackView alloc] init]; 

//set size of popover view in cables view 
self.contentSizeForViewInPopover = CGSizeMake(200, 200); 

//change view background color 
self.view.backgroundColor = [UIColor whiteColor]; 
} 

//this method is called from the class that launches the UIPopoverViewController 
- (void)setButtonTextWithOutputJack:(JackView *)outputJack withInputArray:(NSMutableArray *)inputArray 
{ 
//set output jack which will be the same for all inputs 
self.outputJackView = outputJack; 

//draw add connection button 
[self createAddConnectionButton]; 

//draw delete connection button - not working 
//[self createDeleteConnectionButton]; 
} 

- (void)createAddConnectionButton 
{ 
CableDisconnectButton *addConnectionButton = [CableDisconnectButton buttonWithType:UIButtonTypeCustom]; 
addConnectionButton.frame = CGRectMake(0, 0, 190, 40); 
[addConnectionButton setBackgroundImage:[UIImage imageNamed:@"images/cable_connect_button.png"] forState:UIControlStateNormal]; 
[addConnectionButton setBackgroundImage:[UIImage imageNamed:@"images/cable_connect_button_over.png"] forState:UIControlStateHighlighted]; 

//add output jack 
addConnectionButton.outputJack = self.outputJackView; 

//add action to button 
[addConnectionButton addTarget:self action:@selector(addConnectionButtonTarget:) forControlEvents:UIControlEventTouchUpInside]; 

NSLog(@"output jack name before: %@", self.outputJackView.jackName); 

[self.view addSubview:addConnectionButton]; 

NSLog(@"output jack name after: %@", self.outputJackView.jackName); 
} 

兩個NSLog在最後在第一個(之前)正確返回名稱,並在第二個(之後)返回null。 jackName屬性是NSString的。很顯然,在添加子視圖後,該屬性被設置爲null,但我無法弄清楚爲什麼會發生這種情況。

下面是從類啓動UIPopoverViewController的情況下,重要的方法:

- (void)editCableConnectionsWith:(JackView *)outputJack 
{ 
//launches the note menu popover 
self.cableConnectionMenuController = [[CableConnectionMenuController alloc] init]; 
self.cableConnectionMenuController.delegate = (id)self; 

//find appropriate connection to edit 
for (JackView *currentJack in jackArray) 
{ 
    if (currentJack == outputJack) 
    { 
     //create temp array of input jacks to send to cable connection controller 
     NSMutableArray *inputButtonTempArray = [self returnInputJackArrayWithOutputJack:currentJack]; 

     //set information for creating disconnect buttons in popover 
     [self.cableConnectionMenuController setButtonTextWithOutputJack:currentJack withInputArray:inputButtonTempArray]; 
    } 
} 

self.editConnectionsPopoverController = [[UIPopoverController alloc] initWithContentViewController:self.cableConnectionMenuController]; 

[self.editConnectionsPopoverController presentPopoverFromRect:pulseRing.frame inView:self permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES]; 
} 
+0

經過進一步測試後,在添加子視圖後,此UIViewController中的所有屬性都將設置爲null。這真的沒有意義。 – anthony 2012-04-23 00:01:57

回答

0

是如何jackName財產申報?我的猜測是這是一個薄弱的參考。

我有一個類似的問題,視圖上的弱引用在視圖添加爲子視圖後被重置。我的理解是,只有在存在潛在的保留週期時纔可以使用弱參考(例如,您有一個參考計算器的揹包,計算器指向揹包 - 請參閱Big Nerd Ranch book)。

我不太清楚爲什麼這是一個問題在這裏,但我遇到了類似的東西,並認爲我會分享。

相關問題