I have a program with Two windows (Both are controlled from the same Class File) and a Global Variable named PersonName1, Window A and Window B. On Window A there are 2 buttons, One which:
-(IBAction)setPersonName:(id)sender
{
PersonName1 = #"Tom";
}
And the other which changes from Window A to Window B
On Window B there is a button which does:
- (IBAction)loadNames:(id)sender
{
NSLog(#"%#",PersonName1);
}
The problem is whenever the screen changes, and when I click the button on Window B, the NSlog returns (null) instead of "Tom". What is causing this?
More information from comment:
PersonName1 is declared in the main ViewController.h file as so:
NSString* PersonName1;
and a property in that file aswell.
#property (nonatomic, retain) NSString *PersonName1;
This is then synthesised in the ViewController.m file. Both the IBActions listed in the question are also both in the ViewController.m file.
i think you did not pass value to one view controller into another view controller.
ex:
first view controller
have global value as NSString *nameString.
like below
#property (nonatomic, strong) NSString *PersonName1;
and
second view controller
it have global string as NSString *nameString
like below
#property (nonatomic, strong) NSString *PersonName1;
when you want pass value to one view controller into another mean.do like below
SecoundViewController *SVC = [[SecoundViewController alloc]initWithNibName:#"nibName" bundle:nil];
SVC.PersonName1 = self.PersonName1;
[self.navigationController pushViewController:SVC animated:YES];
Seems to me like you have to click first button on the first window to allocate value to PersonName1. If you didnt click first button but clicked second button instead then it will return null because declaration on PersonName1 will only happen if you click first button.
Related
i have two screen. i want click button first screen after navigation bar change in second screen.
i using storyboards
first screen code
-(IBAction) ChangeSecondPageTitle: (id) sender {
SecondViewController *second=[[self storyboard] instantiateViewControllerWithIdentifier:#"second"];
second.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
second.detailTitle.topItem.title=#"example";
[self presentModalViewController:second animated:YES];
}
second screen.h
#property (weak, nonatomic) IBOutlet UINavigationBar *detailTitle;
I guessing you are trying to set title for your second screen navigation bar.
Use a navigation controller to present your second screen.
In SecondDetailViewController instance, maintain a NSString property.
#property (strong, nonatomic) NSString *navBarTitle;
In viewDidLoad, just set the title.
[self setTitle:navBarTitle];
Actually the reason your code is not working is you set the property to your nav bar and then your viewDidLoad get called. In viewDidLoad it loads the nib file and return new allocated navBar. So to Solve this take a global string set it from first view and then in second in viewDidLoad set your navbar's title. Follow
Adithya's answer for that.
In second screen declare a property
#property (strong, nonatomic) NSString *newTitle;
and in viewDidLoad
self.title = _newTitle;
and in your first screen
...
second.newTitle = #"NEW_TITLE";
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.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
dismissModalViewController AND pass data back
I'm new to ios development and stuck on this problem:
I'm using storyboarding and have a navigation controller, vcA, with a TableView in it which shows some data from a MutableArray (which is initialized in viewdidload of the same class). After selecting any cell, a second view controller, vcB, is shown with a TextField in it and a button called "Add to list".
What I want is that when I enter some text in the TextField and press the "Add to list" button the text should be added to the array of previous view (which gets shown in the TableView) and when i tap the "Back" button on vcB's navigation bar, vcA should show the updated TableView with the new entry in it (on the top of list). Basically I want to add the text from vcB's TextField to the array of vcA and show the new array after clicking the BACK button.
I have searched a lot about this issue and seem to find that delegate and protocols is the way to achieve the desired result but I'm having trouble understanding delegation.
I have the second view controller presenting itself as modal in this example:
In the second view controllers h file:
#protocol SecondViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSString *)item;
#end
#interface SecondPageViewController : UIViewController <UITextViewDelegate>
{
NSString *previouslyTypedInformation;
}
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *previouslyTypedInformation;
#property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;
In the second view controllers m file make sure to synthesize properties and add then add this:
- (IBAction)done:(id)sender
{
NSString *itemToPassBack = self.textView.text;
NSLog(#"returning: %#",itemToPassBack);
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
//dismiss modal view controller here
}
Then in the first view controllers h file set it as delegate:
#interface FirstPageViewController: UIViewController <SecondViewControllerDelegate>
#property (nonatomic) NSString *returnedItem;
Then in the first view controller's m file synthesize and add the method:
- (void)addItemViewController:(SecondPageViewController *)controller didFinishEnteringItem: (NSString *)item
{
//using delegate method, get data back from second page view controller and set it to property declared in here
NSLog(#"This was returned from secondPageViewController: %#",item);
self.returnedItem=item;
//add item to array here and call reload
}
Now you have the text of what was returned! You can add the string to your array in the first view controller's viewDidLoad and call
[self.tableView reloadData];
and it should work.
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.
Can anyone tell me how can I show the value from one a UIPickerView in another view? I am storing a value in a label but I have to show this value in a button in another view controller.
Typically you would store the value data in your model when the user closes the second view. and the first view would read the value from the model when it re-appears (or use notifications). Your model data could be a plist, or nsuserdefaults or core-data etc.
Alternatively you could create a reference to the first viewController when you create your second viewController and assign this as a property on the second viewController. Then the second viewController in effect has a "path" to the first viewController:
The first viewController would have a property such as:
NSString *myStr; // in the header
#property (nonatomic, retain) NSString *myStr; // in the header file
#synthesize myStr; // in the implementation file
The second viewController would have a property such as:
firstViewController *firstVC; // in the header
#property (nonatomic, retain) firstViewController *firstVC; // in the header file
#synthesize firstVC; // in the implementation file
When you create the second viewController you would do something like:
secondViewController.firstVC = self; // in the implementation file
Then when you want to update myStr in the first ViewController (from the second viewController) you would do something like:
firstVC = #" my new value "; // in the implementation file