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 8993851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:17:16+00:00 2026-06-15T23:17:16+00:00

I have a viewController that has a button which, when clicked, dynamically creates a

  • 0

I have a viewController that has a button which, when clicked, dynamically creates a reference to another viewController class, and in the process sets the parameters for that viewController as well. I do this inside a for loop as follows:

-(void) clickOnButton:(id)sender {


    for (PersonObject *checkPerson in [DataModel sharedInstance].personList) {

        if (((UIControl*)sender).tag == checkPerson.personID) {

            ParentViewController *parentView = [[NSClassFromString(checkPerson.childViewController) alloc] init];
            parentView.personName = checkPerson.name;
            NSLog(parentView.personName);
            [self.navigationController pushViewController:parentView animated:YES];


        }

    }

}

In the viewController that gets created and to which the user is sent to, I have the following code in my viewDidLoad method:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"Hello %@", personName);
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, 150, 35)];
    [title setCenter:CGPointMake(self.view.frame.size.width/2, 27)];
    [title setBackgroundColor:[UIColor clearColor]];
    [title setTextAlignment:NSTextAlignmentCenter];
    [title setFont:[UIFont boldSystemFontOfSize:15]];
    [title setText:personName];
    [self.view addSubview:title];

}

When I run my code, the first viewController gives me the correct output to the NSLog of the personName parameter, however, the NSLog() inside the method viewDidLoad in the second viewController statement shows that my value for personName is nil, and nothing shows up as my title for the viewController.

The parameter personName is of type NSString and is found in both the parent viewController class, as well as the child viewController classes (there are several viewController classes that are extending this one parent). How do I dynamically create the child viewController object as well as have it capture the correct parameter value that is being sent to it using the parent viewController?

  • 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-06-15T23:17:18+00:00Added an answer on June 15, 2026 at 11:17 pm

    A common mistake is to have an interface in the view controller to which you’re transitioning to have an interface like:

    @interface MyViewController : UIViewController
    {
        NSString *personName;
    }
    @property (nonatomic, retain) NSString *personName
    @end
    

    and then an implementation that either does not explicitly synthesize personName or one that has a @synthesize statement like:

    @implementation MyViewController
    
    @synthesize personName = _personName;
    
    // the rest of the implementation
    
    @end
    

    Note, if you don’t have an explicitly declared @synthesize statement, it will automatically synthesize personName like the above statement.

    This rather innocuous bit of code is problematic, because you’ll end up with two instance variables, one called personName and another, which is synthesized for you, which is called _personName.

    What you really should do is eliminate the explicitly declared ivar, thus the @interface would look like:

    @interface MyViewController : UIViewController
    @property (nonatomic, retain) NSString *personName
    @end
    

    And then either omit the @synthesize statement altogether or have one that looks like:

    @synthesize personName = _personName;
    

    And then, your code can either refer to the property via its accessors, e.g.

    NSLog(@"Hello %@", self.personName);
    

    Or you can use the instance variable (the ivar):

    NSLog(@"Hello %@", _personName);
    

    (As an aside, it’s generally advisable to use the accessors (e.g. self.personName) when setting properties. The only time you really must use the ivar (e.g. _personName) is in the initializer methods and dealloc.)

    But, bottom line, don’t explicitly define an instance variable, let the compiler synthesize it for you, eliminating this sort of problem. And it’s advisable to synthesize the ivar with the leading underscore, to minimize accidental use of the ivar when you really intended to use the property’s accessor methods.

    See Practical Memory Management in the Advanced Memory Management Programming Guide for more information.

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

Sidebar

Related Questions

I have a button that open another viewController (familyView) when clicked. In familyView there
I have a ViewController for my app which has a single button which, when
I have a ViewController which has an embedded UIImageView . Another ViewController has a
I have a ViewController that has its own NIB. I would like to embed
I want to have a ViewController that has a table view on the top
I have a view controller class that has to implement several protocols. Too keep
I have the problem that my view controller class has too many delegates and
I have created two viewController classes such that one is superclass of another.i have
I have a button and when its clicked/tapped I want to check that a
I have a UISearchBar which is subviewed by another view when a button is

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.