0

I know similiar questions have been asked before, but please bear with me as I am totally new at Objective C (have good knowledge on C).

My case is that I have a tab bar controller with two windows. When I press a button in "second" I change a variable changeName. I want to use the value of changeName in my "first" view controller but stumbled on to some problems:

To reach the other viewcontroller I found this from SO (unfortunately I forgot where):

-(NSString *)newName{

// Create a UIStoryboard object of "MainStoryBoard_iPhone" named storyboard
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:(NSString *)@"MainStoryBoard_iPhone" bundle:(NSBundle *)nil];

// Create a UIViewController object of the wanted element from MainStoryBoard_iPhone
UIViewController *secondview = [storyboard instantiateViewControllerWithIdentifier:(NSString *)@"secondBoard"];

return secondview.changeName;
}

Which I have in my first.m. MainStoryBoard_iPhone is the name of the .xib/storyboard-file.

but no. Error says

Property 'changeName' not found on object of type 'UIViewController *'

In my second.h I have

 @property (readwrite, retain) NSString *changeName;

and in second.m

 @synthesize changeName;

All help is appreciated, but please keep in mind that I have only used OOP for two days. Thank you all in advance

EDIT: And what input should I have here?

 - (void)viewDidLoad
{
[super viewDidLoad];
mylabel.text = Name; // or [mylabel.text Name] or something? 
}

2 Answers 2

5

You need to cast the view controller returned to your customized second view controller (as UIViewController does not have the property changeName).

So do the following :

// Create a UIViewController object of the wanted element from MainStoryBoard_iPhone
SecondViewController *secondview = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:(NSString *)@"secondBoard"]; 

I assume SecondViewController is the name of your second controller.

Sign up to request clarification or add additional context in comments.

6 Comments

Thats what I wanted, but I see that I do not have to cast it if I use SecondViewController *secondview instead of UIViewController * secondview. But could you then explain to me what I have to insert in -(void)viewDidLoad to change my label? Functions/methods in OOP really confuses me.
[myLabel setText:@"My Text"] should do the trick. Make sure myLabel is not nil (as this will still work with no errors but nothing will be actually set)
Thank you, but that will not set myLabel to the returned text from method Name, would it? And how do I assure that myLabel is not nil?
I tried [mylabel setText:[self Name]];, no errors but still no action on my label :\ Doing it your way did produce text, so it should work, shouldn`t it?
I see that my problem is that I have the tabbarcontroller in the middle which also has to get a pointer *tabBar, but UITabBarController class dont have instantiateViewControllerWithIdentifier so I don`t know how to succeed at this
|
0

In my case I wanted to pass a string from FirstViewController to set a text field in SecondViewController, so:

1- In SecondViewController.h

@property (strong, nonatomic) IBOutlet UITextField *someTextField;// this is linked to a UITextField

2- In FirstViewController.m:

#import "SecondViewController.h"

3- I will do this after a press of a button in the FirstViewController:

SecondViewController *SecondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];

//set fields values

    SecondView.someTextField.text = @"HeLLO from First";


UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController: SecondView];                  

//show AdView

   [self.navigationController presentViewController:nav animated:YES completion:nil];

4- This is it!

5- There is a case, if I'm in FirstViewController and wanted to set a string property in the SecondViewController with the same way, the string won't get the value till you press a some kinda button in SecondViewController, it won't get it like in viewDidLoad for example! don't know why.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.