简体   繁体   English

何时在刚刚加载的UIPickerView上调用selectRow :?

[英]When to call selectRow: on a UIPickerView that has just loaded?

I am trying to learn iPhone/iOS programming. 我正在尝试学习iPhone / iOS编程。 I have an UIPickerView that should dispay its selected row as soon as it becomes visible (it is contained on a flippSideView). 我有一个UIPickerView,它在可见时应立即付清其选定的行(它包含在flippSideView中)。

Unfortunately, the flipSideViewController's awakeFromNib is not called. 不幸的是,未调用flipSideViewController的awakeFromNib It is somewhat too late to do it in viewDidLoad . viewDidLoad执行该操作为时已晚。 So, how can I make the pickerView display the selected row as soon it becomes visible? 因此,如何使pickerView在显示选定行后立即显示它?

Update: 更新:

Here is how I show the flipside view 这是我显示反面视图的方式

- (IBAction)showInfo:(id)sender {    

    FlipsideViewController *controller = [[FlipsideViewController alloc] initWithNibName:@"FlipsideView" bundle:nil];
    controller.delegate = self;
    controller.uData = userData;

    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    controller.pickerView.delegate   = userData;
    controller.pickerView.dataSource = userData;

    [controller release];
}// showInfo

In the flipside controller there is a method mySelect (to help me trace trace the calls) 在flipside控制器中,有一个方法mySelect(以帮助我跟踪跟踪调用)

-(void) mySelect:(NSString*) strMethod{
    int row = [uData getCurrentUserRow];
    [pickerView selectRow:row inComponent:0 animated:NO];        
    NSLog(@"selectRow %d called from %@  (pickerView=%d uData=%d)", row, strMethod, (int)pickerView, (int)uData); }

and when the program runs it generates the log 并在程序运行时生成日志

selectRow 3 called from viewDidLoad  (pickerView=87412720 uData=89267696) 
selectRow 3 called from viewWillAppear (pickerView=87412720 uData=89267696) 
selectRow 3 called from viewDidAppear (pickerView=87412720 uData=89267696)

It seems you're having the problem i had a couple days ago, i really don't think it's a bug I think the problem is when everything is called (could be wrong). 看来您遇到了我几天前遇到的问题,我真的不认为这是一个错误,我认为问题是什么都被调用时(可能是错误的)。 Correct me if I'm wrong but you want your PickerView to display a certain value when it's first shown onscreen correct? 如果我错了,请纠正我,但是您希望您的PickerView在屏幕上首次显示时显示某个值是否正确? Are you creating the PickerView in code or from a nib? 您是用代码还是通过笔尖创建PickerView?

In my app, the below code snippet is right at the bottom of my viewDidLoad method 在我的应用中,下面的代码段就在我的viewDidLoad方法的底部

[pickerView selectRow: 100 inComponent:0 animated:YES];

This works as intended. 这按预期工作。 Please post a snippet of your code so we can better understand your problem. 请发布您的代码段,以便我们更好地理解您的问题。

PS: When developing for iOS i would stay away from awakeFromNib and just use ViewDidLoad: PS:为iOS开发时,我会远离awakeFromNib,而只使用ViewDidLoad:

显然,在某些版本的iOS中存在一个错误,这意味着您必须先调用[super viewWillAppear:animated]或通过调用[self view]强制加载[self view] ,才能加载[self view]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM