简体   繁体   English

UIView仅在第二次调用后显示

[英]UIView only shows after being called the second time

I have a view controller that is calling a custom UIView via an IBAction.The custom view contains a UIPickerView that slides up from the bottom of the screen and a toolbar with 'cancel' and 'done' buttons above it. 我有一个视图控制器,它通过IBAction调用自定义UIView。自定义视图包含一个UIPickerView,它从屏幕底部向上滑动以及一个工具栏,其上方具有``取消''和``完成''按钮。 The problem is that the view only appears on the screen after being called for the second time . 问题在于视图仅在第二次调用后才出现在屏幕上。 Using breakpoints I can verify that every single line of code is being called both times. 使用断点,我可以验证每行代码两次都被调用了。 Everything seems to be happening the same way each time. 每次似乎所有事情都以相同的方式发生。 Nothing is NIL, and in fact it's like this for the duration that the app is running, not only the first time it's called. NIL没什么,实际上,在应用程序运行的整个过程中都是这样,不仅是第一次调用它。 You always have to click the button twice to get the view to appear for as long as the app is running. 只要应用程序正在运行,您始终必须单击两次按钮才能使视图出现。

Admittedly, the code for the custom picker view is not mine. 不可否认,自定义选择器视图的代码不是我的。 I copied it from someone else's example. 我从别人的例子中复制了它。 I'm not sure if it's the problem or not. 我不确定是否是问题所在。 I don't see how it could be, but I'm a bit over my head here. 我不知道怎么回事,但是我有点不高兴。 This is how I'm calling the view from my view controller. 这就是我从视图控制器调用视图的方式。

- (IBAction)statusPickerButtonPressed:(id)sender {

self.scrollPickerView = [[StatusPickerView alloc]init];

[self.navigationController.view addSubview:self.scrollPickerView];
self.scrollPickerView.delegate = self;
self.scrollPickerView.dataSource = self;
}

and here's the custom UIView 这是自定义UIView

#import "StatusPickerView.h"

@interface StatusPickerView ()
@property NSArray *pickerArray;
@property NSInteger selectedRow;
@end

@implementation StatusPickerView


- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    [self setToolbar];
    [self becomeFirstResponder];


    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float pickerWidth = screenWidth * 3 / 4;
    float xPoint = screenWidth / 2 - pickerWidth / 2;



    [self setFrame: CGRectMake(xPoint, 50.0f, pickerWidth, 180.0f)];
    self.showsSelectionIndicator = YES;
    [self selectRow:3 inComponent:0 animated:YES];


}
return self;

}


-(void)setToolbar
{
_toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[_toolbar setBarStyle:UIBarStyleDefault];

UIBarButtonItem * btnCancel = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                                                            target:self action:@selector(barbtnPressed:)];
UIBarButtonItem * flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                          target:nil action:nil];
UIBarButtonItem * btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                          target:self action:@selector(barbtnPressed:)];
[btnCancel setTag:1];
[btnCancel setStyle:UIBarButtonItemStyleBordered];

[btnDone setTag:2];
[btnDone setStyle:UIBarButtonItemStyleBordered];

NSArray * btnArray = [[NSArray alloc] initWithObjects:btnCancel, flexible, btnDone, nil];
[_toolbar setItems:btnArray];

self.inputAccessoryView = _toolbar;
self.inputView = self;
}

-(BOOL)canBecomeFirstResponder
{
return true;
}




-(void)barbtnPressed:(id)sender
{
NSInteger tag = [sender tag];
switch (tag) {
    case 1:
    {
        [self removeFromSuperview];
        break;
    }
    case 2:{
        [self removeFromSuperview];
        self.selectedRow = [self selectedRowInComponent:0];
        [[NSNotificationCenter defaultCenter]postNotificationName:@"user_selected_new_section" object:self];

    }
    default:
        break;
}
}

-(int)giveSelectedRow{

return self.selectedRow; 
}

I'm fully prepared to feel foolish here, as the solution is probably obvious, just not obvious to myself. 我已经准备好在这里感到愚蠢,因为解决方案可能很明显,但对我自己而言并不明显。

edit: I tried using [self.view.window addSubview:self.scrollPickerView]; 编辑:我尝试使用[self.view.window addSubview:self.scrollPickerView]; instead of [self.navigationController.view addSubview:self.scrollPickerView]; 而不是[self.navigationController.view addSubview:self.scrollPickerView]; , and the behavior is exactly the same. ,并且行为完全相同。

This line: 这行:

 [self.navigationController.view addSubview:self.scrollPickerView];

should read: 应该读:

 [self.view addSubview:self.scrollPickerView];

You're calling -init instead of -initWithFrame: , so your view is ending up with the default frame of CGRectZero . 您正在调用-init而不是-initWithFrame:因此您的视图最终以CGRectZero的默认框架结束。 Considering that you're then making a frame of your own, that call might as well pass that in to start out with: 考虑到您正在制作自己的框架,则该调用也可以将其传入以开始:

self.scrollPickerView = [[ScrollPickerView alloc] initWithFrame:CGRectZero];

Alternatively, you could keep the current self.scrollPickerView = … code and change the -initWithFrame: to an -init , like this: 另外,您可以保留当前的self.scrollPickerView = …代码,然后将self.scrollPickerView = … -initWithFrame:更改为-init ,如下所示:

- (id)init
{
    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float pickerWidth = screenWidth * 3 / 4;
    float xPoint = screenWidth / 2 - pickerWidth / 2;
    self = [super initWithFrame:CGRectMake(xPoint, 50.0f, pickerWidth, 180.0f)];

    if (self) {
        [self setToolbar];
        [self becomeFirstResponder];

        self.showsSelectionIndicator = YES;
        [self selectRow:3 inComponent:0 animated:YES];
    }
    return self;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM