【问题标题】:UIPickerview AppDelegate problem with subviewsUIPickerview AppDelegate 子视图的问题
【发布时间】:2011-09-16 16:30:17
【问题描述】:

子视图的问题,但它可能与 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

控制器.h

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

控制器.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

【问题讨论】:

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


    【解决方案1】:

    如果你评论

        [myView addSubview:pickerView];
    

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

    请记住 iOs 人机界面指南,

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

    【讨论】:

    • 我想做的一件大事,但它可能违反了 IOS HIG,是将 4 个 UIPickerView 并排放置,但通过将它们放在 UIView 中来调整它们的大小。这可以从 Interface Builder 执行,但从代码执行,UIPickerView 不会改变大小。其他人一定是这样做的,因为我看到 4 个表盘彼此相邻,除非 UIPickerView 中有设置做 4 个表盘???
    • @jdl,使用 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView - (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component - (CGFloat)pickerView:(UIPickerView *) pickerView widthForComponent:(NSInteger) 组件在单个选择器中获取多行。
    • 再次感谢您...成功了!
    【解决方案2】:

    确保 myView 已将 userInteractionEnabled 设置为 YES

    【讨论】:

    • [myView addSubview:pickerView]; myView.userInteractionEnabled=YES;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2020-05-11
    • 1970-01-01
    • 2011-06-07
    相关资源
    最近更新 更多