我是x代码单词的新手,我需要你的帮助。
我正在尝试编写一个代码来显示和隐藏 UIpickerview。
当我按下视图控制器中的选择按钮时,Uipicker视图必须显示,当我按下工具栏中的完成按钮时隐藏,然后在标签中显示所选数据
这是我的代码。
这是 .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]));
}
}
我不知道如何解决它。
哦,还有一件事,我正在使用一个视图,如果我不希望它不是第一个视图,它会是一样的吗? 或者我应该添加更多代码。
谢谢。。。
请添加UIPickerView的委托方法
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent: (NSInteger)component {
return [NumExamName objectAtIndex:row];
}