How to make textFields from different classes equal the same text? - ios

I'm Beginner in iOS development. In my FirstViewController I have two textFields and I use the UITextFieldDelegate to make these two equal the same content. In my SecondViewController I have another textField and I need to make it have the same content with the textFields from the FirstViewController.
FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
_FirstTextField.delegate = self;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
SecondTextField.text = textField.text;

Its simple
pass the text of text field in FirstViewController to SecondViewController
using string and assign the passed string to the text field of SecondViewController
For passing the string you can have many option
1) Using Segue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"secondViewSegue"]) {
SecondViewController *objVC = [segue destinationViewController];
objVC.strText = textfeld.text;
}
2) Passing data directly while pushing
SecondViewController *objVC = [[SecondViewController alloc] initWithNib:#"SecondViewController" bundle:nil];
objVC.strText = textfield.text;
[self pushViewController:objVC animated:YES];

You can pass the variable to the second View Controller in the Segue.
Passing variables between view controllers

Related

iOS Segue PopOver How To Back Pass Data

i want send data view controller (left) from PopOverController (Right), how can I do it?
You can pass data in prepareForSegue method, but first add identifier for segue.
And use this code:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"showPopover"]) {
NSLog(#"FirstViewController: prepareForSegue");
PopOverController * popoverVC = segue.destinationViewController;
popoverVC.myProperty = #"Data to be passed";
}
}
First make one property which you get data of another viewcontroller .
In you case assume we want String data to first viewController so we make one property in second means PopOverController
#property (nonatomic, strong) NSString *recipeName;
After call prepare for segue method in first viewController in your case ViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"IdentifierOfPushViewController"]) {
RecipeDetailViewController *destViewController = segue.destinationViewController;
destViewController.recipeName = #"Hello this passing data"
}
}

Setting property in Segue but in ViewDidLoad returns null

Ok this really makes me headache. I have ViewController (using replace) that I assign its delegate property
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"spoSelectionDemoPlanSegue"])
{
SPOSelectionViewController * vc = [segue destinationViewController];
vc.delegate = self;
NSLog(#"spo segue %#", vc.delegate); //returns current ViewController
}
}
SPOSelectionViewController.h
#property (nonatomic, assign) id<SPOInputDelegate> delegate;
SPOSelectionViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"delegate didload spo %#", delegate); //returns (null)
}
Somehow it prints null! However there is my other ViewController (using popover) that I try to assign its delegate and it does not null. Anyone can give me suggestion?
As I expected and this is silly, since the storyboard's segue is to a navigationController, so actually the destinationViewController is the navigationController not SPOSelectionViewController.
So I solved it by this way:
UINavigationController* navController = segue.destinationViewController;
SPOSelectionViewController * vc = (SPOSelectionViewController*)navController.topViewController;
vc.delegate = self;

Unable to pass data between MasterVC to DetailVC in iOS

I'm trying to pass data(_claimReportToDetailView) from viewDidLoad (of MasterVC) to DetailVC. It's always null.
#interface LAMasterViewController ()
{
NSArray *_claimReports;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_claimReports = [[LADataModelController getSingleton] getClaimReportsOrderedByAccessedDate];
LADetailViewController *detailViewController = [[LADetailViewController alloc] init];
detailViewController.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
NSLog(#"claim%#",detailViewController.claimReportToDetailView); // captures here properly.
}
#interface LADetailViewController : UIViewController
#property(nonatomic ) LAClaimReport *claimReportToDetailView;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(#"sdfdf%#", _claimReportToDetailView); // logs null always.
}
Your viewDidLoad seems strange. You have this line:
LADetailViewController *detailViewController = [[LADetailViewController alloc]init];
Yet you say that the view controller is on the storyboard.
I think what is happening is that you are creating this VC, and setting it's property, but the Storyboard is loading a completely new VC, for which you haven't set the property.
Usually, the way you pass information to VCs on Storyboards is in the prepareForSegue: method.
You have to pass the data to the details view controller in prepareForSeque method in master view controller:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:#"showDetail"]) { //<-make shire the segue identifier match one in storyboard
_claimReports = [[LADataModelController getSingleton] getClaimReportsOrderedByAccessedDate] ;
LADetailViewController *vc = (LADetailViewController*)[sender destinationViewController];
vc.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
}
}
This should fix your project. Give it a try
//Allocating LADetailViewController instance
LADetailViewController *detailViewController = [[LADetailViewController alloc]init];
//Connecting it the specified VC on storyboard
detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"yourVCID"];
//Now the connection is set, so pass the data you need
detailViewController.claimReportToDetailView = (LAClaimReport *)_claimReports[0];
//Depending on present or push you should try one of the 2 lines
[self.navigationController pushViewController:detailViewController animated:YES];
//or
[self presentViewController:detailViewController animated:YES completion:nil];
This happens because whenever you navigate from one UIViewController to other, it is initialized again, so rather than setting value in viewDidLoad, try to set value on the event when you perform navigation and make navigation through code rather than with segue.

