UIButton actions set in XIB but not firing
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-subclassedUISlider
)UISlider
(with selectors set for theUIControlEvent
sValueChanged
,TouchDown
,TouchUpInside
,TouchUpOutside
,TouchCancel
, all o开发者_运维问答f which seem to be working still)UIGestureRecognizer
s (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 subclassedUIView
). 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.
- iPhone 3G on 4.2.1 (8C148) jailbroken with PwnageTool 4.2, baseband version
05.15.04
, no SIM inserted: Buttons not firing - iPad 1 on 4.3.5 (8L1), baseband version
07.11.01
, SIM inserted: Buttons are firing - iPad 2 on 5.0b4 (9A5274d), no baseband (it's WiFi-only): Buttons not firing
- 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.
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];
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!
Switching all selector
/action
-based callbacks to UIGestureRecognizer
s instead has worked around the problem. Still not sure why it isn't working though.
精彩评论