UIPopoverController的使用,看着很牛逼的样子

先看看这个图

要做的样子就是点击下面这个按钮,弹出上面这个带箭头的弹出框

下面来看看怎么搞

#import <UIKit/UIKit.h>

@interface UIActionSheetDemoViewController : UIViewController<UIPopoverControllerDelegate>
{
    UIPopoverController* _popover;
}
#import "UIActionSheetDemoViewController.h"
#import "popController.h"
@implementation UIActionSheetDemoViewController
@synthesize popover = _popover;
#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UISegmentedControl* settingView = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"FootballCF",@"BasketballCF",nil]];
    [settingView setSegmentedControlStyle:UISegmentedControlStyleBar];
    [settingView setFrame:CGRectMake(500, 900, 160, 75)];
    [settingView setNeedsLayout];
    [settingView addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:settingView];
    [settingView release];
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    
    if (self.popover != nil) 
{
        [self.popover release];
        self.popover = nil;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return YES;
}
-(void)segmentAction:(UISegmentedControl*)seg{
    //清除已经显示的弹出窗口
    if (self.popover.popoverVisible) {
        [self.popover dismissPopoverAnimated:YES];

    }
    //初始化待显示控制器
    popController* controller = [[popController alloc] init];
    //设置待显示控制器的范围
    [controller.view setFrame:CGRectMake(550, 490, 350, 450)];
    //设置待显示控制器视图的尺寸
    controller.contentSizeForViewInPopover = CGSizeMake(350, 450);
    //初始化弹出窗口
    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:controller];
    controller.popover = pop;
    self.popover = pop;
    self.popover.delegate = self;
    //设置弹出窗口尺寸
    self.popover.popoverContentSize = CGSizeMake(350, 450);
    
    //显示,其中坐标为箭头的坐标以及尺寸
    [self.popover presentPopoverFromRect:CGRectMake(550, 890, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    
    [controller release];
    [pop release];
    
    //[self.popover dismissPopoverAnimated:YES];
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popover dismiss");
}

@end

#import <UIKit/UIKit.h>

@interface popController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>
{
    UIPopoverController* _popover;
    BOOL isChelsea;
}
@property(nonatomic,retain)UIPopoverController* popover;
@end
#import "popController.h"

@implementation popController
@synthesize popover = _popover;

//定义选项显示内容
const char* chelsea[12] = {"博阿斯","切赫","阿什利科尔","特里","路易斯","伊万诺维奇","兰帕德","拉米雷斯","梅来雷斯","阿内尔卡","德罗巴","托雷斯"};
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
  }
    return self;
}
#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    isChelsea = NO;
    
    //添加完成按钮
    UISegmentedControl* doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Done", nil]];
    [doneButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [doneButton setFrame:CGRectMake(self.view.bounds.origin.x+self.view.bounds.size.width-100, self.view.bounds.origin.y+5, 75, 50)];
    [doneButton addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:doneButton];
    [doneButton release];

    //添加选择项
    UISegmentedControl* clubSeg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Chelsea",@"AC Milan",@"Livupoor",@"Asenal",nil]];
    [clubSeg setSegmentedControlStyle:UISegmentedControlStyleBar];
    [clubSeg setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75,self.view.bounds.size.width,50)];
  [clubSeg addTarget:self action:@selector(chooseClub:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:clubSeg];
    [clubSeg release];
    
    //添加选择列表
    UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+150, self.view.bounds.size.width,self.view.bounds.size.height)];
    [picker setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75, self.view.bounds.size.width,self.view.bounds.size.height)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];
    [picker setTag:101];
    [picker release];
    
    
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 12;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (isChelsea) {
        return [NSString stringWithCString:chelsea[row] encoding:NSUTF8StringEncoding];
    }else{
        return [NSString stringWithFormat:@"chelsea_%d",arc4random()%12];
        ;
    }
    
}
-(void)segmentAction:(UISegmentedControl*)seg{
    [self.view removeFromSuperview];
  [self.popover dismissPopoverAnimated:YES];
}
-(void)chooseClub:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    UIPickerView* picker = (UIPickerView*)[self.view viewWithTag:101];
    switch (index) {
        case 0:{
            NSLog(@"you choose Chelsea");
            isChelsea = YES;
            [picker reloadAllComponents];
            break;
        }
        case 1:
            NSLog(@"you choose AC Milan");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 2:
            NSLog(@"you choose Livopoor");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 3:
            NSLog(@"you choose Asenal");
            isChelsea = NO;
            [picker reloadAllComponents];
        default:
            break;
    }
}
@end

@property(nonatomic,retain)UIPopoverController* popover;
@end

#import <UIKit/UIKit.h>

