Pass a string value from one view controller to another - iphone

I am using this way to pass a string value from one view controller to another but still it's not working. Please help! From the main menu login method I pass a result value of a web service to SecondMenu:
- (IBAction)MainMenuLogin_Method:(id)sender
{
SecondMenu *lc2=[[SecondMenu alloc] initWithNibName:#"SecondMenu" bundle:nil];
lc2.username_TextField.text=#"hello";// in actual it is a soap result
//[self presentModalViewController:lc2 animated:YES];
[[[[UIApplication sharedApplication] delegate] window] addSubview:lc2.view];
}

The most likely explanation is that the username_TextField property is not linked to the actual UITextView. (1) Check that the property definition of username_TextField in the SecondMenu header is defined as an IBOutlet and that (2) it is actually linked up in Interface Builder.
Edit:
Looking at the full code at your link, it looks like the username_TextField property is a property of the Class LoginController but in this code you have instance lcv2 as Class SecondMenu. There is nothing in the code you provided to show that the SecondMenu Class has a username_TextField property. If you initialize lcv2 as an instance of LoginController the code above will work assuming you have the outlets wired correcting in interface builder.

Related

How to pass change the value of a variable in another view(objective c)?

i have two controller
in view1.h i have
NSString *bg;
#interface Lottery5ViewController : UIViewController {...}
in view2.m i have
-(IBAction) switchView2{
self.bg = #"blueButton.png";
}
but it cannot compile and said
request for member 'bg' in something not a structure or union
i just want to modify the value of a variable in view1 from view2
Your problem is that in view2's switchView2 method you are calling self and trying to change an ivar (or property, not sure which it is from the code shown) of a different class. If you want to send something from one instance to another you have to establish some sort of connection between them first. There are a few ways you can get them talking. The better ways to be able to pass something like this is to set up a delegate protocol or use notifications, then view2 could call the delegate method that makes the change in view1 or post a notification that view1 would be registered for so that it could make the change. The less desirable way to accomplish this would be to give view2 an ivar (or property) that is view1. You would then be able to use something like view1.bg = #"blueButton.png";.

Objective-C (iOS) Access UITextField's text from another class

this question is extremely basic and I apologize if the answer should be staring me in the face.
So: I have a UITableView and another viewController. I'm updating some information for CoreData in the UITableView's .m file. In order to do this I need the text in a text field in the other viewController.
So being rather inexperienced, I tried:
[entity setName:[NSString stringWithFormat:#"%#", viewController.entityNameInputField.text]];
I've renamed a few things for clarity. Entity is my entity (no kidding), the attribute I'm setting is called Name, viewController is where the textField is and the text field is called entityNameInputField.
I'm a lot more interested in why this sort of thing doesn't work than what the solution to my problem might be. Why can't I just get the text from the text field in this way?
I suppose to word everything differently:
If I'm in Class A and I want to access the value of say, an NSString in class B, why can't I just use ClassA.TheString or [ClassA TheString] .... from what I've managed to understand, I'm sending these objects getter methods and I can't seem to see why this doesn't work...
Any help would be appreciated :)
Some more code would be useful to see what might be causing your confusion, but if you have declared properties in your classes the associated instance variables (or have manually declared accessors) then you will be able to access the associated ivars.
So, something like this (below) will work fine.
MyClassA * instanceOfA = [[myClassA alloc] init];
MyClassB * instanceOfB = [[myClassB alloc] init];
instanceOfA.myProperty = instanceOfB.myProperty;
As far as I understand, this is not a problem of accessing values from another class. Assuming that both view controllers are retained in memory, you can always call each other's getters/setters.
I assume that you've got a UITableViewController named A and then you're presenting a custom UIViewController named B with a text field. The user taps something in the text field and you want this text to go back to your table view controller. If inside B you have a valid reference to A, you can then call it's setter for the appropriate variable, and then dismiss the custom controller.
For example, in B's header you could have:
#interface MyViewController : UIViewController {
id tableViewController;
}
#property (nonatomic, retain) id tableViewController;
then in A, when you're creating B:
MyViewController *B = [[MyViewController alloc] init];
B.tableViewController = self; // this passes a reference to A
so when you're in B and the user has entered input in the text field, you could do something like
[self.tableViewController setVarname:textField.text];
assuming there's a varname variable in A, and a textField in B.
You can also gain access to A without having to create a separate variable, by calling
[self parentViewController]
if that suits you better.

passing array from one viewcontroller to another

i have a custom tabBar class in which i switch three view controllers ,i am removing the presentview controller and presenting the other .
custom tabbar class
-list
-inbox
-messages
now i have to pass an array from list to inbox
i usually create an instance of the recieving class likeInbox *inbox=[[Inbox alloc]init];
inbox.array=self.array;
but in this case its not working.the array in inbox class returns null when i nslog it
Do you inherit or use the UITabBarController in your custom tabBar?
If you want to pass a variable between list<->inbox<->messages I would suggest that you implement a method in the tabBar that can be called from the subviews. The method would send the array to the appropriate subview.
Check your property in Inbox class and check to see if self.array is not null.
Edit try: inbox.array = [NSArray arrayWithArray:self.array]
ok this worked when i passed the value to applicationdelegate and from the other class accessed from here.

sharing objects in Objective-C

This is a noob question. If I have a ViewController and inside that class I have an object called UserInfo and other ViewController, lets just call it X,Y,Z, etc..etc.
What do I need to do so that those X,Y,Z can use the information of UserInfo?
Well I can have another info called UserInfo inside X,Y,Z and pass UserInfo inside, but I don't think this is good OOP technique. I think inheritance is needed here... but I don't think that it is right too, as the only commonality they have is only the UserInfo
View controllers are just objects, you have to remember that.
If you're in your first view controller and it has a property NSString *aString to pass it to the next view controller you just make the second view controller's NSString *aString property equal to the first one.
Basically do this:
MyNextViewController *viewControllerTwo = [[MyNextViewController alloc] initWithNibName:nil bundle:nil];
[viewControllerTwo setAString:self.aString];
if you subclass all you inherit are the properties, and not their values.

Objective-C: Instance Method is Never Called

I've got a class called RootViewController with this:
Test *myLevelObject = [[Test alloc] init];
[myLevelObject afficher];
It is supposed to call my method -[ Test afficher] but there is no warning or error and it does enter the method. Any ideas? thx
Declaration of test class :
test.m :
#import "Test.h"
#import "RootViewController.h"
test.h :
#import <UIKit/UIKit.h>
#class RootViewController;
#interface Test : UIViewController <UIActionSheetDelegate,UITextFieldDelegate>{
-(void) afficher{
NSLog(#"hello");
poids.text = [NSString stringWithFormat:#"lol"];
labelalco.text = [NSString stringWithFormat:#"cac"];
}
it show in console "hello" but it doesnt execute my 2 line of code.
ok now i tryed to do UIAlertView and it works but it fails only when i use the variables
that are initialized in Test.h with interface builder.
Initialization :
IBOutlet UILabel *labelalco;
Interface builder :
alt text http://img72.imageshack.us/img72/4507/screenshot20100225at090.png
this is another screen shot of where the test and the 'drink' section which is rootviewcontroller is shown .
THANKS
It's possible that your init method is passing back nil, instead of an object. If this is the case, than the -afficher method is sent to nil, which doesn't break, but doesn't DO anything. Check your return value after the alloc/init.
Since you say an NSLog message within the afficher method is written to the console, your method is empirically being called. That is doesn't do what you intend it to do is a bug in your code. From the minimal listing you've provided, I'd guess that poids and lableco are not initialized properly (meaning they're still nil when you method is called).
If you're using IBOutlet in -init method, it won't work there (I guess it's set to nil), but it should work in -awakeFromNib method.
I echo what was stated above. Your afficher is likely getting invoked if you see the "hello" output in your console. (To be absolutely certain, clear your console prior to running and check that it shows again to rule out stale output.) One other thing I would suggest is to use an NSAssert() to be sure your outlets are set. Here's an example:
-(void) afficher{
NSLog(#"hello");
NSAssert(nil!=poids, #"poids should be set.");
poids.text = [NSString stringWithFormat:#"lol"];
NSAssert(nil!= labelalco, #"labelalco should be set.");
labelalco.text = [NSString stringWithFormat:#"cac"];
}
I just looked very closely at your screen shots and it appears that your Test object is a UIViewController. From the things you describe I bet you're assuming the labelalco to be set via interface builder connections. That will not happen in your 1st code snippet because you instantiate it directly without using the nib file.
Test *myLevelObject = [[Test alloc] init];
[myLevelObject afficher];
should be:
Test *myLevelObject = [[Test alloc] initWithNibName:#"Test.xib" bundle:nil];
[myLevelObject afficher];
Assuming Test.xib is where the connections are made in IB. It looks like this is a sub controller in your main window's tab view controller so there may be additional complexity in what you're trying to do.
Your latest comments say you're using a UITabBarController that contains RootViewController and Test.
But in RootViewController, you're trying to call a method in Test by creating a new instance of it. This instance would be separate from the one the tabbarcontroller is using.
I think in RootViewController, you need to instead obtain a reference to the same instance of Test the tabbarcontroller is using.
See this post for a possible way to do this:
iPhone SDK: How to access a ViewController nested in a TabBar from MyAppDelegate?
However, why do you have code in RootViewController that updates Test? If Test is displayed, could it not update itself instead? How does the code in RootViewController get executed after Test has already been displayed?