set UITextField.text from another class - iphone

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.

Related

Why can't this view controller set the text fields of the next view controller?

I have a view controller that has navigation bar with a done button, and two text fields. When the done button is pressed, the method postInfo is called. Here is the implementation:
- (void)postInfo{
ListingViewController* lvc = [[ListingViewController alloc] init];
NSString* listingName = listingNameField.text;
NSString* listingPrice = listingPriceField.text;
NSLog(#"%#", listingName);
NSLog(#"%#", listingPrice);
[lvc.titleLabel setText:listingName];
[lvc.priceLabel setText:listingPrice];
[self.navigationController pushViewController:lvc animated:YES];
}
Here ListingViewController.h:
#import <UIKit/UIKit.h>
#interface ListingViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *priceLabel;
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#end
The UILabel's are set through a xib file and are empty. Will post whatever other code is needed upon request.
At the time you do that push, the next controller's view hasn't been loaded yet, so you can't access its views. You need to create NSString properties in ListingViewController and pass a string to those in your postnfo method. Then in ListingViewController's viewDidLoad method, use those properties to populate the labels (which will have been loaded by the time viewDidLoad runs).
Change your code to this.
- (void)postInfo
{
ListingViewController* lvc = [[ListingViewController alloc] init];
[lvc view]; // loads the view
NSString* listingName = listingNameField.text;
NSString* listingPrice = listingPriceField.text;
NSLog(#"%#", listingName);
NSLog(#"%#", listingPrice);
[lvc.titleLabel setText:listingName];
[lvc.priceLabel setText:listingPrice];
[self.navigationController pushViewController:lvc animated:YES];
}
This happens because until the view property of the view controller is accessed, the view will not be loaded and all of the subviews are nil. They can be configured after viewDidLoad is called on the view controller being pushed. Calling [lvc view] loads the view immediately.
I usually get this too (in the example of when using segues). I get around it by setting an NSString property instead of an IBOutlet during prepareForSegue of the destinationController. Then during viewDidLoad of the next View Controller, I take the value of the property and assign it to the UILabel.
You may find the explanation in this answer useful: https://stackoverflow.com/a/8094146/2358334
If you set a breakpoint just after the line you call
testViewController *viewController = segue.destinationViewController;
when you build and run the project, you will find that the UITextField
property in the destinationViewController is not allocated and
initiated (memory is 0x0) at the breakpoint. Meanwhile the NSString
property is already allocated and initialised (so you can set its
value).
Try to do this
first you need to load the view (push viewcontroller) and then you can access the properties (because you have created views by IBOutlet
- (void)postInfo{
ListingViewController* lvc = [[ListingViewController alloc] init];
NSString* listingName = listingNameField.text;
NSString* listingPrice = listingPriceField.text;
NSLog(#"%#", listingName);
NSLog(#"%#", listingPrice);
[self.navigationController pushViewController:lvc animated:YES];
[lvc.titleLabel setText:listingName];
[lvc.priceLabel setText:listingPrice];
}

Any number that i put in the textfield it appears in the label as 0

i've been doing a thing here using objective-c, but i'm a beginner in this language, and that's what i did:
there is a class, named "firstviewclass", that is in the control of my first view, in this view there is a textfield that the user puts a number. the textfield is in the firstviewclass.h named "setNumber". There is another class, named "secondviewclass" that is in control of the second view, in this view there is a label that is in the secondviewclass.h, and i want that this label recive the value that the user put in the textfield from the first view, but when i test it on the iOS simulator any number that i put in the textfield it appears in the label as 0... I really don't know what to do!
My codes:
firstviewclass.h:
#import <UIKit/UIKit.h>
#interface firstviewclass : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *setNumber;
- (IBAction)gotonextview:(id)sender;
#end
secondviewclass.h:
#import <UIKit/UIKit.h>
#import "firstviewclass.h"
#interface secondviewclass : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *labelthatrecivesthetextfieldvalue;
#end
secondviewclass.m:
#import "secondviewclass.h"
#import "firstviewclass.h"
#implementation secondviewclass
#synthesize labelthatrecivesthetextfieldvalue;
-(void)viewDidLoad{
[super viewDidLoad];
firstviewclass *object = [[firstviewclass alloc] init];
NSString *string = [[NSString alloc] initWithFormat:#"%i", [object.setNumber.text intValue]];
labelthatrecivesthetextfieldvalue.text = string;
}
#end
First of all, I would strongly recommend not naming a property or instance variable setSomething. It will cause headaches for anyone reading your code because it will always look like you're trying to call a setter. Also, please do capitalize your class names.
Your actual problem is that in viewDidLoad you're creating an instance of firstviewclass, and then trying to get the value from setNumber. That is before the user had any chance to enter anything.
Also, the setNumber outlet in firstviewclass will probably be going nowhere anyway, since you're instantiating that class yourself, instead of loading the NIB.
Edit (ah, Storyboard, d'oh):
For Storyboard, you need to pass the setNumber text field's value to the second view.
First of all, remove the firstviewclass *object = [[firstviewclass alloc] init]; line.
Then, in your first view controller's prepareForSegue method, you can pass the value of the setNumber text field to a property in your your second view controller, and use it from there (e.g. in configureView).
I recommend working through Apple's Storyboard tutorial, it shows exactly what you need to do, step by step. The step you're having issues with right now, passing data to your next view controller, is here.
How are you initiating the setting of the label? From viewDidLoad? If so, then 0 would be correct because you have not set object.setNumber.text to anything. Also, you aren't carrying over the data from your first viewController. You need to let the second viewController know what the number is. Try setting this value in your NSUserDefaults, then in your second viewController, load that value from your defaults.
in the viewDidLoad of secondviewclass, you are creating a new instance of firstviewclass, and then accessing the property of the label. Since you haven't set that to be anything, it will always return zero.
Can you post the implementation of the gotonextview method?
EDIT:
I think you need to be setting the label property from the first view controller. so in your gotonextview method, add this line:
secondviewclass.labelthatrecivesthetextfieldvalue.text = self.setNumber.text;
I admit I'm a little lost in your naming scheme but that might work.

Label not updating when passing string between tabs in UITabViewController

I am a beginner of iOS and I am writing some practice code in which I am trying to pass a string from one tab to another in a UITabViewController and using a label to display it in the next tab. Now the code where I am passing the message is:
-(IBAction)sendMessage:(id)sender
{
MessageRecepientViewController * contoller = [self.tabBarController.viewControllers objectAtIndex:1];
[contoller passString:_textField.text];
self.tabBarController.selectedIndex = 1;
}
And I am receiving the text here (in the second view controller) as:
-(void) passString:(NSString *) str
{
_string = str;
}
And in viewDidAppear, i am doing this:
- (void) viewDidAppear:(BOOL)animated
{
self.textLabel.text = [NSString stringWithString:_string];
NSLog(#"did appear called, str = %# and label text = %#", _string, self.textLabel.text);
}
and the log is showing that the value of the string is the passed text as it should be, but the value of textLabel.text is always null.
I have tried everything I could think of from checking that the UILabel is attached to the textLabel outlet to writing the code in viewWillAppear and viewDidLoad but nothing have worked so far. The string is showing the correct value but the label is not updating. What is wrong here?
Ok found the error:
It was a minor typo: I had declared IBOutlet as:
IBOutlet UILabel * textlabel;
and property as:
#property (nonatomic, strong) UILabel * textLabel;
and had attached the outlet to textlabel and had been updating textLabel. Fixed this and things are fine!

How to send text from text field to another view controller

I'm making an app that behaves something like the default Messages.app in iPhone where a user composes a text message in a UITextField and upon tapping the Send button, the value of the UITextField in ComposeViewController will be transferred to the table cell's UILabel in a custom cell in MasterViewController and also to the DetailViewController where another UILabel will get the text value from the UITextField. The DetailViewController is the ViewController loaded when the user taps the cells from the UITableViewCells.
I actually read related articles below but it doesn't work on my end.
How to send the text from textfield to another class?
How to see text from one text field in another text field?
How to send text field value to another class
Can you please guide me on how to properly implement this? I know it's easy. I just don't know why it's not working. ComposeVC is a ModalViewController while DetailVC is the view that loads when the user taps the cell in the MasterVC's table.
Thanks a lot!
Below is my code for ComposeVC.h:
UITextField *messageTextField;
#property (nonatomic, retain) IBOutlet UITextField *messageTextField;
- (IBAction)buttonPressed:(id)sender;
for ComposeVC.m
synthesize messageTextField;
-(IBAction)buttonPressed:(id)sender
{
DetailVC *detailVC = [[DetailVC alloc] init];
detailVC.messageText = messageTextField.text;
}
for DetailVC.h
NSString *messageText;
#property (nonatomic, copy) NSString *messageText;
for DetailVC.m
#synthesize messageText;
- (void)viewLoad
{
testLabel.text = messageText;
}
testLabel is the UILabel inside my DetailVC.
You can simply create property on another viewController. Suppose your textField is on view1 and you want to send it on view2 then:
in view 2
.h file
#interface view2:UIViewController {
NSString *str;
}
#property (nonatomic, retain) NSString *str;
in .m file
#synthesize str;
Now you can send your text from view1 to view 2 like:
objView2.str = txtField.text;
For viewing one textfield's text in another use
secondTextField.text = firstTextField.text;
Hope this helped let me know if you are looking for something different.
take one variable in the app delegate like in appdelegate.h
#interface appdelegate
{
NSString *str1;
}
#property (nonatomic, retain) NSString *str;
In .m file synthesize it.
set the text after editing the textfield(app del.str=textfield.text i.e setting value).And use the same wherever you want.
NSString *str = appdel.str(getting value);
Try this one:
#interface SecondView:UIViewController
{
NSString *stringSecond;
}
#property(nonatomic, retain) NSString *str;
In First View u have to create an reference like this:
#import "SecondView.h"
SecondView *detailViewController = [[SecondView alloc] initWithNibName:#"SecondView" bundle:nil];
detailViewController.stringSecond = #"Some string";

adding view controller in navigationcontroller issue

I have the following class:
#interface DiscountDetailViewController : UIViewController {
UILabel * titleLabel;
UILabel * offerLabel;
}
#property (nonatomic, retain) IBOutlet UILabel * titleLabel;
#property (nonatomic, retain) IBOutlet UILabel * offerLabel;
#end
and I tried to do the following in the previous view:
discount = [[DiscountDetailViewController alloc] initWithNibName:#"DiscountDetailViewController" bundle:nil];
discount.titleLabel.text = temp.vendor;
discount.offerLabel.text = temp.description;
[self.navigationController pushViewController:discount animated:YES];
The issue is that, discount.titleLabel.text when printed is always null... I think it's because I define the titleLabel using interface builder.. so is there a way to resolve this?
I've hooked it up with IB as well..
i don't believe the iboutlets get hooked up until the view is on screen for the first time.
you could try setting the label after its displayed, or add another property to store the label text, then set the iboutlet label based on this new property in viewDidLoad in your DiscountDetailViewController.
If your outlets are properly connected then it is only because your titleLabel will not be available until your viewIsLoaded around -(void)viewDidLoad, I recommend you use an NSString property that can set the title label once view is loaded and update it when it is changed (override setter), or if you know your view is about to show try calling view first.
discount = [[DiscountDetailViewController alloc] initWithNibName:#"DiscountDetailViewController" bundle:nil];
//Force view to load, do not really recommend (though for some reason I am showing you how)
[discount view];
discount.titleLabel.text = temp.vendor;
discount.offerLabel.text = temp.description;
[self.navigationController pushViewController:discount animated:YES];