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

发布于 2024-11-17 07:36:30 字数 1416 浏览 5 评论 0原文

我正在尝试学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原谅过去的我 2024-11-24 07:36:30

看来你遇到了我几天前遇到的问题,我真的不认为这是一个错误,我认为问题出在调用所有内容时(可能是错误的)。如果我错了,请纠正我,但您希望 PickerView 在第一次正确显示在屏幕上时显示某个值?您是在代码中还是从笔尖创建 PickerView?

在我的应用程序中,以下代码片段位于我的 viewDidLoad 方法的底部,

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

这按预期工作。请发布您的代码片段,以便我们更好地理解您的问题。

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

[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:

眼眸印温柔 2024-11-24 07:36:30

显然,某些版本的 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].

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文