Skip to main content

How to pass the Value from UITextField from one VC to a string in another VC?



I have googled for this and even tried searching in many forums, also tried Singleton,etc but each time my 2nd VC'c string is returning a NULL value.








RETRIVALTVC is my VC in which i'm expecting for the Value and IRTViewController is my VC having the TextField.





I have imported all the header files.





This is my RetrivalTVC





RetrivalTVC.h







#import<UIKit>

NSString *string;

@property (nonatomic, copy) NSString *string;







In RetrivalTVC.m when i tried to read the value of string its returning NULL







- (void)viewDidLoad{

[self list];

}



- (NSMutableArray *)list{

NSLog(@"Value of string: %@",string);

}







This is my IRTViewController





IRTViewConroller.h







@property (weak, nonatomic) IBOutlet UITextField *searchTrain;

-(IBAction)search:(id)sender //Action when Button is Pressed







IRTViewController.m







-(IBAciton)search:(id)sender{

RetrivalTVC *retriv = [[RetrivalTVC alloc]init];

retriv.string = searchTrain.text;

//Here when i used NSLog its returning the value







Please Help.. Millions Thanks in advanced.. :)


Comments

  1. Have you created the property of the string in Retrival class or allocated it?

    ReplyDelete
  2. You have to create property in RetrivalTVC like :

    RetrivalTVC.h

    @property (weak, nonatomic) NSString *string;


    and synthesis it in RetrivalTVC.m file.

    ReplyDelete
  3. I think your retriv's scope maybe wrong.

    -(IBAciton)search:(id)sender{
    RetrivalTVC *retriv = [[RetrivalTVC alloc]init];
    retriv.string = searchTrain.text;
    //Here when i used NSLog its returning the value
    NSLog(@"%@",retriv.string);


    if this is right, you should check your code where you use retriv.string

    maybe this retriv is not equal that retriv.

    ReplyDelete
  4. I think you may be missing in synthesizing the variable RetrivalTVC.m class file.

    Change the property from :

    @property (nonatomic, copy) NSString *string;
    TO
    @property (nonatomic, retain) NSString *string;


    If all of solutions doesn't work , then why don't you prefer doing it using the delegate variable i.e Global.

    Just check out this, it will solve your problem :
    global variable read/write access

    ReplyDelete
  5. Your string is not initialized.
    Initialize it in your init of RetrivalTVC.m
    It will solve the error.
    better make it a property.

    Edit: do this .

    RetrivalTVC.h

    #import<UIKit>
    @interface

    NSString *string;//comment this line ..this line is not required.
    @property (nonatomic, copy) NSString *string;

    @end


    RetrivalTVC.m

    #import"RetrivalTVC.h"


    @implementation

    @synthesize string;
    // memory management

    - (void) dealloc
    {
    [super dealloc];
    [self.string release];
    }


    and update this in your your existing code

    - (void)viewDidLoad
    {
    [super viewDidLoad]; // the list method is not required
    }


    Now it will work

    ReplyDelete
  6. use @property (nonatomic, strong) NSString *string; instead of @property (nonatomic, copy) NSString *string;

    also use @property (strong, nonatomic) IBOutlet UITextField *searchTrain; instead `@property (week, nonatomic) IBOutlet UITextField *searchTrain

    Ok i found the error

    don't need to alloc string in viewDidLoad. what happens here you are setting value in string the push the view then viewDidLoad calls and it reallocate it and thus the value is nil

    ReplyDelete
  7. You should write your code like this.

    RetrivalTVC.h

    #import<UIKit>
    NSString *string;
    @property (nonatomic, copy) NSString *string;
    -(IBAction)getValue:(NSString *)val;


    In RetrivalTVC.m

    - (void)viewDidLoad{
    [self list];
    }

    -(IBAction)getValue:(NSString *)val
    {

    string=val;
    NSLog(@"Value of string: %@",string);
    }

    - (NSMutableArray *)list{
    string = [[NSString alloc]init];
    NSLog(@"Value of string: %@",string);
    }


    IRTViewConroller.h

    @property (weak, nonatomic) IBOutlet UITextField *searchTrain;
    -(IBAction)search:(id)sender //Action when Button is Pressed


    IRTViewController.m

    -(IBAciton)search:(id)sender{
    RetrivalTVC *retriv = [[RetrivalTVC alloc]init];
    [retriv getValue:searchTrain.text];
    retriv.string = searchTrain.text;

    ReplyDelete
  8. Method Names may spelled wrong

    Understanding


    IRTViewController - It has text filed, this value need to
    pass to RETRIVALTVC.


    Case 1. RETRIVALTVC has an object of IRTViewController, Use delegates.
    Case 2. When user Tap on button only you will create RETRIVALTVC in IRTViewController.

    Case 1 Soultion.
    RetrivalTVC.h

    #import<UIKit>
    NSString *string;
    @property (nonatomic, copy) NSString *string;


    RetrivalTVC.m

    - (void)viewDidLoad{
    [self list];
    IRTViewController * iRTViewController = [[IRTViewController alloc] init];
    iRTViewController.target = self;
    }

    - (NSMutableArray *)list{
    string = [[NSString alloc]init];
     NSLog(@"Value of string: %@",string);
    }


    IRTViewConroller.h

    @property (weak, nonatomic) IBOutlet UITextField *searchTrain;
    @property (nonatomic, assign) id target;
    -(IBAction)search:(id)sender //Action when Button is Pressed


    IRTViewController.m

    @synthesize target;

    -(IBAciton)search:(id)sender{
    RetrivalTVC *retriv =target;
    retriv.string = searchTrain.text;
    }


    Case 2: It has to work with your approach

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex