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

  • Home
  • SEARCH
  • 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 8284081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T10:58:03+00:00 2026-06-08T10:58:03+00:00

I can’t figure out how to run code from different View Controller ‘s .

  • 0

I can’t figure out how to run code from different View Controller‘s .

I have created a new project, and would appreciate a quick response as its homework, and have to be done by tonight.

In my project I have two View Controller‘s named; viewcontroller1 and viewcontroller2, and have assigned them to two different custom classes.

Viewcontroller1 comes to screen it has two UIButtons, one for running the code, and the other one to push to viewcontroller2.

When user clicks the first UIButton I want that button to run the code below and display the results on a UILabel on my viewcontroller2.

The code creates random numbers using arc4random and then show’s random words in the UILabel. The code works for single page based apps, and is used under implementation.

So basically what i want is, when i click the first UIButton i want to display randomly created words on my UILabel in the viewcontroller2.

-(IBAction)button; {
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1) { 
    label.text = @"hello1";    
    } 
    else if (random_num == 2) {
    label.text = @"hello2";
    }
    else if (random_num == 3) {
    label.text = @"hello3";
    }
    else if (random_num == 4) {
    label.text = @"hello4";
    }
}

The timelabel is the UILabel on my viewcontroller2.

Can you please be detailed in your answer. I am a beginner and might not grasp the concepts right away.

Example

User opens the app

Viewcontroller1 is shown

User press the button 1 which runs code for the label in the viewcontroller2

User press the button 2 to go to viewcontroller

Viewcontroller2 comes up and label on it has changed by the button

I know that it is not a hard thing to do but I can’t do it.

For those who answered me before and for those who will answer thank you soo much..

Thanks.

  • 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-08T10:58:06+00:00Added an answer on June 8, 2026 at 10:58 am

    So, first of all I want to make it clear, it is not possible that viewcontroller1 comes to screen. Actually view of viewcontroller1 comes to screen. You see on the screen only the views and they belongs / controlled by viewcontrollers.

    To answer you question correctly we need to know, what do you use to change the views? Do you use a navigation controller, tabbarcontroller or something else?

    Commonly the labels, which is a UI element for presenting textes are initialized in viewDidLoad method of the viewcontroller. So setting text before that does nothing.

    if you have reference to viewcontroller2 from viewcontroller1 you can create a property to hold the text and set it after pressing the button. And viewcontroller2 can present the text after loading the view on a label. So your code in viewcontroller1 looks like this

    -(IBAction)button; {
     ViewController2 *viewController2 = [[ViewController2 alloc] init];
     viewcontroller2.textToShow = ......;
     //show view of your viewcontroller2
     // e.g.
     [self.navigationController pushViewController:viewcontroller2 animated:YES];
     }
    

    And your viewcontroller2 header file(ViewController2.h)

       @property (nonatomic, strong) NSString *testToShow;
    

    you viewController2 implementation file (Viewcontroller2.m)

    @synthesize textToShow;
    -(void)viewDidLoad
     {
         UILabel *label = UILabel aloc initWithFrame:CGRectMake(0, 0, 200, 200);
         [self.view addSubview:label];
         label.text = self.textToShow;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Can a DateTime value be NULL ? I have this code: From here I
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can't seem to figure out what's wrong with the simple getJSON call below. It's
Can I order my users in the database, so I don't have to say
Can someone thoroughly explain the last line of the following code: def myMethod(self): #
I have a jquery bug and I've been looking for hours now, I can't
Can I run this in a Windows command prompt like I can run it
can someone explain why the compiler accepts only this code template<typename L, size_t offset,
Can someone please explain why this doesn't work? MyClass myClass1 = new MyClass(); object

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.