[英]How to update superview UILabel text from subview in iPhone?
我有两个类, Client_Main和Client_demo 。
在Client_Main类中,我使用的是显示客户端名称的标签,单击Client_Main类中的按钮后,我已将Client_demo类添加为子视图 。 现在,当我单击Client_demo类上的按钮时,我想更改Client_Main类标签的文本。
所以请建议我该怎么做。
将UILabel
的唯一标签置于超级视图中
[superview subviews];
返回超级视图中的所有视图对象,并从中获得带有您设置的唯一标记的视图对象
for (UILabel *label in [yourSubview.superview]) {
if (label.tag==uniqueID) {
//Here is your uilabel instance ,do what you want
}
}
要么
正确方法:委派
只需在Superview上实现实现的委托方法即可使用其实例更改标签。 从子视图类中激发委托方法
有两种方法可以从子视图1-Via NSNotification更新超级视图,在此方法中,您必须在超级视图calass中创建通知并像这样设置观察者
//you can write this code in viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationEventForUpdateData:) name:@"NotificationForUpdateHomeScreenData" object:nil];
and define this method notificationEventForUpdateData in same superview class in this method you can update label textfield etc whatever you want
-(void) notificationEventForUpdateData: (NSNotification *)notification
{
self.label.text=@"newvalue";
}
从子视图中,您必须从要更新的操作(内部方法)中发布此通知,例如单击按钮或表视图的单元格选择等
[[NSNotificationCenter defaultCenter]
postNotificationName:@"NotificationForUpdateHomeScreenData"
2种方式是正确的委派
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.