从UIAlert按钮触发UIPicker

时间:2011-04-02 02:06:31

标签: iphone button sdk uipickerview uialertview

这对我来说似乎是最难的事情。如何从UIAlert按钮触发UIPicker。请帮忙。

1 个答案:

答案 0 :(得分:0)

要记住的是,设置警报视图的视图控制器将显示选择器视图,而警报本身。要执行此操作,视图控制器需要将其自身设置为警报视图的委托。我创建了一个非常简单的基于视图的应用程序来显示这个,这里是根视图控制器的代码:

//
//  PickerAlertViewController.h
//  PickerAlert
//

#import <UIKit/UIKit.h>

@interface PickerAlertViewController : UIViewController <UIAlertViewDelegate>
{
}

- (IBAction) pickerButtonAction;

@end

//  PickerAlertViewController.m
//  PickerAlert
//

#import "PickerAlertViewController.h"

@implementation PickerAlertViewController

- (IBAction) pickerButtonAction
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"Pick Something" 
                                                    message: @"Touch 'Cancel' if you don't wish to make a selection." 
                                                   delegate: self 
                                          cancelButtonTitle: @"Cancel"
                                          otherButtonTitles: @"Picker", nil];
    [alert show];
    [alert release];

}

#pragma mark -
#pragma mark Alert View Delegate Method

- (void) alertView: (UIAlertView *) actionSheet clickedButtonAtIndex: (NSInteger) buttonIndex
{
    if ( buttonIndex == 0)
    {
        NSLog( @"Cancelled");
        // Nothing left to do since the alert has been dismissed.
    }
    else
    {
        NSLog( @"Picker Button selected.");
        // Set up a picker view either programmatically or load from a nib then either
        //  push it on to the navigation stack or present it modaly.
    }
}

#pragma mark -
#pragma mark View Controller Memory Management

- (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)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end

我在“界面”构建器中添加了一个按钮,并将其连接到“pickerButtonAction”方法。在头文件中,我指定'PickerAlertViewController'将遵守UIAlertViewDelegate协议。

设置警报时,其委托设置为self。我用来获取用户选择的委托方法是:

  • (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

我希望这对你有用。