@interface UIActionSheetDemoViewController : UIViewController<UIPopoverControllerDelegate>{
    UIPopoverController* _popover;
}
#import "UIActionSheetDemoViewController.h"
#import "popController.h"
@implementation UIActionSheetDemoViewController
@synthesize popover = _popover;
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UISegmentedControl* settingView = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"FootballCF",@"BasketballCF",nil]];
    [settingView setSegmentedControlStyle:UISegmentedControlStyleBar];
    [settingView setFrame:CGRectMake(500, 900, 160, 75)];
    [settingView setNeedsLayout];
    [settingView addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:settingView];
    [settingView release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    if (self.popover != nil) {
        [self.popover release];
        self.popover = nil;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
-(void)segmentAction:(UISegmentedControl*)seg{
    //清除已经显示的弹出窗口
    if (self.popover.popoverVisible) {
        [self.popover dismissPopoverAnimated:YES];

    }
    //初始化待显示控制器
    popController* controller = [[popController alloc] init];
    //设置待显示控制器的范围
    [controller.view setFrame:CGRectMake(550, 490, 350, 450)];
    //设置待显示控制器视图的尺寸
    controller.contentSizeForViewInPopover = CGSizeMake(350, 450);
    //初始化弹出窗口
    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:controller];
    controller.popover = pop;
    self.popover = pop;
    self.popover.delegate = self;
    //设置弹出窗口尺寸
    self.popover.popoverContentSize = CGSizeMake(350, 450);
    
    //显示,其中坐标为箭头的坐标以及尺寸
    [self.popover presentPopoverFromRect:CGRectMake(550, 890, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    
    [controller release];
    [pop release];
    
    //[self.popover dismissPopoverAnimated:YES];
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popover dismiss");
}

@end
#import <UIKit/UIKit.h>

@interface popController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
    UIPopoverController* _popover;
    BOOL isChelsea;
}
@property(nonatomic,retain)UIPopoverController* popover;
@end
#import "popController.h"

@implementation popController
@synthesize popover = _popover;

//定义选项显示内容
const char* chelsea[12] = {"博阿斯","切赫","阿什利科尔","特里","路易斯","伊万诺维奇","兰帕德","拉米雷斯","梅来雷斯","阿内尔卡","德罗巴","托雷斯"};
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
  }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    isChelsea = NO;
    
    //添加完成按钮
    UISegmentedControl* doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Done", nil]];
    [doneButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [doneButton setFrame:CGRectMake(self.view.bounds.origin.x+self.view.bounds.size.width-100, self.view.bounds.origin.y+5, 75, 50)];
    [doneButton addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:doneButton];
    [doneButton release];

    //添加选择项
    UISegmentedControl* clubSeg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Chelsea",@"AC Milan",@"Livupoor",@"Asenal",nil]];
    [clubSeg setSegmentedControlStyle:UISegmentedControlStyleBar];
    [clubSeg setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75,self.view.bounds.size.width,50)];
  [clubSeg addTarget:self action:@selector(chooseClub:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:clubSeg];
    [clubSeg release];
    
    //添加选择列表
    UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+150, self.view.bounds.size.width,self.view.bounds.size.height)];
    [picker setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75, self.view.bounds.size.width,self.view.bounds.size.height)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];
    [picker setTag:101];
    [picker release];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 12;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (isChelsea) {
        return [NSString stringWithCString:chelsea[row] encoding:NSUTF8StringEncoding];
    }else{
        return [NSString stringWithFormat:@"chelsea_%d",arc4random()%12];
        ;
    }
    
}
-(void)segmentAction:(UISegmentedControl*)seg{
    [self.view removeFromSuperview];
  [self.popover dismissPopoverAnimated:YES];
}
-(void)chooseClub:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    UIPickerView* picker = (UIPickerView*)[self.view viewWithTag:101];
    switch (index) {
        case 0:{
            NSLog(@"you choose Chelsea");
            isChelsea = YES;
            [picker reloadAllComponents];
            break;
        }
        case 1:
            NSLog(@"you choose AC Milan");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 2:
            NSLog(@"you choose Livopoor");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 3:
            NSLog(@"you choose Asenal");
            isChelsea = NO;
            [picker reloadAllComponents];
        default:
            break;
    }
}
@end



@property(nonatomic,retain)UIPopoverController* popover;
@end
#import <UIKit/UIKit.h>

