我正在启动一个应该绘制两个按钮(添加和删除按钮)的 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];
}