【问题标题】:Trigger a UIPicker from a UIAlert button从 UIAlert 按钮触发 UIPicker
【发布时间】:2011-04-02 02:06:30
【问题描述】:

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

【问题讨论】:

    标签: iphone button sdk uipickerview uialertview


    【解决方案1】:

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

    //
    //  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 协议。

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

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

    我希望这对你有用。

    【讨论】:

    • 效果很好,谢谢。我如何让选取器从底部向上滑动,而不是使用呈现不同视图的当前模态视图。那样看起来不太顺畅。
    • 没关系。感谢您的样品。帮了大忙。
    猜你喜欢
    • 1970-01-01
    • 2015-05-29
    • 2011-09-16
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多