1

I have a UIButton in MainViewController.
MainViewController has a childViewContoller.

I need to access the UIButton (tcButton) property in MainViewController FROM the childViewController and set it to setSelected:YES in viewDidLoad. I have the following code in my ChildViewController.m file and it's not working.

#import "ChildViewController.h"
#import "MainViewController.h"
#import "CoreData.h"

@interface ChildViewContoller () 
@property (nonatomic, strong) CoreData *coreData;
@property (nonatomic, strong) MainViewController *mainViewController;
@end

@implementation ChildViewController
@synthesize coreData, mainViewController;

-(void)viewDidLoad 
{
    [super viewDidLoad];
    self.managedObjectContext = [(STAppDelegate *)[[UIApplication sharedApplication]  delegate] managedObjectContext];
    [[(mainViewController *)self.parentViewController tcButton] setSelected:YES];
}
user1107173
  • 10,334
  • 16
  • 72
  • 117

2 Answers2

2

Your code is kind of a mess. Why are you creating a new instance of yourself in viewDidLoad? This makes no sense. If ChildViewController is truly a child view controller, then you can access the parent with self.parentViewController. You only need one line in the viewDidLoad:

-(void)viewDidLoad // Line 4
{

    [[(MainViewController *)self.parentViewController tcButton] setSelected:YES]; // Line 8
}
rdelmar
  • 103,982
  • 12
  • 207
  • 218
  • rdelmar: I thought the same thing and first tried: [self.parentViewController tcButton] setSelected = YES]; and that gave me the error: No Visible @interface 'UIViewController' declare the selector 'tcButton' Then I started googling and saw the code above. However, now I'm trying your code and I get the following error: Parse Issue Expected expression. The cursor arrow is poing at the close parenthesis of (MainViewController *). Yes, I do have a semicolon at the end, I copied and pasted your code. Thanks. – user1107173 Feb 10 '13 at 22:33
  • @user1107173, Oh, sorry, I typed it wrong, it should be setSelected:YES (not =) at the end. – rdelmar Feb 10 '13 at 22:42
  • Thanks for your quick reply. I changed it to setSelected:YES]; and I'm still getting the same Parse Issue Expected expression. The cursor arrow is pointing at the close parenthesis of (MainViewController *). – user1107173 Feb 10 '13 at 22:58
  • @user1107173, that absolutely should work -- edit your question to show what you have in that class now. Have you imported MainViewController.h? – rdelmar Feb 10 '13 at 23:02
  • @user1107173, it should be MainViewController (capital M) -- you're referring to the class, not your property. You could assign your property to (MainViewController *)self.parentViewController if you like. – rdelmar Feb 10 '13 at 23:22
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/24292/discussion-between-rdelmar-and-user1107173) – rdelmar Feb 10 '13 at 23:23
1

There are several issues in your code but the main idea to perform what you want is getting a pointer to the mainViewController. There are many ways to do that but here a simple example how you can implement such thing. For instance in the initializer of the ChildViewContoller you can pass a pointer to the mainViewController:

@interface ChildViewContoller ()

@property (nonatomic, strong) MainViewController *mainViewController;

@end

@implementation ChildViewContoller

- (id)initWithMainViewController:(MainViewController *)mainViewController
{
    self = [super init];

    if (self)
    {
        _mainViewController = mainViewController;
    }

    return self;
}

- (void)viewDidLoad
{
    [_mainViewController.tcButton setSelected:YES];
}

@end

Please not that I have not tested the code above but you can get the idea.

kaal101
  • 654
  • 7
  • 14
  • I'm getting 4 errors with the code: (1)self = [super init]; //Cannot assign to 'self' outside of a method in the init family (2)_mainViewController = mainViewController; //Unexpected interface name 'MainViewController' expected expression (3)return self; //Void method initWithMainViewController should not return a value (4)//Implicit conversion of an objective-C pointer to 'void' is disallowed in ARC. – user1107173 Feb 10 '13 at 21:30
  • Sorry for that but as I said I can't test the code, don't use it as is. Instead try to understand what is doing the code. I've modified a little bit code to avoid some of the erros, please not that this is an ARC version. – kaal101 Feb 10 '13 at 21:56