1
votes

I am trying to call my view in the second view controller with a new set of values to be changed in the first view controller. i am using pointers to get my first viewcontroller's view (used a property that points to firstview controller) but i am not seeing any changes in the view. ideas are appreciated. Thank you

//secondviewcontroller.m

#import "SecondViewController.h"
#import "FirstViewController.h"
#import "Tab.h"
#import "AppDelegate.h"

@interface SecondViewController ()

@end

@implementation SecondViewController
@synthesize changeSizeSlider;




- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"second", @"second");
        self.tabBarItem.image = [UIImage imageNamed:@"second"];

    }
    return self;
}


- (IBAction)changeSizeSlider:(UISlider *)sender
{
    // Change label to match slider's value
    label1.text = [NSString stringWithFormat:@"%g", [changeSize value]];

    // Var for changing size
    CGFloat changeSizeCont = [changeSize value];


    NSLog(@"changeSizeCont = %f", changeSizeCont);


    ((Tab *)vc.view).rect_width  = changeSizeCont;
    ((Tab *)vc.view).rect_height = changeSizeCont;

    // The values here are displayed but i think the view is not reloaded with the nw values
    NSLog(@"Current values for VC's view properties:");
    NSLog(@"rect_width = %f", ((Tab *)vc.view).rect_width);
    NSLog(@"rect_height = %f", ((Tab *)vc.view).rect_height);

}

Calling the firstviewcontroller's view object vc by calling its property in //secondviewcontroller.h

@property FirstViewController *vc;

//firstviewcontroller.m

#import "FirstViewController.h"

@interface FirstViewController ()

@end

@implementation FirstViewController


- (void)viewWillAppear:(BOOL)animated
{
    //NSLog(@">>> %@", NSStringFromSelector(_cmd) );
    self.title = NSLocalizedString(@"First", @"First");
    self.tabBarItem.image = [UIImage imageNamed:@"first"];
    // Get timer
    NSTimer *timer = [NSTimer timerWithTimeInterval: 0.5
                                                      target: self.view
                                                    selector: @selector(setNeedsDisplay)
                                                    userInfo: nil
                                                     repeats: YES];

    // Get runloop, add timer to runloop
    NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
    [runLoop addTimer:timer forMode: NSDefaultRunLoopMode];


}

I have the functions that draws the rectangles written in a class.

//appdelegate.m #import "TabAppDelegate.h"

#import "FirstViewController.h"

#import "SecondViewController.h"

#import "Tab.h"

@implementation TabAppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    time_t seed = time(0);
    srandom((int)seed);

    CGRect bounds        = [self.window bounds];
    Tab *view     = [[Tab alloc] initWithFrame:bounds];
    view.backgroundColor = [UIColor whiteColor];

    FirstViewController *vc = [[FirstViewController alloc] init];
    [vc setView: view];

    SecondViewController *vc2 = [[SecondViewController alloc] init];
    //[vc2 setView: view];
    vc2.vc = vc;


    self.tabBarController = [[UITabBarController alloc] init];
    self.tabBarController.viewControllers = @[vc, vc2];
    self.window.rootViewController = self.tabBarController;

    [self.window makeKeyAndVisible];
    return YES;
}

Tab.m has the draw functions that will appear in the firstview and secondview has a nib that handles the changes in firstviewcontroller.

I pretty much think i am not wrong anywhere, i spent almost one week without figuring out this issue. Appreciate your time guys.

2
Can you add some relevant code? This is a little confusing. - lostInTransit
@lostInTransit: update - user2038249

2 Answers

3
votes

Handling changes, actions, or user interaction in other views is the essential use case of delegates. The best practice is to have the first view controller be the delegate of the second view controller, and then as events happen in the second view controller, it calls certain methods on its delegate to notify it.

Other options for keeping values/state in sync between views are:

0
votes

You can also create a block property on your second view controller which the first view controller sets when it pushes it on the stack

typdef void(^CallWhenChanged)(CGSize);
@interface ViewController2

@property(strong, nonatomic) CallWhenChanged callBlock;

@end

Then:

- (IBAction)changeSizeSlider:(UISlider *)sender
{
    //do stuff
    if( self.callBlock != nil )
    {
        self.callBlock(CGSizeMake(rect_width,rect_height));
    }
}

Delegates as mentioned by jszumski are another option, but I prefer to use delegates when dealing with more than one delegate method.