UISlider subclass causing UI hang in only one UIView of universal app
I have been working on the front-end to a universal iOS app, setting up separate iPhone and iPad view controllers as subclasses of a XIB-less view controller (as described in http://www.kotancode.com/2011/04/05/ios-universal-apps/). Each subclassed view controller contains a device-specific XIB, with each XIB containing portrait and landscape UIViews which are switched upon orientation change (I followed http://aseriesoftubes.com/articles/ipad-development-101-orientation/ for this). The controls common to all four UIViews are synchronised with an IBAction method linked through IBOutletCollections.
Just today I have created a UISlider subclass for my own slider design, which appears to work fine, apart from one large issue: when testing with the iPhone in the simulator starting in portrait orientation the slider works fine; when switched to landscape it is also fine; however when I switch back to portrait the slider works for up to two operations and then freezes, taking some other controls of the view with it; stranger still, if I switch back to landscape the slider works fine! This happens only with the iPhone simulator, not the iPad.
I have tried switching IB's 'Continuous' option on and off, and tried both the 'Touch Up Inside' and 'Value Changed' connections from my slider's Sent Events, with no change (as it will be for a volume control I will need 'Value Changed' to work). My slider's implementation code is thus:
@implementation VolumeSlider
- (id)initWithCoder:(NSCoder *)decoder
{
if((self = [super initWithCoder: decoder])) // Double paretheses to silence warning
{
开发者_开发技巧 // Instantiate slider images
UIImage *thumbImage = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"volume thumb" ofType: @"png"]];
UIImage *trackImage = [[UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @"volume track" ofType: @"png"]] stretchableImageWithLeftCapWidth: 20 topCapHeight: 0];
// Set control elements with images
[self setThumbImage: thumbImage forState: UIControlStateNormal];
[self setMinimumTrackImage: trackImage forState: UIControlStateNormal];
[self setMaximumTrackImage: trackImage forState: UIControlStateNormal];
}
return self;
}
- (void)dealloc
{
[super dealloc];
}
@end
The two action methods that the sliders trigger are:
- (IBAction)volumeChange:(id)sender
{
NSLog(@"VOLUME slider changing");
// VOLUME slider action
}
- (IBAction)volumeChangeFinish:(id)sender
{
NSLog(@"VOLUME change finished");
VolumeSlider *incomingVolume = sender;
for(VolumeSlider *volumeSlider in self.volumeSliders)
{
volumeSlider.value = incomingVolume.value;
}
}
(volumeChange will use the slider value to change volume in real time, volumeChangeFinished will update the other sliders in the view on touch inside up.)
This one has me stumped – it feels like a memory issue but I can't figure out exactly where. Does anyone have an idea what could be causing this?
(Mac OS 10.6.8, Xcode 4.0.2, building for iOS 4.3)
UPDATE ---
I have tried instantiating a new, stock UISlider into the iPhone view and this suffers the same problem, without having been connected up to anything. This points towards an issue with the view controller, or even some bug.
Although I have no definitive reason for the issue, I have managed to fix this. Basically I created a new UIView for the iPhone portrait orientation in Interface Builder, rebuilt the interface and connected it up in place of the dodgy one and the issue was gone. I don't know if I could have had a corrupted view in the XIB, that's the only answer I can proffer.
精彩评论