iPhone UIActionSheet 自动旋转不起作用

发布于 2024-10-15 08:36:30 字数 516 浏览 3 评论 0原文

我读了很多相关内容。人们说,当其父级未设置为自动旋转时,它不会自动旋转。我尝试了一切但没有运气。 我创建了基于视图的应用程序(v4.2),并带有一个执行此操作的按钮:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];

根控制器设置为自动旋转。而actionSheet则没有。请注意,当我旋转模拟器时,不会调用根控制器的任何方向方法。代表有问题吗?怎么了?

I read a lot about that. People say it will not autorotate whene its parent is not set to auto rotate. I tried everything but no luck.
I created view-based app (v4.2) with a button that executes this:

    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Title" delegate:self cancelButtonTitle:@"Cancel Button" destructiveButtonTitle:@"Destructive Button" otherButtonTitles:@"Other Button 1", nil];
    actionSheet.actionSheetStyle = UIActionSheetStyleBlackOpaque;
    [actionSheet showInView:self.view];

The root controller is set to auto-rotate. The actionSheet does not. Note that when I rotate the simulator none of the root controller's orientation methods are called. Is there a problem with the delegate? What is wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里°也失望 2024-10-22 08:36:30

好吧,这是我对这个问题的解决方案:

基本上我们所做的是:

  1. 监听旋转事件。
  2. 监听点击事件。
  3. 关闭 actionSheet 并在旋转完成后再次呈现它。 (我们需要等待一段时间才能完成。

例如:

@interface ViewController ()
{
    UIActionSheet *_sheet;
    BOOL _isClicked;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (IBAction)click:(id)sender
{
    _isClicked = YES;

    [self showActionSheet];
}

- (void)didRotate:(NSNotification *)note
{
    [_sheet dismissWithClickedButtonIndex:1 animated:YES];
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:1.0];
}

- (void)showActionSheet
{
    if (!_isClicked) return;

    _sheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"new" otherButtonTitles:@"bla", nil];
    [_sheet showInView:self.view];
}

Well, here's my solution to this problem:

Basically what we do is:

  1. Listen to the rotation event.
  2. Listen to click event.
  3. Dismiss the actionSheet and present it again after the rotation is done. (we need to wait a small delay in order for it to take.

for example:

@interface ViewController ()
{
    UIActionSheet *_sheet;
    BOOL _isClicked;
}
@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];
}

- (IBAction)click:(id)sender
{
    _isClicked = YES;

    [self showActionSheet];
}

- (void)didRotate:(NSNotification *)note
{
    [_sheet dismissWithClickedButtonIndex:1 animated:YES];
    [self performSelector:@selector(showActionSheet) withObject:nil afterDelay:1.0];
}

- (void)showActionSheet
{
    if (!_isClicked) return;

    _sheet = [[UIActionSheet alloc] initWithTitle:@"title" delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:@"new" otherButtonTitles:@"bla", nil];
    [_sheet showInView:self.view];
}
粉红×色少女 2024-10-22 08:36:30

我发现当我从从属视图(我使用导航控制器推送的)的委托方法中呈现操作表时遇到了这个问题。问题是我的视图不是当前的视图,从属视图仍然在我试图显示操作表的位置。

通过稍微更改我的代码,以便委托方法记录所需与用户的交互,并将操作表呈现推迟到该视图的 viewDidAppear 方法,该表在逻辑界面动画中的适当时间出现,并且自动旋转问题消失了。您可能想看看这是否对您有帮助。

换句话说,流程变成:

  • 下级视图调用委托方法来报告用户在离开时所做的选择。
  • 父视图记录了此信息以供稍后使用。
  • 导航控制器被告知弹出从属视图。
  • 父视图的 viewDidLoad: 方法检测到委托方法中所做的注释。
  • 提出了行动表;旋转现在是正确的。

I found that I was running into this problem when I was presenting the action sheet in a delegate method from a subordinate view (which I had pushed using the navigation controller). The problem was that my view was not the current one, the subordinate view was still up at the point where I was trying to show the action sheet.

By changing my code a little bit so that the delegate method made a note of the interaction needed with the user, and deferring the action sheet presentation to this view’s viewDidAppear method, the sheet appeared at the proper time in the logical interface animation, and the auto-rotation problem went away. You might want to see if that helps you.

In other words, the flow became:

  • Subordinate view called delegate method to report choices the user made when leaving.
  • Parent view recorded this information for later.
  • Navigation controller was told to pop off subordinate view.
  • Parent view’s viewDidLoad: method detected the note made in the delegate method.
  • Action sheet was presented; rotation was now correct.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文