Skip to content

Commit

Permalink
Added example code to handle selection events from a popover view and
Browse files Browse the repository at this point in the history
to redirect this events to the originating view controller.
  • Loading branch information
Andreas Katzian committed Oct 4, 2010
1 parent 700797c commit b366b04
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
2 changes: 1 addition & 1 deletion PopoverView/Classes/MainViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#import <UIKit/UIKit.h>
#import "ViewWithPickerController.h"

@interface MainViewController : UIViewController {
@interface MainViewController : UIViewController <PopoverPickerDelegate> {

UIPopoverController *popoverController;

Expand Down
17 changes: 17 additions & 0 deletions PopoverView/Classes/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ - (void) showPickerPopupAction:(id) sender {
//set popover content size
viewWithPickerController.contentSizeForViewInPopover =
CGSizeMake(viewWithPickerController.view.frame.size.width, viewWithPickerController.view.frame.size.height);

//set delegate
viewWithPickerController.delegate = self;


//create a popover controller
Expand All @@ -123,6 +126,20 @@ - (void) showPickerPopupAction:(id) sender {
animated:YES];
}

// Method gets called whenever the selection of an element
// within the picker view in the popover view occurs.
- (void) viewWithPickerController:(ViewWithPickerController*) viewWithPickerController didSelectValue:(NSString*) value
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Selection"
message:[NSString stringWithFormat:@"You selected %@!", value]
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil];

[alert show];
[alert release];
}


// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
Expand Down
17 changes: 17 additions & 0 deletions PopoverView/Classes/ViewWithPickerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,34 @@

#import <UIKit/UIKit.h>

//need to declare class
@class ViewWithPickerController;


//A simple protocol to implement delegation
//for value selection within the picker view
@protocol PopoverPickerDelegate

@required
- (void) viewWithPickerController:(ViewWithPickerController*) viewWithPickerController didSelectValue:(NSString*) value;

@end


@interface ViewWithPickerController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {

IBOutlet UIPickerView *pickerView;
IBOutlet UILabel *label;

id<PopoverPickerDelegate> delegate;

}

@property(nonatomic, retain) IBOutlet UIPickerView *pickerView;
@property(nonatomic, retain) IBOutlet UILabel *label;

@property(nonatomic, assign) id<PopoverPickerDelegate> delegate;



@end
9 changes: 7 additions & 2 deletions PopoverView/Classes/ViewWithPickerController.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@implementation ViewWithPickerController

@synthesize pickerView, label;
@synthesize pickerView, label, delegate;


#pragma mark -
Expand All @@ -38,7 +38,12 @@ - (NSString *)pickerView:(UIPickerView *)picker titleForRow:(NSInteger)row forCo

//handle selection of a row
- (void)pickerView:(UIPickerView *)picker didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
self.label.text = [pickerView.delegate pickerView:picker titleForRow:row forComponent:component];
NSString *value = [pickerView.delegate pickerView:picker titleForRow:row forComponent:component];
self.label.text = value;

//notify the delegate about selecting a value
if(delegate != nil)
[delegate viewWithPickerController:self didSelectValue:value];
}


Expand Down
7 changes: 7 additions & 0 deletions PopoverView/PopoverView.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,14 @@
isa = PBXProject;
buildConfigurationList = C01FCF4E08A954540054247B /* Build configuration list for PBXProject "PopoverView" */;
compatibilityVersion = "Xcode 3.1";
developmentRegion = English;
hasScannedForEncodings = 1;
knownRegions = (
English,
Japanese,
French,
German,
);
mainGroup = 29B97314FDCFA39411CA2CEA /* CustomTemplate */;
projectDirPath = "";
projectRoot = "";
Expand Down

0 comments on commit b366b04

Please sign in to comment.