@interface UIActionSheetDemoViewController : UIViewController<UIPopoverControllerDelegate>{
    UIPopoverController* _popover;
}
#import "UIActionSheetDemoViewController.h"
#import "popController.h"
@implementation UIActionSheetDemoViewController
@synthesize popover = _popover;
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UISegmentedControl* settingView = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"FootballCF",@"BasketballCF",nil]];
    [settingView setSegmentedControlStyle:UISegmentedControlStyleBar];
    [settingView setFrame:CGRectMake(500, 900, 160, 75)];
    [settingView setNeedsLayout];
    [settingView addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:settingView];
    [settingView release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    if (self.popover != nil) {
        [self.popover release];
        self.popover = nil;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
-(void)segmentAction:(UISegmentedControl*)seg{
    //清除已经显示的弹出窗口
    if (self.popover.popoverVisible) {
        [self.popover dismissPopoverAnimated:YES];

    }
    //初始化待显示控制器
    popController* controller = [[popController alloc] init];
    //设置待显示控制器的范围
    [controller.view setFrame:CGRectMake(550, 490, 350, 450)];
    //设置待显示控制器视图的尺寸
    controller.contentSizeForViewInPopover = CGSizeMake(350, 450);
    //初始化弹出窗口
    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:controller];
    controller.popover = pop;
    self.popover = pop;
    self.popover.delegate = self;
    //设置弹出窗口尺寸
    self.popover.popoverContentSize = CGSizeMake(350, 450);
    
    //显示,其中坐标为箭头的坐标以及尺寸
    [self.popover presentPopoverFromRect:CGRectMake(550, 890, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    
    [controller release];
    [pop release];
    
    //[self.popover dismissPopoverAnimated:YES];
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popover dismiss");
}

@end
#import <UIKit/UIKit.h>

@interface popController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
    UIPopoverController* _popover;
    BOOL isChelsea;
}
@property(nonatomic,retain)UIPopoverController* popover;
@end
#import "popController.h"

@implementation popController
@synthesize popover = _popover;

//定义选项显示内容
const char* chelsea[12] = {"博阿斯","切赫","阿什利科尔","特里","路易斯","伊万诺维奇","兰帕德","拉米雷斯","梅来雷斯","阿内尔卡","德罗巴","托雷斯"};
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
  }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    isChelsea = NO;
    
    //添加完成按钮
    UISegmentedControl* doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Done", nil]];
    [doneButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [doneButton setFrame:CGRectMake(self.view.bounds.origin.x+self.view.bounds.size.width-100, self.view.bounds.origin.y+5, 75, 50)];
    [doneButton addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:doneButton];
    [doneButton release];

    //添加选择项
    UISegmentedControl* clubSeg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Chelsea",@"AC Milan",@"Livupoor",@"Asenal",nil]];
    [clubSeg setSegmentedControlStyle:UISegmentedControlStyleBar];
    [clubSeg setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75,self.view.bounds.size.width,50)];
  [clubSeg addTarget:self action:@selector(chooseClub:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:clubSeg];
    [clubSeg release];
    
    //添加选择列表
    UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+150, self.view.bounds.size.width,self.view.bounds.size.height)];
    [picker setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75, self.view.bounds.size.width,self.view.bounds.size.height)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];
    [picker setTag:101];
    [picker release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 12;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (isChelsea) {
        return [NSString stringWithCString:chelsea[row] encoding:NSUTF8StringEncoding];
    }else{
        return [NSString stringWithFormat:@"chelsea_%d",arc4random()%12];
        ;
    }
    
}
-(void)segmentAction:(UISegmentedControl*)seg{
    [self.view removeFromSuperview];
  [self.popover dismissPopoverAnimated:YES];
}
-(void)chooseClub:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    UIPickerView* picker = (UIPickerView*)[self.view viewWithTag:101];
    switch (index) {
        case 0:{
            NSLog(@"you choose Chelsea");
            isChelsea = YES;
            [picker reloadAllComponents];
            break;
        }
        case 1:
            NSLog(@"you choose AC Milan");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 2:
            NSLog(@"you choose Livopoor");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 3:
            NSLog(@"you choose Asenal");
            isChelsea = NO;
            [picker reloadAllComponents];
        default:
            break;
    }
}
@end



@property(nonatomic,retain)UIPopoverController* popover;
@end
#import <UIKit/UIKit.h>

