从不同视图通过按钮更新标签 [英] Update a label through button from different view

查看:73
本文介绍了从不同视图通过按钮更新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果标签在uiview控制器的不同类别中按钮点击更新标签...当点击按钮时,标签应该更新...我试了很多次..但它没有发生..

How to update a label by button clicks when they both are in different classes of uiview controller...when button is clicked,the label should be update...i tried many times..but its not happening..

还有一个问题是我的应用程序在模拟器中运行良好但是当我在设备上运行时,动态创建的按钮(按钮图像)不可见,操作正在执行但图像丢失..我知道为什么?

one more Question is my app is running good in simulator but when i run on device the dynamically created button(button image) is not visible,action is performing but image is missing..may i know why?

推荐答案

在iOS中,有几种方法可以保持视图之间的通信(实际上是视图控制器) 。对我来说最容易发送通知。您在要进行更改的视图中添加通知的观察者,并从将触发更改的视图中发布通知。通过这种方式,您可以从ViewController B告诉ViewController A 已准备就绪,进行更改

There are a few ways to maintain communication between views (view controllers, actually) in iOS. Easiest of which for me is sending notifications. You add an observer for a notification in the view you want to make the change, and from the view that will trigger the change, you post the notification. This way you tell from ViewController B to ViewController A that "something is ready, make the change"

这当然需要您的接收器要创建的视图,并且已经在监听通知。

This, of course, requires your receiver view to be created and already be listening for the notification.

在ViewController B中(发件人)

In ViewController B (sender)

- (void)yourButtonAction:(id)sender
{
    [[NSNotificationCenter defaultCenter] postNotificationName:@"theChange" object:nil];
}

在ViewController中A(接收方)
添加观察者以监听通知:

In ViewController A (receiver) Add the observer to listen for the notification:

- (void)viewDidLoad
{
    //.........
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(makeTheChange) name:@"theChange" object:nil];
}

别忘了删除它(在这种情况下,在 dealloc

Do NOT forget to remove it (in this case, on dealloc)

- (void)dealloc
{
     [[NSNotificationCenter defaultCenter] removeObserver:self name:@"theChange" object:nil];
     [super dealloc];
}

最后,更新标签的方法

- (void)makeTheChange
{
    yourLabel.text = @"your new text";
}

这篇关于从不同视图通过按钮更新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