gpt4 book ai didi

objective-c - UIPickerView: numberOfRowsInComponent 被同一个组件调用两次并且不显示行标题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:56:39 25 4
gpt4 key购买 nike

我正在尝试为我的应用程序设置一个登录表单,但 UIPickerView 没有显示行数据。我看到的是 numberOfRowsInComponent 被调用了两次?但是 titleForRow 每行只被调用一次。

调试输出:

2011-06-07 11:07:31.096 myECG[19689:207] numberOfRowsInComponent: 2 : 0
2011-06-07 11:07:31.096 myECG[19689:207] numberOfRowsInComponent: 2 : 0
2011-06-07 11:07:31.098 myECG[19689:207] USA : 0
2011-06-07 11:07:31.098 myECG[19689:207] Canada : 0

LoginViewController.h

#import <UIKit/UIKit.h>


@interface LoginViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate> {
IBOutlet UITextField *emailField;
IBOutlet UITextField *passswordField;
IBOutlet UIButton *loginButton;
IBOutlet UIActivityIndicatorView *loginIndicator;
IBOutlet UIPickerView *pickerView;
NSMutableArray *sites;

}

@property (nonatomic, retain) UITextField *emailField;
@property (nonatomic, retain) UITextField *passwordField;
@property (nonatomic, retain) UIButton *loginButton;
@property (nonatomic, retain) UIActivityIndicatorView *loginIndicator;
@property (nonatomic, retain) UIPickerView *pickerView;
@property (nonatomic, retain) NSMutableArray *sites;

-(IBAction) login:(id) sender;
@end

LoginViewController.m #import "LoginViewController.h" #import "myECGViewController.h"

@implementation LoginViewController
@synthesize emailField, passwordField, loginButton, loginIndicator, pickerView, sites;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization

}

//NSLog(@"%@", sites);
return self;
}

- (void)dealloc
{
[super dealloc];
}

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];

// Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{

[super viewDidLoad];
sites = [[NSMutableArray alloc] init];
[sites addObject:@"Canada"];
[sites addObject:@"USA"];

//[pickerView selectRow:1 inComponent:0 animated:YES];
// Do any additional setup after loading the view from its nib.
}

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

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return NO;
}
-(IBAction) login:(id) sender
{
loginIndicator.hidden = FALSE;
[loginIndicator startAnimating];
loginButton.enabled = FALSE;
// Do login
NSLog(@"E:%@ P:%@", emailField.text, passswordField.text);
}

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)thePickerView
{
return 1;
}

-(NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
NSLog(@"%@ : %d", [sites objectAtIndex:row], component);
return [sites objectAtIndex:row];
}
- (NSInteger)pickerView:(UIPickerView *)thePickerView numberOfRowsInComponent:(NSInteger)component
{
NSLog(@"numberOfRowsInComponent: %d : %d", [sites count], component);
return [sites count];
}
-(void)pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"Selected: %@", [sites objectAtIndex:row]);
}
@end

感谢任何帮助。

最佳答案

我试过你的代码,它工作正常。我可以在 pickerView 中看到两行,一排是美国,一排是加拿大 see the image below

确保您已正确设置委托(delegate)和数据源。

关于objective-c - UIPickerView: numberOfRowsInComponent 被同一个组件调用两次并且不显示行标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6267383/

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