UIButton 操作在 XIB 中设置但未触发

发布于 2024-11-26 11:50:34 字数 1994 浏览 1 评论 0原文

问题描述很简短,但我想我不需要说太多。当我加载自定义电影播放器​​的 .xib 时,UIButton/UIBarButtonItem 控件将设置为我的 File's Owner 对象的 ivars/properties,并且它们还设置了操作/选择器各种本地方法。该函数从未进入(未命中 gdb 断点)。

我已经验证了 setView:set...Button: 正在被调用(还没有调查它们是否为零)。我还尝试在 UI 初始化时手动调用 addTarget:action:forControlEvents:/setAction: ,但无济于事。

这里还有一个额外的奇怪之处:按钮实际上突出显示,然后保持突出显示。因此,它们从 UIControlStateNormal 转换为 UIControlStateHighlighted(/UIControlStateSelected,具体取决于单击滚动的方式),但是基于 UIControlEventTouchUpInside > 否则会触发按钮的操作,按钮似乎保持在突出显示/选定状态。

请注意,同一 .xib 中的以下控件可以正常工作:

  • MPVolumeView(Apple 子类 UISlider
  • < code>UISlider (为 UIControlEvents ValueChangedTouchDownTouchUpInside 设置选择器, TouchUpOutsideTouchCancel,所有这些似乎仍在工作)
  • UIGestureRecognizer(甚至目前按钮上的单击按钮也可以工作)

这曾经可以工作,那么从那时起 .xib 发生了什么变化?好吧:

  • 现在使用 AVPlayerLayer 作为顶级视图的层类(因此顶级视图也是 UIView 的子类)。这是为了修复一些电影加载不可靠/视觉滞后的问题。

这实际上就是所有改变,至少我能想到的。我也尝试过删除并重新连接 IB 中的插座。也欢迎不一定是解决方案的想法!

编辑:好的,这里是它正在工作/损坏的设备的概述。模拟器未测试,因为我有 .a 库,其中只有 armv6/armv7 架构编译。

  1. iPhone 3G on 4.2.1 (8C148) 使用 PwnageTool 4.2 越狱,基带版本05.15.04,未插入 SIM 卡:按钮无法启动
  2. 4.3.5 (8L1) 上的 iPad 1,基带版本 07.11.01,已插入 SIM 卡:按钮是< /em> 在 5.0b4 (9A5274d) 上触发
  3. iPad 2,无基带(仅 WiFi):按钮无法
  4. 在 4.3.4 上触发 iPhone 4 (8K2),插入 SIM:按钮正在触发

我尝试从 iOS 4.3 (Xcode 4.0.2 4A2002a) 和 iOS 5.0b4 (Xcode 4.2 4C139) 部署到设备 1 和 2,结果相同在这两种情况下(手机无法使用,平板电脑可以使用)。我还尝试从设备 1 和 2 上完全卸载该应用程序并进行全新构建,重新安装时结果相同。我仅从 Xcode 4.2 部署到设备 3,仅从 Xcode 4.0.2 部署到设备 4。

我只能看到没有细胞信号=不起作用,但这是无意义的。为了以防万一,我在设备 2 上关闭了手机数据,但它在 WiFi 上仍然可以正常工作。

Problem description is brief but I think I don't need to say much. When I load my custom movie player's .xib, the UIButton/UIBarButtonItem controls are set to ivars/properties of my File's Owner object, and they also have their actions/selectors set to various local methods. The function is never entered (gdb breakpoint not hit).

I've verified setView: and set...Button: are being called (haven't investigated whether they're nil yet). I also tried manually calling addTarget:action:forControlEvents:/setAction: upon UI initialisation, to no avail.

There's extra oddness here too: the buttons actually highlight, and then stay highlighted. So they transition from UIControlStateNormal to UIControlStateHighlighted(/UIControlStateSelected, depending on how single taps roll), but upon the UIControlEventTouchUpInside which would otherwise trigger the action of the button, the button seems to remain in its highlighted/selected state.

Note that the following controls in the same .xib work without issue:

  • MPVolumeView (Apple-subclassed UISlider)
  • UISlider (with selectors set for the UIControlEvents ValueChanged, TouchDown, TouchUpInside, TouchUpOutside, TouchCancel, all of which seem to be working still)
  • UIGestureRecognizers (even single tap ones on buttons currently work)

This used to work, so what's changed in the .xib since then? Well:

  • Now using an AVPlayerLayer as the layerClass of the top-level view (hence the top-level view is also a subclassed UIView). This was to fix some movie loading dodginess/visual lag.

That's literally all that's changed, at least that I can think of.. I've tried removing and reconnecting the outlets in IB too. Thoughts which are not necessarily solutions are also welcomed!

EDIT: Okay, so here's an overview of which devices it's working/broken in. Simulator not tested because I have .a libraries which only have the armv6/armv7 architectures compiled in.

  1. iPhone 3G on 4.2.1 (8C148) jailbroken with PwnageTool 4.2, baseband version 05.15.04, no SIM inserted: Buttons not firing
  2. iPad 1 on 4.3.5 (8L1), baseband version 07.11.01, SIM inserted: Buttons are firing
  3. iPad 2 on 5.0b4 (9A5274d), no baseband (it's WiFi-only): Buttons not firing
  4. iPhone 4 on 4.3.4 (8K2), SIM inserted: Buttons are firing

I've tried deploying to devices 1 and 2 from both iOS 4.3 (Xcode 4.0.2 4A2002a) and iOS 5.0b4 (Xcode 4.2 4C139), same results in both instances (phone doesn't work, pad works). I've also tried completely uninstalling the app from devices 1 and 2 AND doing a clean build, same results on reinstall. Device 3 I deployed to from Xcode 4.2 only and device 4 from Xcode 4.0.2 only.

Only correlation I can see there is no cell signal = not working, but that's nonsensical. I turned cell data off on device 2 just in case, but it's still working fine over WiFi.

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

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

发布评论

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

评论(3

不必在意 2024-12-03 11:50:34

我有类似的问题。原来是因为我的按钮不在视图中。

我的应用程序处于横向模式,但 UIViewController 的视图在某些设备和/或 iOS 版本上旋转时不会自动调整大小。

为了形象化这个问题,我在初始化视图和按钮的地方添加了这个:

self.view.backgroundColor = [UIColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:0.4f];

打开这个,我可以看到在某些情况下不响应按下的按钮位于 UIViewController 的视图之外(因此没有获取事件) )。

为了解决这个问题,我只是将其添加到初始化视图和按钮的位置:

// Be sure to set the frame for this view controller to be fullscreen
CGSize winSize = [UIScreen mainScreen].bounds.size;

// Note that we switched height and width...this is because
// these values are always for portrait mode
self.view.frame = CGRectMake(0.0f, 0.0f, winSize.height, winSize.width);

请务必特别注意 [UIScreen mainScreen].bounds.size 将始终返回纵向值,如果您是这样,则必须反转它们以横向模式操作。

添加后,我可以看到我的视图覆盖了全屏,并且按钮开始响应所有设备和 iOS 版本上的事件/按下操作。

如果您使用的是 cocos2d,您可以使用它,而不必担心手动管理方向:

CGSize winSize = [[CCDirector sharedDirector] winSize];

I had a similar issue. It turned out to be because my button wasn't in the view.

My app is in landscape mode but the view of the UIViewController wasn't getting automatically resized when it was rotated on some devices and/or iOS version.

To visualize the problem I added this where I was initializing my view and buttons:

self.view.backgroundColor = [UIColor colorWithRed:1.0f green:0.0f blue:1.0f alpha:0.4f];

With this on, I could see that the button that wasn't responding to presses in some cases was outside of the view of the UIViewController (and thus not getting events).

To fix this I just added this to where I was initializing my view and buttons:

// Be sure to set the frame for this view controller to be fullscreen
CGSize winSize = [UIScreen mainScreen].bounds.size;

// Note that we switched height and width...this is because
// these values are always for portrait mode
self.view.frame = CGRectMake(0.0f, 0.0f, winSize.height, winSize.width);

Be sure to pay particular attention to the fact that [UIScreen mainScreen].bounds.size will always return portrait values and you will have to reverse them if you are operating in landscape mode.

After adding that I could see that my view covered the full screen and the buttons started responding to events/presses on all devices and iOS versions.

If you are using cocos2d you can use this instead and not have to worry about manually managing orientations:

CGSize winSize = [[CCDirector sharedDirector] winSize];
白衬杉格子梦 2024-12-03 11:50:34

让我猜你使用 UIGestureRecognizer。

您必须使用 UIGestureRecognizerDelegate 并告诉识别器在用户按下按钮时不要触发。

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UIButton class]]){
        return NO;
    }
    return YES;
}

这解决了我的问题!

Let me guess you use UIGestureRecognizer.

You have to use the UIGestureRecognizerDelegate and tell the recognizer not to fire if the user hits a button.

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {
    if ([touch.view isKindOfClass:[UIButton class]]){
        return NO;
    }
    return YES;
}

This fixed the problem for me!

初吻给了烟 2024-12-03 11:50:34

将所有基于 selector/action 的回调切换为 UIGestureRecognizer 可以解决该问题。但仍然不确定为什么它不起作用。

Switching all selector/action-based callbacks to UIGestureRecognizers instead has worked around the problem. Still not sure why it isn't working though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文