从 UIPicker 进行选择时尝试显示图像和标签
Posted
技术标签:
【中文标题】从 UIPicker 进行选择时尝试显示图像和标签【英文标题】:Trying to display an image and label when making a selection from the UIPicker 【发布时间】:2013-05-02 18:17:29 【问题描述】:我有一个极客姓名数组和每个极客职业的数组。极客名称应该出现在 UIPicker 中。选择名称后,选择器的底部应显示所选玩家的图像和职业。我无法将图像作为 _ImageView = @[@"....我收到以下消息:
2013-05-02 13:40:03.761 Assignment5[2968:c07] -[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7549d30
2013-05-02 13:40:03.780 Assignment5[2968:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIView numberOfComponentsInPickerView:]: unrecognized selector sent to instance 0x7549d30'
*** First throw call stack:
(0x1c92012 0x10cfe7e 0x1d1d4bd 0x1c81bbc 0x1c8194e 0xc634 0xd25f 0x642dd 0x10e36b0 0x228efc0 0x228333c 0x228eeaf 0x1032bd 0x4bb56 0x4a66f 0x4a589 0x497e4 0x4961e 0x4a3d9 0x4d2d2 0xf799c 0x44574 0x4476f 0x44905 0x4d917 0x1196c 0x1294b 0x23cb5 0x24beb 0x16698 0x1beddf9 0x1bedad0 0x1c07bf5 0x1c07962 0x1c38bb6 0x1c37f44 0x1c37e1b 0x1217a 0x13ffc 0x1e9d 0x1dc5)
libc++abi.dylib: terminate called throwing an exception
这是我的代码:
ViewController.h
#import <UIKit/UIKit.h>
@interface Assignment5ViewController : UIViewController
<UIPickerViewDelegate, UIPickerViewDataSource>
@property (strong, nonatomic) IBOutlet UIPickerView *picker;
@property (strong, nonatomic) IBOutlet UILabel *career;
@property (strong, nonatomic) IBOutlet UIImageView *geekImageView;
@property (strong, nonatomic)NSArray *geekNames;
@property (strong, nonatomic)NSArray *careerNames;
@end
ViewController.m
@interface Assignment5ViewController ()
@end
@implementation Assignment5ViewController
- (void)viewDidLoad
[super viewDidLoad];
_geekNames = @[@"Alan Turing", @"Bill Gates", @"Dennis Ritchie", @"James Gosling", @"Larry Wall", @"Linus Torvalds", @"Mark Zuckerberg", @"Steve Jobs", @"Tim Berners-Lee", @"Vinton Cerf"];
_careerNames = @[@"Conceived Turing Machine", @"Started Microsoft", @"Created C and Unix", @"Created Java Language", @"Created Perl Language", @"Created Linux OS", @"Created Facebook", @"Started Apple", @"Created World-Wide-Web", @"Created TCP/IP"];
- (void)didReceiveMemoryWarning
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
- (IBAction)textFieldReturn:(id)sender
[sender resignFirstResponder];
#pragma mark
#pragma mark PickerView DataSource
- (NSInteger)numberofComponentsInPickerView:(UIPickerView *)pickerView
return 1;
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
return _geekNames.count;
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
return [_geekNames objectAtIndex:row];
#pragma mark -
#pragma mark PickerView Delegate
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component
_careerNames = [[NSString alloc]initWithFormat:@"%@", [_geekNames objectAtIndex:row]];
@end
【问题讨论】:
【参考方案1】:从该错误看来,您已将选择器的数据源设置为 UIView 而不是您的视图控制器。你是如何设置数据源的?你是在IB做的吗?您应该删除该连接并重做。
【讨论】:
我该怎么做?我很抱歉,但我以前从未这样做过,我非常困惑。我从来没有编写过目标 c,所以我试图理解它。 @user1787078,你是怎么做的?删除连接并重做?你一开始就建立联系了吗?你是怎么做到的? 是的,但我看过一本书。仍在努力掌握这一切。 @user1787078,如果您在 IB 中的选取器视图上右键单击(或控制单击),它将弹出一个黑色窗口,其中将显示所有连接。 “dataSource”右边是什么意思? 是的,我同时选择了数据源和委托。一旦我这样做并运行它,我就得到了一个黑匣子。好的,我想我现在知道你在说什么了。我会尝试一下。谢谢!以上是关于从 UIPicker 进行选择时尝试显示图像和标签的主要内容,如果未能解决你的问题,请参考以下文章