我在目标C中创建了一个Picker视图,并用NSArray填充它,一切正常,它编译并在屏幕上显示但是选择器没有做任何事情。我想转向,因为用户拖动他们的手指,就像你期望的那样,但无论如何它都没有反应。
'lil帮助?
我用于选择器的代码来自“开始iPhone开发”,但我在屏幕上有更多的视图(地图视图和我用于绘图的视图),为清楚起见,我可以将所有代码转储到这里,所以我将它限制在我认为相关的范围内。
// SingleComponentPickerViewController.h
#import <UIKit/UIKit.h>
@interface SingleComponentPickerViewController : UIViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIPickerView *singlePicker;
NSArray *pickerData;
}
@property (nonatomic, retain) UIPickerView *singlePicker;
@property (nonatomic, retain) NSArray *pickerData;
- (IBAction)buttonPressed:(id)sender;
@end
// SingleComponentPickerViewController.m
#import "SingleComponentPickerViewController.h"
@implementation SingleComponentPickerViewController
@synthesize singlePicker;
@synthesize pickerData;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Initialization code
}
return self;
}
- (IBAction)buttonPressed:(id)sender
{
NSInteger row = [singlePicker selectedRowInComponent:0];
NSString *selected = [pickerData objectAtIndex:row];
NSString *title = [[NSString alloc] initWithFormat:@"You selected %@!", selected];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:@"Thank you for choosing." delegate:nil cancelButtonTitle:@"You're Welcome" otherButtonTitles:nil];
[alert show];
[alert release];
[title release];
}
- (void)viewDidLoad {
NSArray *array = [[NSArray alloc] initWithObjects:@"Luke", @"Leia", @"Han", @"Chewbacca", @"Artoo", @"Threepio", @"Lando", nil];
self.pickerData = array;
[array release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}
- (void)dealloc {
[singlePicker release];
[pickerData release];
[super dealloc];
}
#pragma mark -
#pragma mark Picker Data Source Methods
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return [pickerData count];
}
#pragma mark Picker Delegate Methods
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return [pickerData objectAtIndex:row];
}
@end
这就是代码,但我觉得问题更多的是如何放置/交互视图。
答案 0 :(得分:0)
将“useractionEnabled”属性设置为true(在IB或代码中)?这应该是默认值。
但正如上面评论中提到的......某些代码可能会有所帮助:)