我正在啓動應該繪製兩個按鈕(添加和刪除按鈕)的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];
}
經過進一步測試後,在添加子視圖後,此UIViewController中的所有屬性都將設置爲null。這真的沒有意義。 – anthony 2012-04-23 00:01:57