How to pass value to another view controller? - ios

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;

Related

access to property found with isMemberOfClass

How can I get access to a property of a ViewController when I have created an object of it like this:
for (UIViewController* vc in self.navigationController.viewControllers)
{
if ([vc isMemberOfClass:NSClassFromString(#"myViewController")])
{
// change property value on viewcontroller vc, for instance: vc.myText = #"hello" ??
}
}
thanks in advance!
You need to cast to let the compiler know the data type (or cheat and set it by an indirect method):
myViewController *mvc = (myViewController *)vc;
mvc.myText = #"hello";
Note also that class names should start with a capital first letter.
Just use simply like this:
(myViewController *)vc.myText = #"hello"
myText needs to be public variable also.

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

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.

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
}

Custom init or public properties What is the best way to send parameters between viewControllers

I would like to know which is the best way to send parameters between viewControllers. I know there are two possibilities, pass the parameters in the public properties after the init call.
ViewController *vc = [ViewController alloc] init];
vc.propertyOne = #"whatever";
vc.propertyTwo = #"whatever2";
Or creating a new Custom init like
initWithProperty:(NSString *)prperty1 andPropertyTwo:(NSString *)property2
{
self = [super init];
if (self) {
self.propertyOne = prperty1;
self.propertyTwo = property2;
}
return self;
}
ViewController *vc = [[ViewController alloc] initWithProperty:#"whatever andPropertyTwo:#"xxxx"];
I would like to know Advantages and disadvantages of each one, and "when" and "why" is better use one of them.
You should pass parameters to a custom init method if those values are needed immediately in the implementation of the init method.
You should use properties, that are set just after calling alloc/init, if you have several properties to set and those properties aren't needed in the init method itself.
Many times the properties aren't needed until viewDidLoad (in the case of a view controller) so using properties is cleaner. You don't want to end up with an init method with a dozen parameters.

Resources