Passing data from a label to another view controller - objective-c

what i have is when you select something is a uipickerview a label is than changed to the contents of the picker view. This works however I'm having trouble with taking the contents of the label in my pickerView.h and send it to set the contents of another label in my pickerDuration.h(secondViewController)

okay try this....
Have a NSString in ur SecondViewController and make sure u create a property of it (strong if u are using IOS 5 ARC) Synthesize it
You must have an object of SecondViewController for navigation purpose. Use the same object to set the value like this
secondViewObj.myStr = label.text;
make sure u do that before navigating to the next view
EDIT
Okay Try this
After performSegueWithIdentifier:sender: your view controler will call
assuming your new view controller has some propertys to set:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
if ([[segue identifier] isEqualToString:#"NextView"]) {
SecondViewController *myVC = [segue destinationViewController];
myVC.mystr= label.text;
}

Create on object of SecondViewController in your currentViewController which has the label. Declare a string in SecondViewController.
Init secondViewController object in your currentViewController and set its NSString to label's text. Something like this
secondViewController.string = label.text;

Related

didSelectRowAtIndexPath, pushViewController and a little lable

I have a table controller in which I use didSelectRowAtIndexPath to navigate from the pushed cell to another view. In it I initialize new view controller and push some data in it. After that I do pushViewController.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
// Navigation logic may go here. Create and push another view controller.
ServicesModel *service = [services objectAtIndex:indexPath.row];
ServiceViewController *serviceViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"ServiceView"];
serviceViewController.serviceModel = service;
NSLog(#"Set model %#", service.title);
// Pass the selected object to the new view controller.
[self.serviceController pushViewController:serviceViewController animated:YES];
}
In my ServiceViewController I have a label serviceTitle and ServiceModel property for selected service
#property (weak, nonatomic) IBOutlet UILabel *serviceTitle;
#property (strong, nonatomic) ServiceModel *serviceModel;
Using viewDidLoad I'm trying to change text of the label
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"viewDidLoad %#", self.serviceModel.title);
self.serviceTitle.text = self.serviceModel.title;
}
Also I'm trying to access model in viewDidAppear
- (void) viewDidAppear:(BOOL)animated
{
NSLog(#"viewDidAppear %#", self.serviceModel.title);
}
but when view opens, label is empty. Why? What am I doing wrong? The most strange is the log:
(-[ServiceViewController viewDidLoad]) (ServiceViewController.m:43) viewDidLoad (null)
(-[ServicesTableViewController tableView:didSelectRowAtIndexPath:]) (ServicesTableViewController.m:127) Set model Google.com
(-[ServiceViewController viewDidAppear:]) (ServiceViewController.m:36) viewDidAppear (null)
It shows that viewDidLoad fires before I assign the model property. And in viewDidAppear model property is still null. How it can be?
You have two problems. The first one, as 0x7fffffff mentioned, is that you're instantiating your controller incorrectly (it should be initWithNibName:bundle: if made in a xib, and like 0x7fffffff said if in a storyboard).
Second, you can't access the label in serviceViewController from didSelectRowAtIndexPath, because its view has not been loaded yet. So, instead of setting the label in didSelectRowAtIndexPath, you should have a string property in serviceViewController, and give it the value service.text. Then in viewDidLoad, you can populate your label with that string.
Is the label missing altogether, or do you see it and it just didn't receive updated text? If the label is missing, then it's probably a problem in how you're creating the view controller. If for example, you're using storyboards, you should be accessing the view controller like this:
ServiceViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeStoryBoardID"];
Instead of this:
ServiceViewController *serviceViewController = [[ServiceViewController alloc] init];
If however, you can see the label, but it just hasn't updated it's text, the first thing you should to is examine the connections inspector in Interface Builder, and verify that the IBOutlet for the label is properly linked.

send image between views using prepareForSegue

I am working on an Iphone application using storyboard.
I have a button in a view (SelectionViewController) and when the user clicks on the button, it goes to another view (ImagesResultsViewController) (.
What I need is to send an image from the first view to the second. for that I named the segue identifier "resultViewSegue" and used the prepareForSeague method to get reference to the destination view and place the image in a UIImage view on it:
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
//if the segue identifier is "resultViewSegue"
if([segue.identifier isEqualToString:#"resultViewSegue"])
{
//get the destination view which is ImagesResultsViewController
ImagesResultsViewController * destinationView = segue.destinationViewController;
//Set the image in the UIImageView
[destinationView.mainImageView setImage:myImageToSend];
//mainImageView is a UIImageView in the "ImagesResultsViewController" I created a property for it and synthesize it
}
}
However the image was not set. after debugging I found out that the destinationView.mainImageView is null. How can I get a reference of the UIImageView and set its picture from the segue?
Thank a lot for any help
create a UIImage property in the ImagesResultsViewController
In the prepareForSegue method set it to the image you want and then in the viewdidload
set it in the UIImageView

Passing Objects using Segue

I have trouble understanding segues and how they work and pass objects. Basically I have a calculator and am trying to graph the objects stored in an array. So far I have an object called brain which is an instance of CalculatorBrain. Now brain has an NSArray property that I use as a stack to store variables. Let's say I add the values 3 and 5 to the array and then want to segue. I have my segue selected to a button called "Graph" so when I click the button it segues. How would I pass brain to the new view controller that I am segueing to? I have a property called setGraphingPoint which is defined in the new view controller which I think should accept the passed object. Also, if I pass brain through a segue will the values 3 and 5 be passed along with it or will it create a new object of CalculatorBrain? Here is what I have so far.
This is defined in the new view controller
#property (nonatomic, strong) CalculatorBrain *graphingPoint;
#synthesize graphingPoint = _graphingPoint;
-(void) setGraphingPoint:(CalculatorBrain*) graphingPoint{
_graphingPoint = graphingPoint;
[self.graphingView setNeedsDisplay];
}
This is called from the old view controller which will have button to segue
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if([segue.identifier isEqualToString:#"Graph"])
[segue.destinationViewController setGraphingPoint:[self.brain program]];
You can use protocols. For instance, you can have your prepareForSegue look like this:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
id destination = segue.destinationViewController;
if([destination conformsToProtocol:#protocol(GraphPointUsing)])
[destination setGraphingPoint:[self.brain program]];
}
Then you just have to make sure the the ViewController that you are segueing to conformist to GraphPointUsing.
If you do not want to use protocols, but you still want to call methods on GraphPoint you can do this:
//In new ViewController suppose we want to call the method `foo` on `GraphPoint`
[self.graphingPoint foo];
//Or if we want to call a setter we can do
[self.graphingPoint setFoo:5];

Objective-C (iOS): prepareForSegue won't pass my data into destination VC

VC1 = NewGameViewController
VC2 = GameViewController
NewGameViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if( [segue.identifier isEqualToString:#"newGameSegue"]) {
GameViewController *gameVC = (GameViewController *)segue.destinationViewController;
NSArray *array = [self nameArrayForTextFieldArray:self.namePicker.textFieldArray withColon:YES];
gameVC.nameArray = [[NSArray alloc] initWithArray:array];
}
-(NSArray *)nameArrayForTextFieldArray:(NSArray *)array withColon:(BOOL *)bool
basically returns an nsarray of strings given an nsarray of textfields. withcolon is a bool of whether or not you want the strings to have a colon appended to the end.
when i debug my code, the _nameArray ivar in gameVC still reads nil after every line here is called...can anyone help me out here??
The prepareForSegue method is invoked by UIKit when a segue from one screen to another is about to be performed. It allows us to give data to the new view controller before it will be displayed. Usually you’ll do that by setting its properties.
The new view controller can be found in segue.destinationViewController. If GameViewController embed the navigation controller, the new view controller will not be GameViewController but the navigation controller that embeds it.
To get the GameViewController object, we can look at the navigation controller’s topViewController property. This property refers to the screen that is currently active inside the navigation controller.
To send an object to the new view controller you can use this solution using performSegueWithIdentifier:
For example, if we want to perform a segue pressing a UIButton we can do this:
In the MyViewController.h we create a IBAction (connected to UIButton), dragging the button from storyboard to code:
- (IBAction)sendData:(id)sender;
In MyViewController.m we implement the method:
- (IBAction)sendData:(id)sender
{
NSArray *array = [self nameArrayForTextFieldArray:self.namePicker.textFieldArray withColon:YES];
[self performSegueWithIdentifier:#"newGameSegue" sender:array];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"newGameSegue"]) {
UINavigationController *navigationController = segue.destinationViewController;
GameViewController *controller = (GameViewController *)navigationController.topViewController;
controller.nameArray = sender;
}
}
Is GameViewController embedded in a navigation controller? In that case, your destinationViewController property is of type UINavigationController, not GameViewController. You can get to GameViewController by calling [segue.destinationViewController.viewControllers lastObject].
I'm assuming that you've done a NSLog (or examine it in the debugger) of array immediately before setting gameVC.nameArray. You really want to make sure it's being set the way you think it is. It's amazing how many times I've spent debugging something like this only to realize the problem was in my equivalent to nameArrayForTextFieldArray. Or a typo in the name of the segue identifier. Or random things like that.
Assuming that's ok, then a couple of things are possible:
How is your nameArray property defined in GameViewController? If it's not a strong reference (or a copy), then when your array falls out of scope, it will be deallocated. I think this would manifest itself slightly differently, but it's worth confirming.
Also, I've seen situations where a controller like GameViewController might have some confusion between various ivars and properties (which is why I never define ivars for my properties ... I let #synthesize do that).
I assume you're not using a custom setter for nameArray. I just want to make sure. If so, though, please share that, too.
Bottom line, can you show us all references to nameArray in your #interface of GameViewController as well as in its #synthesize statement?

Simplest way to pass property to a UITableViewController

I have two UITableViewControllers (hooked up to a UINavigationController). When I click on a UITableViewCell on the first controller, I created a segue so that the second controller comes into view. I want to have a variable in the second view controller that contains the text on the that was selected. How do I do this? I have tried passing it directly, but it doesn't work for some reason.
Declare an property (like selectedText - an NSString) on the receiving viewcontroller (in this case we'll call it SecondTableViewController). Here is the code:
In SecondTableViewController.h:
#interface SecondTableViewController : UITableViewController {
}
#property(nonatomic, strong) NSString *selectedText;
In SecondTableViewController.m:
#implementation SecondTableViewController
#synthesize messageDetail;
In your FirstTableViewController.m:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure we are dealing with the proper Segue
if ([segue.identifier isEqualToString:#"idOfMySegue"]) {
SecondTableViewController *svc = segue.destinationViewController;
svc.selectedText = myVarValue; // This can be got from either setting another var in the tableviewcontroller or by just passing the entire object at the selected index of the NSIndexPath.row (if you are populating the tableview with an array. Note you would change the object type of the passed along object from an NSString to whatever the other object is.
}
}
The prepareForSeque method is where you pass along any data or objects from one view to another.