-2

What I have is menu at the top (vertical slider). On the top I have button, on clicking this button, the view will get scroll down and display the menu.

For this what I am doing is creating a view and showing it at top.

enter image description here

Now when I click on the button, I am planning to change the frame of top view with animation.

I am able to handle the frame and animation, however problem I am facing is setting the text to the right label and left button.

Below is the dropbox link to download the sample project. Any idea/ solution will be appreciated.

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


What I tried is below code

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

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

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

However still on the right side I see products only.


Full Code

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

What I want to do is keep common things in CommonViewController

Charles
  • 50,943
  • 13
  • 104
  • 142
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276

3 Answers3

1

Below is what I did...

In bViewController.m, I added below.

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

And in 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;
    }
}
Fahim Parkar
  • 30,974
  • 45
  • 160
  • 276
0

You need to make a string property for CommonViewController and assign the text to it. And in CommonViewController's ViewDidLoad set that text to rightSideLabel.

manya
  • 61
  • 2
0

You have to respect MVC design Pattern approach. So instead of "newClass.rightSideLabel.text = @"NEW VALUE HERE";" You have to write :

in NewClass.h: -@property(strong,nonatomic) stringText;

in NewClass.m:(ViewDidLoad Method) -rightSideLabel.text=stringText;

in the current class: -newClass.stringText= @"NEW Value HERE";

Med.Amine.Touil
  • 1,225
  • 2
  • 11
  • 15
  • newclass is bviewcontroller in my case? I don't get you... – Fahim Parkar Jan 30 '14 at 10:41
  • new class is the instance from the class(ViewController) where your Label exists – Med.Amine.Touil Jan 30 '14 at 10:46
  • I did what you are saying... but it is not working... bcz CustomViewController didload is getting called before bViewController... – Fahim Parkar Jan 30 '14 at 11:11
  • aaaaaa OK !! Use the NSNotificationCenter – Med.Amine.Touil Jan 30 '14 at 11:14
  • in CustomViewController.m: (ViewDidAppear) -[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(upDateLabelText:)name:@"upDateLabel" object:nil]; in CustomViewController.m: -(void)upDateLabelText :(NSNotification*) pNotification { Label.text=stringText; } in bViewController After setting the stringValue -[[NSNotificationCenter defaultCenter]postNotificationName:@"upDateLabel" object:nil]; – Med.Amine.Touil Jan 30 '14 at 11:19
  • I did... thanks for your answer... See my answer for what I did... – Fahim Parkar Jan 30 '14 at 11:23