Sharing simple data between views .. i'm missing something.

Discussion in 'iOS Programming' started by ViviUO, Nov 21, 2010.

  1. ViviUO macrumors 6502

    Joined:
    Jul 4, 2009
    #1
    So I have 2 views. Let's call them View 1 and View 2.

    View 1 has a UITextField named inputField and a button that opens a modal view, View 2. I also call [View 2 fillLabel], with fillLabel being a method declared in View 2.

    View 2 has a label on it, named outputLabel, and a method to set it's label as mentioned above.

    My View 1.m has an action assigned to it's button to display View 2 while at the same time calling [View 2 fillLabel].

    However, it is not working. I have tried several different ways:

    Code:
    ======
    View 1.h
    ======
    
    #import <UIKit/UIKit.h>
    #import "View2ViewController.h"
    
    @class View2ViewController;
    
    @interface View1ViewController : UIViewController {
    	IBOutlet View2ViewController *view2view;
    	IBOutlet UITextField *inputTextField;
    
    
    }
    
    @property(nonatomic, retain) IBOutlet View2ViewController *view2view;
    @property(nonatomic, retain) IBOutlet UITextField *inputTextField;
    
    -(IBAction)showModal:(id)sender;
    -(IBAction)hideKeyboard:(id)sender;
    
    @end
    
    ======
    View 1.m
    ======
    
    #import "View1ViewController.h"
    
    @implementation View1ViewController
    
    @synthesize inputTextField, view2view;
    
    -(IBAction)showModal:(id)sender {
    	view2view.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
    	[self presentModalViewController:view2view animated:YES];
    	
    	[view2view fillLabel]; 
    	
    }
    Code:
    ======
    View 2.h
    ======
    
    #import <UIKit/UIKit.h>
    #import "View1ViewController"
    
    @class View1ViewController;
    
    
    @interface View2ViewController : UIViewController {
    	IBOutlet UILabel *outputLabel;
    	
    	View1ViewController *view1;
    
    }
    
    @property (nonatomic, retain) UILabel *outputLabel;
    @property (nonatomic, retain) View1ViewController *view1;
    
    -(IBAction)resignModal:(id)sender;
    -(void)fillLabel;
    
    @end
    
    ======
    View 2.m
    ======
    
    #import "View2ViewController.h"
    #import "View1ViewController.h"
    
    @implementation View2ViewController
    
    @synthesize outputLabel, view1;
    
    -(IBAction)resignModal:(id)sender {
    	[self dismissModalViewControllerAnimated:YES];
    	
    }
    
    -(void)fillLabel {
    	
    	label.text=view1.inputTextField.text;
    	
    }
    Basically, I am having trouble in -(void)fillLabel.

    .... i even tried converting the text from the TextField into a string ... no luck.

    Am I missing something? Do I have to make a whole class just to share a single line of text?
     
  2. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #2
    Class View2ViewController has a member variable named "view1", but where do you initialize it? Same question about class View1ViewController and member variable "view2view".
     
  3. forum user macrumors regular

    Joined:
    Aug 28, 2008
    #3
    Try:

    Code:
    ======
    View 1.h
    ======
     
    -(IBAction)showModal:(id)sender {
    	view2view.modalTransitionStyle=UIModalTransitionStyleCoverVertical;
    	[view2view [fillLabel:inputTextField.text]]; 
    	[self presentModalViewController:view2view animated:YES];
    	
    	
    }
    Code:
    ======
    View 2.h
    ======
     
    
    -(void)fillLabel:(NSString *) suppliedString {
    	
    	[label setText:suppliedString ]; 
    
    }
     
  4. ViviUO, Nov 22, 2010
    Last edited: Nov 22, 2010

    ViviUO thread starter macrumors 6502

    Joined:
    Jul 4, 2009
    #4
    I use that later on and left it out for this question. :D

    This didn't seem to work either, I will have to keep playing with it I guess. :confused:

    This seems really easy to do. I guess I am just a special case.
     
  5. ianray macrumors 6502

    Joined:
    Jun 22, 2010
    Location:
    @
    #5
    OK, but it is generally better to post more detail -- to make it easier for everyone to help you :)
     

Share This Page