2014-09-19 79 views
3

我刚刚更新到iOS 8,我的应用程序有问题,除了操作工作表上的日期选取器。当为了显示操作表而将操作选择为与子视图一起添加的日期选择器时,应用程序会崩溃。我发现这个问题的代码,其是:UIDatePickerView问题,因为更新到ios 8

-(void)willPresentActionSheet:(UIActionSheet*)actionSheet{ 

theDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, 320, 216)]; 
[actionSheet addSubview:theDatePicker]; 


NSArray *subviews = [actionSheet subviews]; 
[[subviews objectAtIndex:0]setFrame:CGRectMake(20, 266, 280, 46)]; //this line 
[[subviews objectAtIndex:1]setFrame:CGRectMake(20, 317, 280, 46)]; //also this one 

} 

控制台日志产生这样的:

-[__NSArrayM objectAtIndex:]: index 1 beyond bounds [0 .. 0]' 

以前我解决了它(严重)通过改变每个索引2 & 3,其工作,但是一个凌乱的实施。

这是实际执行:

-(void)willPresentActionSheet:(UIActionSheet*)actionSheet{ 

theDatePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, 320, 216)]; 
[actionSheet addSubview:theDatePicker]; 


NSArray *subviews = [actionSheet subviews]; 
[[subviews objectAtIndex:0]setFrame:CGRectMake(20, 266, 280, 46)]; 
[[subviews objectAtIndex:1]setFrame:CGRectMake(20, 317, 280, 46)]; 

} 

-(void)datePickerViewFromInput:(NSString*)title{ 
    SheetPicker=[[UIActionSheet alloc]initWithTitle: title delegate:self cancelButtonTitle:@"Cancel"  destructiveButtonTitle:nil otherButtonTitles:@"Set", nil]; 
[SheetPicker showInView:self.view ]; 
[SheetPicker setFrame:CGRectMake(0,117, 325, 383)]; 
theDatePicker.datePickerMode = UIDatePickerModeDate; 
NSDateFormatter *Formatter = [[NSDateFormatter alloc]init]; 
[Formatter setDateFormat:@"MM/dd/yyyy"]; 

} 

如果我删除的问题代码,动作片将显示,但已不再是一个日期选择器。 (我不知道为什么)

有关如何解决此问题的任何想法?

在此先感谢

回答

2

也许苹果改变视图层次,there is a note in the docs并没有真正覆盖你的情况,但还是给了我们一个提示,这是怎么回事:

UIActionSheet的目的不是要被继承,也不应该您将视图添加到其层次结构中。如果您需要提供比UIActionSheet API提供更多定制的工作表,您可以创建自己的工作表并使用presentViewController以模态方式呈现它:animated:completion :.

就像UIAlerView苹果不希望我们改变它的体验 - 那里 - 他们做了一些修补视图层次结构。

最激动人心的解决方案是按照苹果建议的做法:创建一个视图控制器并以模态方式呈现它。

+0

辉煌,谢谢。 – TinMan7757 2014-09-19 10:13:47

0

有一个滑块改变UIActionSheet在iOS的8试试这个代码。

UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; 

[alert.view addSubview:theDatePicker]; 
[self presentViewController:alert animated:YES completion:nil]; 
+0

这可行,但在一定程度上。 DatePicker不会作为子视图添加到警报控制器中,而是添加到主视图中。然后它停留在那里,我需要它被添加到警报控制器,所以我也可以解雇它。 – TinMan7757 2014-09-19 10:11:00

+0

谢谢..这是真的。 – Deepak 2014-09-19 10:44:03

+1

你在哪里不远,使用 [partentView.view addSubview:TheDatePicker]; 只需调整控制器视图以容纳选取器。 – TinMan7757 2014-09-19 11:04:46