2012-04-22 14 views
0

2つのボタン(追加ボタンと削除ボタン)を描画するはずのUIPopoverViewControllerを起動します。 UIPopoverのContentViewControllerには、UIPopoverViewControllerの起動直前に設定されたoutputJackViewというプロパティがあります。このプロパティは、ボタンを適切に描画するために必要です。問題は、最初のボタンがサブビューとして追加された直後です。outputJackViewは何とかnullに設定されています。ここでサブビューを追加した後にプロパティが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); 
} 

最後の2つのNSLogは最初のもの(前)に正しく名前を返し、2番目の(後ろ)にnullを返します。 jackNameプロパティはNSStringです。サブビューが追加された後、プロパティがnullに設定されていることは明らかですが、なぜそれが起こるのか理解できません。

- (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

答えて

0

jackNameプロパティが宣言されている方法:ここで

は、重要な場合にUIPopoverViewControllerを起動するクラスのメソッドのですか?私の推測は弱い参照だということです。

ビューがサブビューとして追加された後、ビューの弱い参照がリセットされたのと同様の問題がありました。私の理解では、潜在的な保持サイクルがある場合にのみ、弱い参照を使用する必要があります(たとえば、電卓への参照を含むバックパックがあり、電卓はバックパックを指す - Big Nerd Ranch bookを参照)。

私はそれがなぜここで問題になるのかよく分かりませんが、私は似たようなことに遭遇し、分かち合うと思いました。

関連する問題