UIPickerview AppDelegate子视图的问题

时间:2011-06-16 19:47:48

标签: iphone objective-c xcode cocoa-touch uiview

子视图问题但是它可能与AppDelegate有关,将UIPickerView放在myView中,与self.view相关联?

由于

[self.view addSubview:pickerView]; <-- works ok
//[myView addSubview:pickerView];<-- fails, can't get dial movement or response

AppDelegate.h

 #import <UIKit/UIKit.h>
@class PickerViewController;
@interface PickerViewAppDelegate : NSObject <UIApplicationDelegate> {
    UIWindow *window;
    PickerViewController *pvController;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@end

AppDelegate.m

#import "PickerViewAppDelegate.h"
#import "PickerViewController.h"
@implementation PickerViewAppDelegate
@synthesize window;
- (void)applicationDidFinishLaunching:(UIApplication *)application {    
    pvController = [[PickerViewController alloc] initWithNibName:@"PickerView"    bundle:[NSBundle mainBundle]];
    [window addSubview:pvController.view];
     // Override point for customization after application launch
    [window makeKeyAndVisible];
}
- (void)dealloc {
    [pvController release];
    [window release];
    [super dealloc];
}
@end

或者Controller.h

#import <UIKit/UIKit.h>
@interface PickerViewController : UIViewController <UIPickerViewDataSource,     UIPickerViewDelegate> {
    IBOutlet UIPickerView *pickerView;
    NSMutableArray *arrayColors;
}
@end

Controller.m或者

#import "PickerViewController.h"
@implementation PickerViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    UIView *myView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
    [myView setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:myView];
    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0.0, 0.0, 100.0, 100.0)];
    pickerView.delegate = self;
    pickerView.showsSelectionIndicator = YES;
    arrayColors = [[NSMutableArray alloc] init];

    for (int i=0; i<60; i++) {
        [arrayColors addObject:[NSString stringWithFormat:@"%d", i]];
    }
    //[self.view addSubview:pickerView]; // <-- works
    [myView addSubview:pickerView]; // <-- this fails.. this is what I need for multiple views
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [arrayColors release];
    [super dealloc];
}

#pragma mark -
#pragma mark Picker View Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView {
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component {
    return [arrayColors count];
}

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    return [arrayColors objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"Selected Color: %@. Index of selected color: %i", [arrayColors objectAtIndex:row], row);
}
@end

2 个答案:

答案 0 :(得分:1)

如果您发表评论

    [myView addSubview:pickerView];

你会理解,myView框架太小了。您将能够仅与触摸父视图框的选择器进行交互。如果要使拾取器表现正常,则必须将父视图框设置为包含拾取器框架 - 使其等于或大于。

请记住iOs人机界面指南,

  

选择器的整体尺寸(包括其背景)固定为与iPhone上的键盘相同的尺寸。

答案 1 :(得分:0)

确保myView的userInteractionEnabled设置为YES