@interface UIActionSheetDemoViewController : UIViewController<UIPopoverControllerDelegate>{
    UIPopoverController* _popover;
}
#import "UIActionSheetDemoViewController.h"
#import "popController.h"
@implementation UIActionSheetDemoViewController
@synthesize popover = _popover;
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    UISegmentedControl* settingView = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"FootballCF",@"BasketballCF",nil]];
    [settingView setSegmentedControlStyle:UISegmentedControlStyleBar];
    [settingView setFrame:CGRectMake(500, 900, 160, 75)];
    [settingView setNeedsLayout];
    [settingView addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:settingView];
    [settingView release];
}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    
    if (self.popover != nil) {
        [self.popover release];
        self.popover = nil;
    }
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
-(void)segmentAction:(UISegmentedControl*)seg{
    //清除已经显示的弹出窗口
    if (self.popover.popoverVisible) {
        [self.popover dismissPopoverAnimated:YES];

    }
    //初始化待显示控制器
    popController* controller = [[popController alloc] init];
    //设置待显示控制器的范围
    [controller.view setFrame:CGRectMake(550, 490, 350, 450)];
    //设置待显示控制器视图的尺寸
    controller.contentSizeForViewInPopover = CGSizeMake(350, 450);
    //初始化弹出窗口
    UIPopoverController* pop = [[UIPopoverController alloc] initWithContentViewController:controller];
    controller.popover = pop;
    self.popover = pop;
    self.popover.delegate = self;
    //设置弹出窗口尺寸
    self.popover.popoverContentSize = CGSizeMake(350, 450);
    
    //显示,其中坐标为箭头的坐标以及尺寸
    [self.popover presentPopoverFromRect:CGRectMake(550, 890, 10, 10) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
    
    [controller release];
    [pop release];
    
    //[self.popover dismissPopoverAnimated:YES];
}

-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController{
    NSLog(@"popover dismiss");
}

@end
#import <UIKit/UIKit.h>

@interface popController : UIViewController<UIPickerViewDelegate,UIPickerViewDataSource>{
    UIPopoverController* _popover;
    BOOL isChelsea;
}
@property(nonatomic,retain)UIPopoverController* popover;
@end
#import "popController.h"

@implementation popController
@synthesize popover = _popover;

//定义选项显示内容
const char* chelsea[12] = {"博阿斯","切赫","阿什利科尔","特里","路易斯","伊万诺维奇","兰帕德","拉米雷斯","梅来雷斯","阿内尔卡","德罗巴","托雷斯"};
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        
  }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    isChelsea = NO;
    
    //添加完成按钮
    UISegmentedControl* doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Done", nil]];
    [doneButton setSegmentedControlStyle:UISegmentedControlStyleBar];
    [doneButton setFrame:CGRectMake(self.view.bounds.origin.x+self.view.bounds.size.width-100, self.view.bounds.origin.y+5, 75, 50)];
    [doneButton addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:doneButton];
    [doneButton release];

    //添加选择项
    UISegmentedControl* clubSeg = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"Chelsea",@"AC Milan",@"Livupoor",@"Asenal",nil]];
    [clubSeg setSegmentedControlStyle:UISegmentedControlStyleBar];
    [clubSeg setFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75,self.view.bounds.size.width,50)];
  [clubSeg addTarget:self action:@selector(chooseClub:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:clubSeg];
    [clubSeg release];
    
    //添加选择列表
    UIPickerView* picker = [[UIPickerView alloc] initWithFrame:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+150, self.view.bounds.size.width,self.view.bounds.size.height)];
    [picker setBounds:CGRectMake(self.view.bounds.origin.x, self.view.bounds.origin.y+75, self.view.bounds.size.width,self.view.bounds.size.height)];
    picker.delegate = self;
    picker.dataSource = self;
    picker.showsSelectionIndicator = YES;
    [self.view addSubview:picker];
    [picker setTag:101];
    [picker release];
    
    
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return YES;
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return 12;
}
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (isChelsea) {
        return [NSString stringWithCString:chelsea[row] encoding:NSUTF8StringEncoding];
    }else{
        return [NSString stringWithFormat:@"chelsea_%d",arc4random()%12];
        ;
    }
    
}
-(void)segmentAction:(UISegmentedControl*)seg{
    [self.view removeFromSuperview];
  [self.popover dismissPopoverAnimated:YES];
}
-(void)chooseClub:(UISegmentedControl*)seg{
    NSInteger index = seg.selectedSegmentIndex;
    UIPickerView* picker = (UIPickerView*)[self.view viewWithTag:101];
    switch (index) {
        case 0:{
            NSLog(@"you choose Chelsea");
            isChelsea = YES;
            [picker reloadAllComponents];
            break;
        }
        case 1:
            NSLog(@"you choose AC Milan");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 2:
            NSLog(@"you choose Livopoor");
            isChelsea = NO;
            [picker reloadAllComponents];
        case 3:
            NSLog(@"you choose Asenal");
            isChelsea = NO;
            [picker reloadAllComponents];
        default:
            break;
    }
}
@end



不积跬步无以至千里,闲来无事,搞一个属于自己的小站,如果看到这篇文章感觉对你有帮助的话,就支持关注一下我的小站: 我的小站



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值