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

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.