弹出 UIPickerView (iphone)

发布于 2024-12-07 16:21:29 字数 2666 浏览 1 评论 0原文

我正在创建一个 textField,当单击 UIPickerView 时会出现。 当我运行此代码时出现错误: 线程1:Profram接收信号:“SIGABRT”

我对 iPhone 开发还很陌生,但负责人不在,我正在接一个项目。

如果您能让我知道这有什么问题...

这是 ViewController.h 的样子:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

IBOutlet UIPickerView *picker;
IBOutlet UITextField *text;

}


- (IBAction)showPicker:(id)sender;


@end

ViewController.m

#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
 }

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}


- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;


}
}

- (void)showPicker {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
picker.center = CGPointMake(160, 240);
[UIView commitAnimations];


if (!self.navigationItem.rightBarButtonItem) {
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
    [self.navigationItem setRightBarButtonItem:doneButton animated:YES];
  //  [doneButton release];
}   
}


- (void)hidePicker {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
picker.center = CGPointMake(160, 240);
[UIView commitAnimations];


[self.navigationItem setRightBarButtonItem:nil animated:YES];
}


- (void)done:(id)sender {

[self hidePicker];

[self.navigationItem setRightBarButtonItem:nil animated:YES];
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

[self showPicker];

return NO;
}

I am creating a textField which when clicked UIPickerView comes up.
When I run this code there is an error:
Thread 1:Profram received signal:"SIGABRT".

I am quite new on iPhone development, but the person in charge is away and I am taking on a project.

If you could let me know what's wrong with this...

This is how the ViewController.h looks like:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {

IBOutlet UIPickerView *picker;
IBOutlet UITextField *text;

}


- (IBAction)showPicker:(id)sender;


@end

and ViewController.m:

#import "ViewController.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
     // e.g. self.myOutlet = nil;
 }

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}


- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
    return YES;


}
}

- (void)showPicker {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
picker.center = CGPointMake(160, 240);
[UIView commitAnimations];


if (!self.navigationItem.rightBarButtonItem) {
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]     initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(done:)];
    [self.navigationItem setRightBarButtonItem:doneButton animated:YES];
  //  [doneButton release];
}   
}


- (void)hidePicker {

[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.4];
[UIView setAnimationDelegate:self];
picker.center = CGPointMake(160, 240);
[UIView commitAnimations];


[self.navigationItem setRightBarButtonItem:nil animated:YES];
}


- (void)done:(id)sender {

[self hidePicker];

[self.navigationItem setRightBarButtonItem:nil animated:YES];
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

[self showPicker];

return NO;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的他你的她 2024-12-14 16:21:29
- (IBAction)showPicker:(id)sender;

这个函数没有在你的 .m 文件中实现,而是使用上面的 -(void) showPicker...

以及这个有什么用...

- (void)done:(id)sender
- (IBAction)showPicker:(id)sender;

this function is not implemented in your .m file instead of -(void) showPicker use the above one...

and what is the use of this...

- (void)done:(id)sender
苍风燃霜 2024-12-14 16:21:29

选择器视图必须有一个数据源来获取要显示的数据和一个委托来响应事件。通常,我会通过采用相应的协议 UIPickerViewDelegate 和 UIPickerViewDataSource 将控制器定义为两者。您的控制器没有,这可能意味着数据源和委托位于另一个对象内。您应该检查界面生成器,紧紧单击选择器,然后查看出现的委托和数据源。如果您发现控制器以外的其他类,那么您应该在其中查找错误。如果您找到控制器的类,那么问题是您没有实现填充选择器视图的方法。作为参考,选择器至少正在寻找数据源方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

祝狩猎好运。

The picker view must have a data source to get the data to display and a delegate to respond to events. Typically I would define the controller as being both by adopting the corresponding protocols UIPickerViewDelegate and UIPickerViewDataSource. Your controller doesn't, this may mean that the data source and delegate are within another object. You should check in interface builder, tight click on the picker and see what comes up as being he delegate and data source. If you find another class than your controller that's where you should look for the bug. If you find your controller's class then the issue is you didn't implement the methods for the picker view to be populated. For reference the picker is looking at least for the data source methods :

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

Good luck for the hunting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文