简体   繁体   English

如何从iPhone的子视图更新Superview UILabel文本?

[英]How to update superview UILabel text from subview in iPhone?

I have two class, Client_Main and Client_demo . 我有两个类, Client_MainClient_demo
In Client_Main class I am using a label which shows client name and after click on a button from Client_Main class I have added Client_demo class as a subview . Client_Main类中,我使用的是显示客户端名称的标签,单击Client_Main类中的按钮后,我已将Client_demo类添加为子视图 Now when I click on a button on Client_demo class I want to change the text of Client_Main class label. 现在,当我单击Client_demo类上的按钮时,我想更改Client_Main类标签的文本。

So please suggest me how to do this. 所以请建议我该怎么做。

Put an unique tag for the UILabel in superview UILabel的唯一标签置于超级视图中

[superview subviews]; returns all the view objects in the superview and from that get the one with the unique tag you set and that is it 返回超级视图中的所有视图对象,并从中获得带有您设置的唯一标记的视图对象

 for (UILabel *label in [yourSubview.superview]) {
        if (label.tag==uniqueID) {
            //Here is your uilabel instance ,do what you want
        }
    }

OR 要么

The proper way : Delegation 正确方法:委派

just make a delegate method with implementation on the superview to change on label using its instance. 只需在Superview上实现实现的委托方法即可使用其实例更改标签。 Fire the delegate method from the subview class 从子视图类中激发委托方法

more on delegation 有关授权的更多信息

there is two way to update superview from subview 1-Via NSNotification in this approch you have to create notification in superview calass and set observer like this 有两种方法可以从子视图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";
 }

from subview you have to post this notification from that action(inside method ) you want to update like button click or cell selection of table view etc like this way 从子视图中,您必须从要更新的操作(内部方法)中发布此通知,例如单击按钮或表视图的单元格选择等

   [[NSNotificationCenter defaultCenter]
 postNotificationName:@"NotificationForUpdateHomeScreenData" 

2- way is Proper delegation 2种方式是正确的委派

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

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