何时在刚刚加载的 UIPickerView 上调用 selectRow: ?
我正在尝试学习 iPhone/iOS 编程。我有一个 UIPickerView,一旦它变得可见(它包含在 FlippSideView 上),它就应该显示它所选择的行。
不幸的是,flipSideViewController 的 awakeFromNib
没有被调用。在 viewDidLoad
中执行此操作有点太晚了。 那么,如何让 pickerView 在所选行可见时立即显示它呢?
更新:
这是我显示翻转视图的方式
- (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
在翻转控制器中有一个方法 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); }
,当程序运行时,它会生成日志
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)
I am trying to learn iPhone/iOS programming. I have an UIPickerView that should dispay its selected row as soon as it becomes visible (it is contained on a flippSideView).
Unfortunately, the flipSideViewController's awakeFromNib
is not called. It is somewhat too late to do it in viewDidLoad
.
So, how can I make the pickerView display the selected row as soon it becomes visible?
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)
-(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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来你遇到了我几天前遇到的问题,我真的不认为这是一个错误,我认为问题出在调用所有内容时(可能是错误的)。如果我错了,请纠正我,但您希望 PickerView 在第一次正确显示在屏幕上时显示某个值?您是在代码中还是从笔尖创建 PickerView?
在我的应用程序中,以下代码片段位于我的 viewDidLoad 方法的底部,
这按预期工作。请发布您的代码片段,以便我们更好地理解您的问题。
PS:当为 iOS 开发时,我会远离 awakeFromNib 并只使用 ViewDidLoad:
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? Are you creating the PickerView in code or from a nib?
In my app, the below code snippet is right at the bottom of my viewDidLoad method
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:
显然,某些版本的 iOS 中存在一个错误,这意味着您的视图只有在调用
[super viewWillAppear:animated]
或通过调用[self view]
强制加载时才会加载>。There apparently is a bug in some versions of iOS that means your view is not loaded until you call
[super viewWillAppear:animated]
or force it to load by calling[self view]
.