UIView仅在第二次被调用后显示

时间:2013-12-12 16:14:42

标签: ios objective-c uiview uipickerview

我有一个通过IBAction调用自定义UIView的视图控制器。自定义视图包含一个从屏幕底部向上滑动的UIPickerView和一个在其上方带有“取消”和“完成”按钮的工具栏。问题是视图仅在第二次调用后出现在屏幕上。使用断点我可以验证每次都调用每一行代码。一切似乎每次都以同样的方式发生。没有什么是NIL,事实上它在应用程序运行期间就是这样,而不仅仅是第一次调用它。您始终必须单击该按钮两次才能在应用程序运行时显示视图。

不可否认,自定义选择器视图的代码不是我的。我从别人的例子中复制了它。我不确定这是不是问题。我不知道它怎么可能,但我在这里有点过头了。这就是我从视图控制器调用视图的方式。

- (IBAction)statusPickerButtonPressed:(id)sender {

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

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

这是自定义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; 
}

我完全准备在这里感到愚蠢,因为解决方案可能很明显,对我自己来说并不明显。

编辑: 我尝试使用[self.view.window addSubview:self.scrollPickerView];代替[self.navigationController.view addSubview:self.scrollPickerView];,行为完全相同。

2 个答案:

答案 0 :(得分:0)

这一行:

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

应为:

 [self.view addSubview:self.scrollPickerView];

答案 1 :(得分:0)

您正在呼叫-init而不是-initWithFrame:,因此您的视图最终会以CGRectZero的默认帧结束。考虑到你正在构建自己的框架,那个调用也可以通过以下内容开始:

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

或者,您可以保留当前的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;
}