Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3490514
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T11:31:32+00:00 2026-05-18T11:31:32+00:00

In my mainViewController.h I have declared a UIButton *myButton and I synthesize it in

  • 0

In my mainViewController.h I have declared a UIButton *myButton and I synthesize it in mainViewController.m. I also created an accessor method (myButton) that just returns the button. In another view (and therefore a different view controller class) I have:

mainViewController *mainVC = [[mainViewController alloc] init];
UIButton *myButton = [mainVC myButton];
[eventButton setTitle:@"New Title" forState:UIControlStateNormal];

I want this code to change the title label of the button, but it doesn’t seem to work. Using a NSLog statement (NSLog(@"%@", myButton.titleLabel);) to see what the current title is, it always returns null. From what I’ve read, this is because I’m creating an instance of the mainViewController and therefore I’m not actually accessing/changing the button. How can I access the actual view controller and not an instance?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T11:31:33+00:00Added an answer on May 18, 2026 at 11:31 am

    (Before the longer discussion: it looks like you’re typing myButton one line 2, and eventButton on line 3. Make sure these match!)

    You’re correct in thinking that you’re creating a new instance of mainViewController. To get at the one you want, You’ll either need to pass a reference to mainViewController into the new view controller when you create it, writing a method like:

    @synthesize mainViewController;
    
    -(void) initWithMainVC:(MainViewController *)mainVC {
        if ((self = [super init])) {
            self.mainViewController = mainVC;
        }
        return self;
    }
    

    to override init, or, if you’ve got a reference to mainViewController in your App Delegate, you can get it using:

    MainViewController *mainVC = [(YourAppDelegate *)[[UIApplication sharedApplication] delegate] mainViewController];
    

    Either way you choose, you’ll want to read up on object oriented programming and pointers in objective C. When you call alloc, you’re manufacturing a new copy of that class. All copies sit in different chunks of memory, so any new one you make (as in your code above) is actually a totally different entity, like two Priuses, or something like that.

    A pointer is like a nametag that identifies, or points the way, to that block of memory. In my init code example above, we’re attaching a new nametag to a mainViewController instance with the same guts. Same thing with the second line — we make a new pointer (see the star?) tell it it’s going to be pointing to a MainViewController instance, and slap that nametag, or pointer, on to an instance.

    (The second one is a little more complicated, as you’re plucking mainViewController from an object called a singleton, or an object that you can get at from anywhere… read more about those and the App Delegate at this post from Cocoa with Love.)

    Anyway, I get nervous expounding on this stuff on Stack! Check out Apple’s Objective C reference for some more of the basics. The sample code on the iOS Dev Center is really, really good to check out. Download a project that looks nice, or, better yet, just create a project from an Xcode template, and go try to figure out how it’s stitched together. Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one mainViewController that is also a delegate for a pickerview contained in
I have created a button: button = [StretchableButton new]; [button addTarget:mainViewController action:@selector(addNumberFromButton:) forControlEvents:UIControlEventTouchUpInside]; Inside
Have an AppDelegate and a MainViewController . In MainViewController the UIButton was created, AppDelegate
I have the following piece of code in my MainViewController. Is that the best
I have an UIViewController named MainViewController I have another UIViewController named LeftSharingViewController; I would
I have added a subview in my MainViewController. How can i call a method
MainViewController have UIToolbar and when button is pressed on the UIToolbar , it displays
I have a view controller, MainViewController in which I have a UIButton as an
I have create an IOS app. The app has MainViewController that will present/dismiss Modal
I have a viewController called mainViewController that contains a UIWebView called myWebView. mainViewController presents

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.