Ios 如何设置另一个类的文本?
我拥有的是顶部的菜单(垂直滑块)。在顶部的“我有”按钮上,单击此按钮,视图将向下滚动并显示菜单 为此,我正在创建一个视图并在顶部显示它 现在,当我单击按钮时,我计划用动画更改俯视图的帧 我能够处理帧和动画,但是我面临的问题是将文本设置为右标签和左按钮 下面是下载示例项目的dropbox链接。任何想法/解决方案都将不胜感激Ios 如何设置另一个类的文本?,ios,iphone,objective-c,oop,ios7,Ios,Iphone,Objective C,Oop,Ios7,我拥有的是顶部的菜单(垂直滑块)。在顶部的“我有”按钮上,单击此按钮,视图将向下滚动并显示菜单 为此,我正在创建一个视图并在顶部显示它 现在,当我单击按钮时,我计划用动画更改俯视图的帧 我能够处理帧和动画,但是我面临的问题是将文本设置为右标签和左按钮 下面是下载示例项目的dropbox链接。任何想法/解决方案都将不胜感激 我尝试的是下面的代码 CommonViewController *newClass = [[CommonViewController alloc] init]; newC
我尝试的是下面的代码
CommonViewController *newClass = [[CommonViewController alloc] init];
newClass.parentObject = self;
NSLog(@"text is %@", newClass.rightSideLabel.text);
newClass.rightSideLabel.text = @"NEW VALUE HERE";
然而,仍然在右侧,我只看到产品
完整代码 CommonViewController.h bViewController.h
我想做的是在CommonViewController中保留常用内容您需要为CommonViewController创建一个字符串属性并将文本分配给它。
在CommonViewController的ViewDidLoad中,将文本设置为rightSideLabel。您必须尊重MVC设计模式方法。因此,与“newClass.rightSideLabel.text=@“newvalue HERE”不同,“您必须编写: 在NewClass.h中: -@属性(强,非原子)stringText 在NewClass.m中:(ViewDidLoad方法) -rightSideLabel.text=stringText 在当前类中: -newClass.stringText=@“此处新值” 下面是我所做的 在
bViewController.m
中,我添加了以下内容
[[NSUserDefaults standardUserDefaults] setValue:@"New Value Here" forKey:@"notValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeRightSideLabel" object:self];
在CommonViewController.m
,viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"ChangeRightSideLabel" object:nil];
- (void) receiveTestNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if ([[notification name] isEqualToString:@"ChangeRightSideLabel"]) {
NSLog (@"Successfully received the test notification!");
trightSideLabel = [[NSUserDefaults standardUserDefaults] valueForKey:@"notValue"];
rightSideLabel.text = trightSideLabel;
}
}
@西蒙克洛林:如果你读对了,它不会重复。。。对我来说,它设置数据而不传递数据…@downvoter:这个问题有什么不对???在我的情况下,newclass是bviewcontroller?我不明白你的意思…新类是你的标签所在的类(ViewController)的实例我做了你所说的。。。但它不起作用。。。bcz CustomViewController didload在bViewController之前被调用…AAAAA OK!!在CustomViewController.m:(ViewDidDisplay)-[NSNotificationCenter defaultCenter]添加观察者:自选择器:@selector(upDateLabelText:)名称:@“upDateLabel”对象:nil];设置stringValue-[[NSNotificationCenter defaultCenter]postNotificationName:@“upDateLabel”对象:nil]后,在bViewController中的CustomViewController.m:-(void)upDateLabelText:(NSNotification*)pNotification{Label.text=stringText;}中;
#import <UIKit/UIKit.h>
#import "CommonViewController.h"
@interface bViewController : CommonViewController
@end
#import "bViewController.h"
@interface bViewController ()
@end
@implementation bViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor lightGrayColor];
CommonViewController *newClass = [[CommonViewController alloc] init];
newClass.parentObject = self;
NSLog(@"text is %@", newClass.rightSideLabel.text);
newClass.rightSideLabel.text = @"NEW VALUE HERE";
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
[[NSUserDefaults standardUserDefaults] setValue:@"New Value Here" forKey:@"notValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeRightSideLabel" object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveTestNotification:) name:@"ChangeRightSideLabel" object:nil];
- (void) receiveTestNotification:(NSNotification *) notification
{
// [notification name] should always be @"TestNotification"
// unless you use this method for observation of other notifications
// as well.
if ([[notification name] isEqualToString:@"ChangeRightSideLabel"]) {
NSLog (@"Successfully received the test notification!");
trightSideLabel = [[NSUserDefaults standardUserDefaults] valueForKey:@"notValue"];
rightSideLabel.text = trightSideLabel;
}
}