Unable to pass data between a table view and another view controller - ios

I'm having trouble transferring data between a table view controller and another view controller.
On my sending controller - tableviewcontroller.m - I have this under prepareForSegue:
FriendDetailViewController *fvc = (FriendDetailViewController *)segue.destinationViewController;
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
PFUser *u = self.friends[path.row];
fvc.nameLabel.text = u.username;
//friendViewController.m (the destination view controller)
- (void)viewWillLoad {
[super viewDidLoad];
if (self.user) {
self.nameLabel.text = self.user.username;
}else{
self.nameLabel.text = #"No data to display";
}
}
The label displays "No data to display".
I've read other posts and viewed tutorials but I cannot figure this out.

You're only setting the nameLabel in your prepareForSegue, not the actual user, so it is uninstantiated when your new VC loads.
Try adding this into your prepareForSegue:
fvc.user = u;
Assuming your user you want to pass is stored in u, that should do it (also assuming user is a public property of the new view controller.)

You can put an attribute in your .h file of FriendDetailViewController and the set it before calling:
fvc.attribute = u.username;
After this, you can call pushViewController for "fvc" inside didSelectRowAtIndexPath

You can't set the UI actions on prepareForSegue.
You could instead define the user variable on destination viewcontroller.
After that, on prepareForSegue, you can set the selected PFUser to the destination viewcontroller's user.
And when you'd like to show the user's name, use viewWillAppear instead of viewWillLoad.
If you can't understand, please let me know, I can show detailed code for all.

Related

pass data from UITableCell to UITabView

I am currently working on a project that requires a list of customers to be displayed in a UITableView, the associated cell then segues to a TabView to display a detailed customer record in a tabbed ui.
I have setup the story board with the required TableView and populated fine. The TabViews all setup and I have added a custom class to the main TabView controller which can take the ID (required to interrogate service and return further data) and Customer Name. I have also added a UIViewController for the first tab in which I need to get the ID value.
I can't seem to get hold of the ID or Company Name that is passed. I have tried importing the .h file of the UITabView. I know the UITabView .h file is being populated with the values as in the .m file I am using the Customer Name to update the title of the Navigation Bar. However, whenever I breakpoint on line that gets the ID in the .m file for the individual tab, it always returns nil.
I am using the following code to try and get this value:
companyTabController *headerData = [companyTabController alloc];
_companyName_lbl.text = headerData.companyName;
_companyID_lbl.text = headerData.ID;
I have tried several variations of the above and all to no avail.
You can also use NSUserDefaults to save the data, I think that is the simplest way to save the data throughout the app.
From the code you posted, the headerData is a new instance. So the companyName and the ID will be nil unless you assign some value to them.
Since, you mentioned that you are able update the navigation bar title, try using the same object for fetching the values in this controller as well. (Maybe you can use a singleton object)
If your segueing you have to use the prepareForSegue:sender: method as such:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
companyTabController *companyTC = [segue destinationViewController];
companyTC.companyName_lbl.text = headerData.companyName;
etc
}
if your not segueing you will have to instantiate it as such :
- (void) didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [self.tableView cellForRowAtIndexPath:indexPath];
companyTabController *companyTC = [self.storyboard instantiateViewControllerWithIdentifier:#"CopmanyTabController"];
companyTC.companyName_lbl.text = selectedCell.textLabel.text or = headerData.companyName;
[self.navigationController pushViewController:companyTC animated:YES];
}

In iOS how to get object fromViewController name?

I know that this is double question. I know that I can use property in toViewController to get name of UIViewController to get NSString which tells me where I am coming from.
Anyway I want to ask if there a simple way to get name of UIViewController when unwinding from segue.
I have a UIViewController with segues to 3 forms. I programatically return to that view controller. I need to run a specific code only when I am returning from one of view controllers. My goal is using string from name of fromViewController start that specific code.
Using UIViewController by NSString from its class name isn't safe enough because the name can be changed.
You can use isKindOfClass instead:
UIViewController *destinationViewController = segue.destinationViewController;
if ([destinationViewController isKindOfClass:[MyViewControllerClass1 class]]) {
// put code related to transition to MyViewControllerClass1
}
else if ([destinationViewController isKindOfClass:[MyViewControllerClass2 class]]) {
// put code related to transition to MyViewControllerClass2
}
You can use:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
UIViewController *destinationViewController = segue.destinationViewController;
NSString * identifier = destinationViewController.identifier;
NSString * title = destinationViewController.title;
}
Create a Custom delegate method in the primary VC, create 3 strings with unique name so that u can identify.
EG.
NSString* stringFrmFORM1, *stringFrmFORM2, *stringFrmFORM3;
-(void)setString:(NSString*)myString{
//set the string from the VC1,2,3 to each string based on Primary VC's strings
}
Call the delegate method from each registration VC, and set those Strings.
You will have your registration strings to each of the Unique strings that you have set, from each of the Registration VC's.
To answer your base question, you can get the name of a class in string form with:
NSString *strClassName = NSStringFromClass([fromViewController class]);
but as #AlexPeda pointed out in ze answer, -isKindOfClass: would be better.
if ([fromViewController isKindOfClass:[SpecificViewController class]]) {
//run your 'specific' code
}

How can you pass data from a UIContainerView to an other UIContainerView, which are in the same parent viewcontroller?

