将选择器视图添加到UIView

时间:2015-01-09 07:41:35

标签: objective-c uiview uipickerview custom-view

我的应用与自定义视图相关。在单视图控制器中,有14个按钮。单击每个按钮时,包含选择器视图的自定义视图应与ok和取消按钮以及透明背景一起显示。我已经为此编写了代码,当我尝试将选择器视图添加到主视图时,应用程序会崩溃。我的代码如下。

-(custompickerview *)sharedInstance{
    static custompickerview *myInstance = nil;
    if (nil == myInstance) {
       myInstance = [[[self class] alloc] initWithView];
    }//End of if statement

    myInstance.window = [[UIWindow alloc] initWithFrame:[UIScreen   
       mainScreen].bounds];
    myInstance.window.windowLevel = UIWindowLevelStatusBar;
    myInstance.window.hidden = YES;

    myInstance.window.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];

    return myInstance;
}

-(id)initWithView{
    NSArray *arrayOfViews;
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"custompickerview"
                                                 owner:nil
                                               options:nil];
    }else{
        arrayOfViews = [[NSBundle mainBundle] loadNibNamed:@"custompickerview"
                                                 owner:nil
                                               options:nil];
    }

    if ([arrayOfViews count] < 1){
        return nil;
    }

    custompickerview *newView = [arrayOfViews objectAtIndex:0];
    //[newView setFrame:frame];
    newView.layer.cornerRadius = 10.0;
    newView.layer.borderColor = [UIColor blackColor].CGColor;
    newView.clipsToBounds = YES;
    self = newView;
    return self;
}

-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
    for (UIView *view in self.subviews) {
    if (!view.hidden && view.alpha > 0 && view.userInteractionEnabled && [view   
        pointInside:[self convertPoint:point toView:view] withEvent:event])
        return YES;
    }
    [self didCustomPopUpUnload];

    return NO;
}


- (void)didCustomPopUpAlertLoad:(UIView *)parentView andtitle:(NSString  
       *)strTitle {
    [self setRootView:parentView];
    [parentView addSubview:_pickerview];

    //Add alertview into transparent view to hide parent view interaction
    UIView *transparentView = [[UIView alloc] initWithFrame:parentView.bounds];
    [transparentView setBackgroundColor:[UIColor clearColor]];
    //[transparentView addSubview:_pickerview];
    [transparentView addSubview:self];

    float x = (int)(transparentView.bounds.size.width - self.bounds.size.width)>>1;
    float y = (int)(transparentView.bounds.size.height - self.bounds.size.height)>>2;
    [self setFrame:CGRectMake(x, y+62, self.bounds.size.width,   
     self.bounds.size.height)];

     //    [self setFrame:CGRectMake(x+10, y+62, self.bounds.size.width, self.bounds.size.height)];
    [self.window addSubview:transparentView];
    [self.window makeKeyAndVisible];
}

将子视图添加到父视图可能有误。是否有任何示例代码如何将选择器视图添加到自定义视图?

0 个答案:

没有答案