objective-ccocoa-touchmpvolumeview

How to add the MPVolumeView via Xcode Designer?


I originally added an MPVolumeView dynamically on to a page...

#import "MediaPlayer/MPVolumeView.h"
.
.
-(IBAction) handleVolumeButtonClicked:(id) sender {
    if (volumeView == nil) {
        volumeView = [[MPVolumeView alloc] initWithFrame:CGRectMake(25, 378, 270, 30)];
        [self.view addSubview:volumeView];
        [volumeView release];
    } else {
        [volumeView removeFromSuperview];
        volumeView = nil;
    }
}

But for some reason I started getting reports of the application crashing when dynamically adding the volume component.

To work around this, I decided to try and add the component to the view via the XCode designer, but then I realised that I didn't know how to do this!

I dragged an 'Object' from the template to the Object palette first, but then I found that I couldn't add it to the view. So I scrapped that idea and then dragged the 'View' object directly on to the .xib view.

Once the component was added, I tried to change the 'Custom Class' to 'MPVolumeView', but the view just rendered an empty rectangle. Running the code in the simulator failed to render anything.

Does anyone know what steps I am missing to add a class to the view that doesnt already exist in the palette?


Solution

  • Not sure if you have figured this out by now. I just created an app with a volume slider successfully. Here's what I did (from MPVolumeView):

    #import 
    
    @interface MyView : UIViewController {
        MPVolumeView *_mpVolumeViewParentView;
    }
    @property (nonatomic, retain) IBOutlet MPVolumeView *mpVolumeViewParentView;
    
    @end
    #import "MyView.h"
    
    @implementation MyView
    
    @synthesize mpVolumeViewParentView = _mpVolumeViewParentView;
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        [[self mpVolumeViewParentView] setBackgroundColor:[UIColor clearColor]];
        MPVolumeView *myVolumeView = [[MPVolumeView alloc] initWithFrame:[[self mpVolumeViewParentView] bounds]];
        [[self mpVolumeViewParentView] addSubview:myVolumeView];
        [myVolumeView release];
    }
    - (void)dealloc {
        [_mpVolumeViewParentView release];
        [super dealloc];
    }

    Hope it helps.