添加子视图后,属性为null

时间:2012-04-22 21:37:44

标签: objective-c uipopovercontroller addsubview

我正在启动一个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];
}

1 个答案:

答案 0 :(得分:0)

如何声明jackName属性?我的猜测是它是一个弱参考。

我有一个类似的问题,在视图作为子视图添加后,视图的弱引用被重置。我的理解是,只有在存在潜在的保留周期时才会使用弱引用(例如,您有一个背包,其中包含计算器并且计算器指向背包 - 请参阅Big Nerd Ranch book)。

我不太清楚为什么这是一个问题,但我遇到了类似的东西,并认为我会分享。