gpt4 book ai didi

ios - 显示和隐藏 UIPickerview 时出错

转载 作者:行者123 更新时间:2023-11-29 12:52:40 25 4
gpt4 key购买 nike

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

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

当我按下 View Controller 中的选择按钮时,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]));
}
}

我不知道怎么解决。

还有一件事,我正在使用一个 View ,如果我不希望它成为第一个 View ,它会一样吗?或者我应该添加更多代码。

谢谢...

最佳答案

请添加这个UIPickerView的委托(delegate)方法

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

关于ios - 显示和隐藏 UIPickerview 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22042757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com