显示和隐藏UIPickerview时出错

时间:2014-02-26 13:23:03

标签: ios ios7 uipickerview

我是x-code字的新手,我需要你的帮助。

我正在尝试编写代码来显示和隐藏UIpickerview。

当我按下视图控制器中的选择按钮时,Uipickerview必须显示,当我按下工具栏中的完成按钮然后在标签中显示所选数据时隐藏

这是我的代码..

这是.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
<UIPickerViewDelegate,UIPickerViewDataSource>
{
    IBOutlet UIPickerView *NumExamPicker;
    NSMutableArray *NumExamName;
}

@property (weak, nonatomic) IBOutlet UILabel *lbout;
@property (strong, nonatomic) IBOutlet UIView *PickerContainer;
- (IBAction)SelectButton:(id)sender;
- (IBAction)DoneButton:(id)sender;

@end

这是我的.m文件

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize lbout;
@synthesize PickerContainer;


- (void)viewDidLoad
{
    [super viewDidLoad];
    NumExamName = [[NSMutableArray alloc] init];
    [NumExamName addObject:@"One"];
    [NumExamName addObject:@"Two"];
    [NumExamName addObject:@"Three"];
    [NumExamName addObject:@"Four"];

    PickerContainer.frame = CGRectMake(0, 600, 320, 206);
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [NumExamName count];
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    lbout.text = [NumExamName objectAtIndex:row];
}

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

- (IBAction)SelectButton:(id)sender {

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    PickerContainer.frame = CGRectMake(0, 362, 320, 206);
    [UIView commitAnimations];
}

- (IBAction)DoneButton:(id)sender {
    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationDuration:0.3];
    PickerContainer.frame = CGRectMake(0, 600, 320, 206);
    [UIView commitAnimations];
}
@end

当我运行模拟器时,它崩溃并停在此方法:

//  main.m
//  showandhideuipickerview

#import <UIKit/UIKit.h>

#import "AppDelegate.h"

int main(int argc, char * argv[])
{
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    }
}

我不知道如何解决它。

哦,还有一件事,我正在处理一个单一的视图,如果我不希望它成为第一视图,它会是一样的吗?或者我应该添加更多代码。

感谢...

1 个答案:

答案 0 :(得分:1)

请添加UIPickerView的委托方法

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent: (NSInteger)component {
    return [NumExamName objectAtIndex:row];
}