【问题标题】:Error in showing and hiding UIPickerview显示和隐藏 UIPickerview 时出错
【发布时间】:2014-02-26 13:23:02
【问题描述】:

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

我正在尝试编写代码来显示和隐藏 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]));
    }
}

不知道怎么解决。

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

谢谢...

【问题讨论】:

  • 你是否在 InterfaceBuilder 中定义了NumExamPicker,即它是否已经是 PickerContainer 中包含的视图?
  • @Flexicoder 我已将 uipickerview 拖放到 .h 文件中。这是您的意思吗?...我将 uipickerview 放在视图中并将其命名为 PickerContainer
  • 所以您已经直观地布置了 UI 元素,并拖到 .h 以创建属性?在您的代码中,您拥有的 3 个位置 PickerContainer.frame = 将其更改为 self.PickerContainer.frame = 可以解决问题吗?
  • 在哪个按钮崩溃?当按下完成按钮或选择选择器行时?
  • 选择器代表很好???你设置它们了吗?

标签: ios ios7 uipickerview


【解决方案1】:

请添加 UIPickerView 的这个委托方法

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多