I'm struggling to get my data from a UIContainerView to another UIContainerView. For example: I have 2 containers, which are calculatorContainer and displayResultContainer. So when I press "=" button to calculate the result, I want it to show up in the displayResultContainer. I already tried different options with the segue method and parentViewController access, but still no luck.
There are two possibilities.
1.Using appDelegate. Use a property in app delegate to pass data between containers.
Put this in first container
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
appDelegate.dataToPass=dataToPass;
in the second container
MyAppdeleagte appDelegate=[[UIApplication sharedApplication]delegate];
dataToPass=appDelegate.dataToPass;
2.Using ParentViewController.
in first container
ParentViewController parent=(ParentViewController *)[self parentViewController];
parent.dataToPass=dataToPass;
in the second container
ParentViewController parent=(ParentViewController *)[self parentViewController];
data=parent.dataToPass;
Use delegates.
Steps:
From 1st UIContainerView, call a delegate method to the parent view controller.
The parentview controller then pass the value to the 2nd UIContainerView.
You have to declare this method in the second UIContainerView:
UIContainerView2
-(id)initWithsetresult:(Int)Result {
int showPreResult = result;
return self;
}
On button action:
antagonist = [[UIContainerView2 alloc]initWithsetresult: calculateResult];
Here is an easy method, but first you should know that this method works for forward flow not backward (in backward flow you will need a thing called delegate):
View 1 = calculator
View 2 = result display.
In view2 get a text field and an NSString in its .h file:
#property(nonatomic,strong) NSString *myResult; //to contain the result
#property(nonatomic,strong) IBOutlet UITextfield *myResultText; // to display the result
In the first View (view1) take inputs and add buttons like (addition +) etc.:
#property(nonatomic,strong) IBOutlet UIButton *addition;
Connect that Outlet(button) with the view controller. Then add a method for that button and connect it to that button:
-(int)addFunc;
Then take in user input from 2 text fields that you have already made in the 1st View (input1TextField and input2TextField):
Inside the addFunc write:
(int)addFunc{
int input1,input2,result;
input1 = self.input1TextField.text;
input2 = self.input2TextField.text;
result = input1+input2;
return result;
}
Now inside the IBaction method write:
-(IBAction)myIbaction{
[self performSegueWithIdentifier #"seguename", sender :self]; // set the segue name and fill it here.
SecondViewController * sec = [segue destinationViewController];
int result = [self addFunc];
sec.myResult = (NSString)result;
}
Now in the second View (view2) in viewDidLoad method write:
-(void)viewDidLoad{
self.myResultText = self.myResult;
}
And it will display your result.
Don't know if it helps or not but surely you get the picture that this is how you will be able to perform it.
Hope it helps.

How to pass value to another view controller?

I am new to IOS Programming. I am sending an integer value to another view controller from segue like this:
SlideShowViewController *destViewController = segue.destinationViewController;
[destViewController setSelectedButton:tagIndex];
I am successfully accessing this value in my SlideShowViewController.m file:
#synthesize selectedButton;
NSLog(#"%i",selectedButton);
Now I want to access this same value which saved in selectedButton in my other View Controller. So I did this in my viewDidLoad of AnotherViewController:
SlideShowViewController *button= [[SlideShowViewController alloc] init];
NSLog(#"selected button is %i",button.selectedButton);
But I am not getting the value here.
While going to second view controller do same as you did for first
SecondViewController *destViewController2 = segue.destinationViewController;
[destViewController2 setSelectedButton:tagIndex];
In your SlideShowViewController.h create a property to hold data.
In your SlideShowViewController.m synthesize property and remove
SlideShowViewController *button= [[SlideShowViewController alloc] init];
this line from SlideShowViewController.m. In this line your are creating new instance of SlideShowViewController.
In the last code section, you're creating a new instance of the SlideShowViewController. This instance won't have the same variables as the first instance. In order to access the selectedButton field, you need to have a reference to your first SlideShowViewController instance to access its properties.
// Here you create a new instance. You need to hang onto the destViewController
// pointer if you want to access its selectedButton property later. This means
// you'll need to use a global variable, or otherwise expose it in some way for
// future reference.
SlideShowViewController *destViewController = segue.destinationViewController;
[destViewController setSelectedButton:tagIndex];
Later, possibly in a different method, you could do something like:
// Note, we're referring to destViewController using self. I'm assuming you've
// set up a property on the self object to refer to destViewController outside
// of the scope of the initial initialization.
int selectedButtonIndex = self.destViewController.selectedButton;

Modify tableview (lied on another view controller ) based on table row selected

How to change the Images in table view which is lied in the new view controller based on row selected .
e.g. Say I hv category table in my Category view controller.Now when user taps on Camera,a new view controller should load containing all the camera images and on tap of any image,it should display its Detail.
how to implement this , as I am new to this tech.,Please help me ...
you can follow some simple steps to make it working.
1. First View: add the following code in your didSelectRowAtIndexPath method of tableview
-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//Fetch the value of the selected row in NSString
NSString *tableRowValue = cell.textLable.text;
//Call the next view
secondView *secondViewController = [[secondVIew alloc] initWithNibName:#"secondView" bundle:nil];
secondViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve ;
//Initialize the view with the clicked row value thwough NSString variable
[secondViewController initializeWith:tableRowValue];
}
2.Second View:
In your .h file, add the following initialization method:
-(void)initializeWith:(NSString *)sentRowValue;
IN your .m file, implement the initilization method:
-(void)initializeWith:(NSString*)sentRowValue
{
NSString *receivedRowValue = sentRowValue;
//take log to check
}
// now perform all the table view delegate methods using the value you have received.
NOTE: declare all the variables in your .h file and make them as property and synthesize them in your .m file
hope this helps

Resources