IOS7 Change value of label in different view before segue (Same controller)

I have 2 simple views in my Storyboard and both use the same ViewController. I have the following code to switch the the second view...
self.labelStatus.text = #"CHECKING";
[self performSegueWithIdentifier:#"LoginSuccess" sender:sender];
*labelStatus is on the second view.
For some reason the labels text is not changing. Can anyone advise why this is?
[self performSegueWithIdentifier:#"LoginSuccess" sender:sender];
//change the label value in prepareForSegue: method
- (void)prepareForSegue:(UIStoryboardSegue *)segue {
if ([segue.identifier isEqualToString:#"LoginSuccess"]) {
UIViewController *loginVC = segue.destinationViewController;
//declare the string in .h file and assign it in viewDidLoad in loginVC
loginVC.labelString = #"CHECKING";
}
}
in loginViewController.h file
#property(nonatomic, strong) NSString *labelString;
in loginViewController.m file
- (void)viewDidLoad
{
[super viewDidLoad];
self.labelStatus.text = labelString
}
You can customise your destinationViewController (properties, views, etc) in prepareForSegue: method, which is called before segue is executed:
- (void)prepareForSegue:(UISegue *)segue {
if ([segue.identifier isEqualToString:#"mySegue"]) {
UIViewController *destVC = segue.destinationViewController;
// do your customisation
}
}
Edit-
If you need to change the label in the same view controller, then you should modify the label in the prepareForSegue method.

Passing Information Through a Segue

I'm attempting to obtain the HTML code from a UITextView and let users view it, live, in a UIWebView in the next segue. Here is what I have so far.
DetailViewController - view with UITextView:
UITextView: codeView
PreviewViewController - view with UIWebView
UIWebView: webView
UIWebView Code: webViewCode
DetailViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([[segue identifier] isEqualToString:#"webViewCode"]) {
PreviewViewController *preview = [segue destinationViewController];
preview.webViewCode = self.codeView.text;
self.preview.webViewCode = [[NSString alloc] initWithString:codeView.text];
[[segue destinationViewController] setWebViewCode:codeView.text];
}
}
PreviewViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[webView loadHTMLString:webViewCode baseURL:nil];
}
Everything is connected in the interface builder, including the Storyboard Segue Identifier. As you can see I've tried multiple ways on passing the information through but have only come up with errors. Is there something I'm missing?
This code does not make sense:
if ([[segue identifier] isEqualToString:#"webViewCode"]) {
PreviewViewController *preview = [segue destinationViewController];
preview.webViewCode = self.codeView.text;
self.preview.webViewCode = [[NSString alloc] initWithString:codeView.text];
[[segue destinationViewController] setWebViewCode:codeView.text];
}
You create a local variable preview, of type PreviewViewController. As the other poster pointed out, you need to add a typecast to this to avoid a compiler warning:
PreviewViewController *preview =
(PreviewViewController*) [segue destinationViewController];
The real problem, though, is that you then ignore the local variable you just defined, and try to use a property, self.preview, which is backed by a completely different instance variable, and probably nil. Don't do that. get rid of the "self." bit in this line:
preview.webViewCode = [[NSString alloc] initWithString:codeView.text];
And then delete the line below completely, since it does the same thing as the line before.

Resources