ios - 控件在初始加载时不响应 UIPopoverController 但在后续加载时响应?

标签 ios ipad

我有一个正在加载覆盖 Controller 的应用程序(显示相机以便我可以扫描)。它在 iPhone 上运行良好,在我第二次调用它后在 iPad 上运行良好。让我解释一下。

我有一个以模态方式加载 View Controller 的 UIButtonBarItem。 Controller 中有几个控件,大多数按钮(使用 Nib 定义)。如果我在 iPhone 上加载 Controller (通过响应 UIButtonBarItem 操作),它每次都会加载并且所有按钮都能正常工作。

但是...如果我使用 UIPopoverController 加载相同的 View Controller ,则在我第一次加载它时没有任何按钮会响应。因此,我触摸 Controller 外部某处的屏幕并关闭 Controller 。然后,我再次触摸相同的操作按钮,现在当 Controller 加载时, View Controller 中的所有控件都运行良好。真的很奇怪!

[可能的提示] 当我第一次加载它时,按钮到处都是奇怪的位置。随后的每个调用都会在正确的位置显示按钮。我通过禁用 Nib 中的“Autoresize subviews”来实现它。这些按钮现在位于正确的位置,但当我第一次加载此弹出窗口时它们仍然没有响应。

这是我用来响应 UIButtonBarItem 的代码。

 -(void)launchOverlayController:(id)sender
 {
if([pickerControllerPopover isPopoverVisible])
{
    [pickerControllerPopover dismissPopoverAnimated:YES];
    pickerControllerPopover = nil;
    return;
}

// Deselect any selected cell
[self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:NO];


// Working code that shows the overlay (camera on) but the overlay takes the whole screen
SRSScanVINViewController *scanVINViewController = [[SRSScanVINViewController alloc] init];
[pickerController setOverlay:scanVINViewController];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:scanVINViewController];
[navController setModalPresentationStyle:UIModalPresentationFormSheet];
[navController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];
[[UIApplication sharedApplication] setStatusBarHidden:YES];
if([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
{
    pickerControllerPopover = [[UIPopoverController alloc] initWithContentViewController:pickerController];
    [pickerControllerPopover setDelegate:self];
    [pickerControllerPopover setPopoverContentSize:CGSizeMake(320.0f, 460.0f)];
    [pickerControllerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
}
else
{
    [self presentViewController:pickerController animated:YES completion:nil];
}

 }

我完全没有想法。我不明白为什么 overlaycontroller 中的控件在我每次调用它时都能正常工作,但第一次除外。

提前感谢任何人的帮助。

最佳答案

所以答案是父类(super class)破坏了您的 View 。我猜它不是为子类设计的,但无法确定。它在其中一种“view..”方法中所做的是用自己的 View 覆盖 self.view,并使您的 View 成为该 View 的 subview 。第一次围绕它使您的 View 框架具有零维。下次它像以前一样离开它时 - 也许是一些持久的标志。它还在其 subview 的不同位置插入 View ,这看起来很奇怪,但如果您有代码,您可能会明白为什么。

Soooo - 问题的解决方案是将 View 的 subview 移动到 superView(子类的 View ),然后将 View 的框架设置为空框架:

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];  // StackOverflow says to add this TCL?

    // Set the initial scan orientation
    [self setLayoutOrientation:self.parentPicker.orientation];

    if ([self.parentPicker hasFlash])
    {
        [flashButton setEnabled:YES];
        [flashButton setStyle:UIBarButtonItemStyleBordered];
        [self.parentPicker turnFlash:NO];
    } else
    {
        [flashButton setEnabled:NO];
    }

    textCue.text = @"";
    viewHasAppeared = NO;

    // move the subviews    
    while([self.view.subviews count]) [self.view.superview addSubview:[self.view.subviews objectAtIndex:0]];
    self.view.frame = CGRectMake(0,0,0,0);
}

PS:请注意,您在这里错过了一个 superView 调用,但这似乎并不重要(您不知道您的复杂父类(super class)可能需要哪种方法,所以我一定会向他们发送您拦截的所有内容。

关于ios - 控件在初始加载时不响应 UIPopoverController 但在后续加载时响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335435/

相关文章:

ios - 如何在UIBarbutton中将UIAlertcontroller实现为PopOver(向上箭头方向)

iOS:UIWebView全开源浏览器?

objective-c - 在 Objective C 中使用正则表达式从 url 获取 Vimeo Clip_id

ios - SwiftUI 中另一个按钮内的按钮

cocoa-touch - iPad UIWebView 将 UIPopoverController View 定位在 href 链接坐标处

javascript - Window.open + 移动设备 + Canvas == 不工作?

ios - 没有兼容模式的 iPad 上的 iPhone 应用

ios - 如何更改特定操作表上 UIAlertController 上的字体颜色?

iphone - 为什么 NSXMLParser 在 foundCharacters 方法中选择这个空格?

iphone - 如何在 iOS 中动态声明的自定义 View 中添加 UITableView?