Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 防止UIDatePicker使用TableView滚动_Iphone_Ios_Objective C_Uidatepicker - Fatal编程技术网

Iphone 防止UIDatePicker使用TableView滚动

Iphone 防止UIDatePicker使用TableView滚动,iphone,ios,objective-c,uidatepicker,Iphone,Ios,Objective C,Uidatepicker,这似乎是一个非常简单的问题,但我还没有找到解决方案: 我有一个UIDatePicker,它显示何时选择了某个表视图单元格。我用以下代码显示它: - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section == 1) { switch (indexPath.row) { c

这似乎是一个非常简单的问题,但我还没有找到解决方案:

我有一个
UIDatePicker
,它显示何时选择了某个表视图单元格。我用以下代码显示它:

    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        switch (indexPath.row) {
            case 0: {
                _alarmPicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 315, 320, 0)];
                [_alarmPicker setDatePickerMode:UIDatePickerModeTime];
                [_alarmPicker addTarget:self action:@selector(alarmPickerChanged) forControlEvents:UIControlEventValueChanged];
                [self.view addSubview:_alarmPicker];
            }
                break;

            default:
                break;
        }
    }
}

问题是,无论何时显示选择器,它都会与表的其余部分一起上下滚动。选择选择器时,除了简单地禁用表视图上的所有滚动外,我如何保持选择器固定但表可滚动?

您可以将其添加到键窗口:

[[[UIApplication sharedApplication] keyWindow] addSubview:_alarmPicker];

因此,它将位于顶部。您正在将其添加到表视图中(假设您使用的是
UITableViewController
)。

如果您使用的是iOS 6,则可以在UIDatePicker和
self.view.superview
之间设置约束。这将阻止日期选取器移动:它将被“固定”到superview。

经过一些测试后,我发现这对我来说非常简单:
[self.window addSubview:picker]


对于删除我当时使用的选择器:
[picker removeFromSuperview]

您的视图控制器是一个子类吗?