如何设置另一个类的文本?

时间:2014-01-30 10:17:30

标签: ios iphone objective-c oop ios7

我所拥有的是顶部的菜单(垂直滑块)。在顶部我有按钮,单击此按钮,视图将向下滚动并显示菜单。

为此我正在做的是创建一个视图并在顶部显示它。

enter image description here

现在当我点击按钮时,我打算用动画改变顶视图的框架。

我能够处理框架和动画,但是我面临的问题是将文本设置为右侧标签和左侧按钮。

下面是用于下载示例项目的Dropbox链接。任何想法/解决方案将不胜感激。

https://www.dropbox.com/s/pdaelw2k8ljnaki/TwoView.zip


我尝试的是代码

CommonViewController *newClass = [[CommonViewController alloc] init];
newClass.parentObject = self;

NSLog(@"text is %@", newClass.rightSideLabel.text);

newClass.rightSideLabel.text = @"NEW VALUE HERE";

然而,在右侧,我只看到产品。


完整代码

CommonViewController.h

#import <UIKit/UIKit.h>

@interface CommonViewController : UIViewController

@property (retain, nonatomic) IBOutlet UIView *topView;
@property (retain, nonatomic) IBOutlet UILabel *rightSideLabel;
@property (retain, nonatomic) IBOutlet UIButton *leftSideButton;
@property (retain, nonatomic) IBOutlet UIButton *middleButton;
@property (retain, nonatomic) IBOutlet UIImageView *topImageView;
@property (nonatomic, assign) CommonViewController  *parentObject;
@end

CommonViewController.m

#import "CommonViewController.h"

@interface CommonViewController ()

@end

@implementation CommonViewController

@synthesize topView, rightSideLabel, leftSideButton, middleButton, topImageView, parentObject;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    topView = [[UIView alloc] initWithFrame:CGRectMake(0, -416, 320, 460)];

    topImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
    topImageView.image = [UIImage imageNamed:@"top_bar_bg4.png"];

    middleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [middleButton setFrame:CGRectMake(120, topView.frame.size.height-44, 80, 44)];
    [middleButton setBackgroundImage:[UIImage imageNamed:@"slide_arrow_open.png"] forState:UIControlStateNormal];

    leftSideButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftSideButton setFrame:CGRectMake(0, topView.frame.size.height-44, 80, 44)];
    [leftSideButton setTitle:@"BACK" forState:UIControlStateNormal];
    [leftSideButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

    rightSideLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, topView.frame.size.height-40, 110, 40)];
    rightSideLabel.text = @"PRODUCTS";
    rightSideLabel.textColor = [UIColor whiteColor];
    rightSideLabel.backgroundColor = [UIColor clearColor];



    [topView addSubview:topImageView];
    [topView addSubview:middleButton];
    [topView addSubview:leftSideButton];
    [topView addSubview:rightSideLabel];
    [self.view addSubview:topView];

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

bViewController.h

#import <UIKit/UIKit.h>
#import "CommonViewController.h"

@interface bViewController : CommonViewController

@end

bViewController.m

#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

我想要做的是在CommonViewController

中保留常见的东西

3 个答案:

答案 0 :(得分:1)

以下是我的所作所为......

bViewController.m中,我在下面添加了。

[[NSUserDefaults standardUserDefaults] setValue:@"New Value Here" forKey:@"notValue"];
[[NSUserDefaults standardUserDefaults] synchronize];
[[NSNotificationCenter defaultCenter] postNotificationName:@"ChangeRightSideLabel" object:self];

CommonViewController.mviewDidLoad

[[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;
    }
}

答案 1 :(得分:0)

您需要为CommonViewController创建一个字符串属性并为其分配文本。 并且在CommonViewController的ViewDidLoad中将该文本设置为rightSideLabel。

答案 2 :(得分:0)

您必须尊重MVC设计模式方法。因此,而不是“newClass.rightSideLabel.text = @”这里的新价值“;”你必须写:

在NewClass.h中

: - @ property(strong,nonatomic)stringText;

NewClass.m中的

:( ViewDidLoad方法) -rightSideLabel.text = stringText;

在当前班级中: -newClass.stringText = @“NEW Value HERE”;