gpt4 book ai didi

objective-c - 如何从另一个类访问 xib 中的 UIPicker?

转载 作者:行者123 更新时间:2023-11-28 22:50:31 26 4
gpt4 key购买 nike

我在 XIB 中有一个 UIPickerView 并且我有修改此 View 中其他元素的事件:

- (void) pickerView: (UIPickerView *) pickerView didSelectRow: (NSInteger) row inComponent: (NSInteger) component {
[self.definitionBeep play];
self.glossaryTerm.text = [[self.dataSource objectAtIndex:row]objectAtIndex:0];
self.glossaryDefinition.text = [[self.dataSource objectAtIndex:row]objectAtIndex:1];
}

然后我有另一个 View,它不是用 IB 构建的,而是以编程方式构建的,可以从我的 ViewController.m 访问,如下所示:

- (IBAction)someTouchElement:(id)sender{

if(myWebScroller == nil) {
myWebScroller = [[MyWebScroller alloc]
initWithFrame: CGRectMake(15, mainControl.frame.origin.y, mainControl.frame.size.width, mainControl.frame.size.height)
title:@" MY TITLE"
category:myCat];
[self.view addSubview: myWebScroller];
}
}

MyWebScroller 中,我正在监听点击并能够成功拦截它们。我想要做的是更改 UIPickerView 的值并让它更改 glossaryTerms 的值,如上所示。

如果您需要更多说明,请发表评论,我会相应地更新问题。谢谢

最佳答案

扩展伦纳德要求你做的事情..

要监听通知,你需要为它注册一个观察者。所以您可以在 ViewDidLoad: 或 Viewcontroller.m 的 ViewWillAppear 中执行此操作

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NotificationPosted:) name:@"ClickedInWebView" object:nil];

然后在 Viewcontroller.m 中。实现方法“通知已发布

-(void)NotificationPosted:(NSNotification *)notification {

NSLog(@"Something happened");
//change the picker here.
}

在此之后,在 webView.m 中,您可以随时在选择器 View 中更改值时发布通知。

[[NSNotificationCenter defaultCenter] postNotificationName:@"ClickedInWebView" object:nil userInfo:nil]

;

或者,您也可以通过 NSDictionary 传递 NSString 或 NSNumber。

NSDictionary *userInfo = [NSDictionary dictionaryWithObjectsAndKeys:value, @"keyName", nil]; [[NSNotificationCenter defaultCenter] postNotificationName:@"ClickedInWebView" object:nil userInfo:userInfo];

然后你就可以在viewcontroller中那个实现的方法中使用这个字典了。使用 notification.userinfo

关于objective-c - 如何从另一个类访问 xib 中的 UIPicker?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12094094/

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