Access a TextField from another ViewController - viewcontroller

i have a problem with my code, i think.
Im trying to read out data from a textfield, which is on another view controller, but every time i try to access the text of the textfield its give me NULL.
I declared a instance of the SecondViewController and a property like this:
FirstViewController:
SecondViewController* controller;
#property (nonatomic, assign) SecondViewController* controller;
And with this Method i will access the text:
FirstViewController:
controller = [[[SecondViewController alloc] init] autorelease];
test = controller.textField.text;
NSLog(#"%#", test);
I also maked a forward Class declaration and imported the Header. The textField is also declared as property. And the "test" is a NSString in my FirstViewController with a property.
Why the NSLog() gives me (NULL)? I don't understand it...
P.S. Sorry for my bad English
Greetings

Related

set UITextField.text from another class

I have two views, and i'm trying to show text that i getting from first view in UITextField of another . Second view shown by - (source) so methods ViewWillAppear and ViewDidLoad won't work. And viewDidLoad method of second view is runs when app is started.
I'm tried to make method of second class
secondClass.h:
#property (strong, nonatomic) IBOutlet UITextField *itemName;//all hooked up in storyboard
-(void)SetName:(NSString *)name;
secondClass.m:
-(void)SetName:(NSString *)name{
NSLog(#"%#",name);
itemName.text = name;//itemName - textField
}
and use it in first one:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
[secondView SetName:#"Bill"];
NSlog shows "Bill" but textField.text won't change anything.
My guess that app shows UITextField without changes because it shows second view that it gets from viewDidLoad method and i need to update it somehow
My question: What is the best approach to change attributes of UI elements from different classes?
Easiest way:
secondViewConroller.h :
#property NSString * conversationName;
secondViewConroller.m :
#synthesize conversationName;
-(void)SetName:(NSString *)name{
NSLog(#"%#",name);
itemName.text = conversationName
}
On alloc:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
conversationName = #"Set this text";
[secondView SetName:#"Bill"];
I would suggest you to read about Protocols after that.
Easiest way:
in
secondViewConroller.h :
#property (nonatomic, retain) NSString *stringName;
secondViewConroller.m :
#synthesize stringName;
and in viewDidLoad method you write this line
itemName.text = stringName
On alloc:
secondViewConroller *secondView = [[secondViewConroller alloc]init];
secondView.stringName = #"Set this text";
guess there is something wrong with your itemName variable if it appears to get to the nslog.
did you create a referencing outlet in the interface builder for the textfield?
otherwise you can get the right textfield by tag, in IB put for instance tag 1 on the textfield and do in code:
UITextField *tf=(UITextField*)[self.view viewWithTag:1];
tf.text=name;
(replace self.view for the view holding the textfield, if not directly in the main view)
So i found a solution: There's was something wrong with calling method SetName: with parameters that i getting from first UIViewController.
Basically the solution is : create NSObject and put in there value from first UIViewConroller and then use it in second.
This TUTORIAL helped me to resolve the problem.

How to pass variable from one controller to another in ios?

I have one controller with variable like
NSString *str;
In same controller i am assigning userId in string str.
I need above userId in another controller to show user data.
How should I get value of userId in another controller?
Below is my code:
MainViewController.h
#property (nonatomatic) NSString *str;
MainViewController.m
#synthesize str;
str = 1;
Now in FirstViewController.h
#property(nonatomatic, retain) MainViewController *mainCon;
FirstViewController.m
#synthesize mainCon;
NSLog(#"user id is %#", mainCon.str);
Here in log i am getting null value.
Your best bet is going to be in the [prepareForSegue] method
within that method I usually have something like
if ([segue.identifier isEqualToString#"segueName"]){
//I use this if statement to check which segue I am performing, if I have multiple
//segues from a single view controller
FirstVc *newViewController = segue.destinationViewController;
newViewController.str = self.str;
}
This will pass the string from your main VC to your new VC. This is all assuming that your transitions are laid out in Interface Builder or some way that gives you access to the segues and identifiers
Take a Property in MainVC and Pass value from FirstVC then It will work for you.
the following code must be in MainVC
#property (strong, nonatomic) NSString *str;
#synthesize str;
in FirstVC
MainVC * vc = [[MainVC alloc]init];
vc.str = #"test";
Step1:
You have to create a viewcontroller class as Constants
Step 2:
You can delete .m file in Constanst class
Step3:
You have to import the Constants.h in Projectname_Prefix.pch which will be xcode's in other resources folder. Now Constant class will act as global class.
Step4:
Now, you can declare variables in Constants.h. The variables those declared in Constants.h will act as global variable. you can access it anywhere.
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondViewController.str1 = str;
[self.navigationController pushViewController:secondViewController animated:YES];
It's a quicker and easy solution.

Pass variable to another view

I'm trying to set a variable in another view.
I'm in a view which I have named ProgramViewController and from here I would like to set a variable named bands in MyViewController.
I thought it would be as simple as
MyViewController *myViewController = [[MyViewController alloc] init];
myViewController.bands = #"hello world";
[myViewController release];
And in the header of MyViewController:
#interface MyViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> {
NSString *bands;
}
#property (nonatomic, retain) NSString *bands;
That does not work, though.
Can anyone please tell me what I am doing wrong?
EDIT:
I synthesize bands in MyViewController:
#synthesize pageNumberLabel, tableProgram, bands;
But when trying to print it with NSLog in the viewDidLoad of MyViewController I get (null)
NSLog(#"%#", bands);
You need to synthesize the bands variable in MyViewController.m
#synthesize bands;
A variable needs to be synthesize in order to use it as a public property outsize of a class in Objective-C
I solved this in another way. I managed to load the data in MyViewController instead. I was dealing with a page controller and had to populate a UITableView but had troubles loading the data.
This is solved in a completely other way now.

how can I display MSMutableArray data in UITableViewController?

I am getting data from NSTextField, and saving data in NSMutableArray. I want to show the same data in another class which in child of UITableViewController.
How can I show data of NSMutableArray to myTableView which is another class??
Help me out, please
Surely, I will appraise if I found proper way.
Your tableView is in a MyViewController class. You need to create a NSMutableArray *sourceArray attribute on that class, as well as the associated property, using for instance:
#property (nonatomic, retain) NSMutableArray *sourceArray;
Then when you instantiate this View Controller or whenever you make it appear, assign the results to sourceArray :
MyViewController *mvc = [[MyViewController alloc] initWith...];
mvc.sourceArray = theResult;
[self presentModalViewController:mvc animated:YES];
[mvc release];
Then use the sourceArray as the Table View datasource.
Make a property of NSMutableArray in app delegate class and assign the result of the source array to it as soon as you fetch any result, because you can access its instance all over your project.

Objective-C - simple setting property example erroring with 'request for ___ in something not a structure or union'

I've been banging my head with this one this evening and am sure it's something very simple that I've missed
I've created a new project with the appdelegate and a view controller class as below. The view controller synthesises the property in the .m, and the app delegate header imports the view controller .h file. Code below:
View controller header:
#interface untitled : UIViewController {
NSString *string;
}
#property (nonatomic, retain) NSString *string;
App delegate:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
testViewController = [[untitled alloc] initWithNibName:#"untitled" bundle:nil];
testViewController.string = #"Testing String";
[window addSubview:testViewController.view];
[window makeKeyAndVisible];
}
Can someone please help and point out the obvious mistake as to why setting the string property fails here with the error mentioned? Is it because of being inside this method? I've never had issues setting properties in other methods before after initing a view controller.
Thanks.
The error is saying it does not understand that the class has that property. It means you have either the wrong class or that it knows nothing about the class.
So, you need to add:
#import "untitled.h"
in your application delegate - also, you need to have the variable be of type "untitled" (I am pretty sure you declared the type as UIViewController and not untitled):
untitled * testViewController = (untitled *)[[untitled alloc] initWithNibName:#"untitled" bundle:nil];
By the way, by convention you should always start class names in uppercase.