Unable to pass data between MasterVC to DetailVC in iOS - 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.

Related

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

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

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;

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.

Setting a property in prepareForSegue using a popover

I have a ViewController that has a couple of buttons on it created in Interface Builder. The first button will display a popover linked up in IB, it is linked to a UINavigationController and has a TableView under it with a class of PopupViewController.
The second button, I have an action setup for goToCategory and when clicking on that, I want to set a property on the PopupViewController
ViewController.m
//go to category
-(IBAction)goToCategory:(id)sender{
NSLog(#"GO TO CAT");
//PopupViewController *popupVC = [self.storyboard instantiateViewControllerWithIdentifier:#"popoverVC"];
//popupVC.currentLevel = 1;
[self performSegueWithIdentifier:#"popoverSegue" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(#"seg1");
if ([[segue identifier] isEqualToString:#"popoverSegue"]){
//PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController];
//PopupViewController *popupVC = [segue destinationViewController];
PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0];
popupVC.test = #"just a test";
NSLog(#"seg2");
}
}
PopupViewController.h
#property (nonatomic, strong) NSString *test;
PopupViewController.m
#synthesize test;
-(void)viewDidLoad{
NSLog(#"test: %#", test); //returns test: (null)
}
I've found a lot of answers on SO, hence some of the commented out lines in my prepareForSegue. But none of these set the value of test. PopupViewController *popupVC = [segue destinationViewController]; throws an error due to it referring to the UINavigationController so I can't use that as it is, even though that seems to be the way to do it in a lot of answers I've seen. But no matter which way I try doing it, the output is always null?
UPDATE:
PopupViewController *popupVC = (PopupViewController *)[[segue destinationViewController] visibleViewController]; and PopupViewController *popupVC=[[[segue destinationViewController]viewControllers]objectAtIndex:0]; from my prepareForSegue above both work on the 6.1 simulator. My iPad's iOS is 5.1.1 which it isn't working on. Is there something different I need to do for iOS 5?
Try this
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSLog(#"seg1");
if ([[segue identifier] isEqualToString:#"popoverSegue"])
{
UINavigationController *navController = (UINavigationController*)[segue destinationViewController];
PopupViewController *popupVC = [navController topViewController];
popupVC.test = #"just a test";
NSLog(#"seg2");
}
}

In storyboard, Popoverview don't receive delegation

I'm working sample app by storyboard.
When made popoverview, I used the way - 'Embed in navigation controller'.
But I'm getting a big trouble of delegate usage.
It's that don't delegate to popover view's.
[ViweController.m]
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([[segue identifier] isEqualToString:#"PopRootViewController"]){
NSLog(#"[segue destinationViewController] :%#", [segue destinationViewController]);
rootViewController = [[RootViewController alloc] init];
rootViewController.delegate = (id)self;
NSLog(#"%#---%#---%#", rootViewController.delegate, self, rootViewController);
}
}
-(void)didTap22 {
NSLog(#"delegate step 1 success!! ");
}
The result of this source like this :
[segue destinationViewController] :<UINavigationController: 0x88660a0>
<ViewController: 0x6b795e0>---<ViewController: 0x6b795e0>---<RootViewController: 0x6b7da60>
but [RootViewController] don't receive the delegation.
[RootViewController.m]
#implementation RootViewController
#synthesize items, delegate;
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
....
self.items = mutableFetchResults;
NSLog(#"333333..... %#, ....... %#", self.delegate, self);
}
results :
333333..... (null), ....... <RootViewController: 0x8866520>
RootViewController's delegation is null.
I can't find solution about this.
Anybody help me, please!
I suspect you didn't declare your delegate UIPopoverControllerDelegate in the class declaration of your view controller.
This line is suspicious:
rootViewController.delegate = (id)self;
you don't need to cast self normally if you made the declaration I am mentioning.

Resources