IOS 7新特性--UIActionSheet

今天在研究UIActionSheet   IOS7新特性时,当时也不知道脑子怎么回事,直接把代码放到viewDidLoad中来执行,费了半天的劲总是出现问题,也怀疑过是不是xcode5的问题,把UIActionSheet这小段代码发给朋友,说没有问题,很郁闷 。后来想起自己平时用都是放到一个button的方法里来操作,于是有个观点产生UIActionSheet必须配合动作时才有效果。于是去查看开发文档,上面有句话也验证了观点:Action sheets display a set of buttons representing several alternative choices to complete a task initiated by the user.

官方文档:https://developer.apple.com/library/ios/documentation/UserExperience/Conceptual/UIKitUICatalog/UIActionSheet.html


生活就是这样,总会出现个小问题。IOS7下的UIActionSheet在方法上没有改变。常用方法还是

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex;

代码如下 :

UIActionSheet *actionSheet = [[UIActionSheetalloc]initWithTitle:@"Fuck-IOS7"

                                                      delegate:self

                                             cancelButtonTitle:@"取消"

                                        destructiveButtonTitle:nil

                                             otherButtonTitles:@"让你气死我了",@"让你气死他了",@"让你气死我们了",nil];

    actionSheet.actionSheetStyle =UIActionSheetStyleAutomatic;

   // [actionSheet showInView:[UIApplication sharedApplication].keyWindow];

    [actionSheet showInView:self.view];

xcode5在新建工程时,内存释放是自动释放,所以这里就不用写[actionSheetrelease];方法了。如果想设置成非自动release,方法如下图:

IOS 7新特性--UIActionSheet_第1张图片

效果图如下:



现在把出现的问题贴下:Sheet can not be presented because the view is not in a window,顺便在stackoverflow上找了答案,希望能给大家带来帮助,在此感谢我的朋友们。

1.

UIWindow* window = [[UIApplication sharedApplication] keyWindow]; if ([window.subviews containsObject:self.view]) { [emailSheet showInView:self.view]; } else { [emailSheet showInView:window]; }
2.
[actionSheet showInView:[[[[UIApplication sharedApplication] keyWindow] subviews] lastObject]];
参考:http://stackoverflow.com/questions/18932544/nsinvalidargumentexception-reason-sheet-can-not-be-presented-because-the-vi

你可能感兴趣的:(IOS 7新特性--UIActionSheet)