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];
}
4

1 回答 1

0

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

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

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

于 2013-03-04T00:10:03.663 回答