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.. :)
Have you created the property of the string in Retrival class or allocated it?
ReplyDeleteYou have to create property in RetrivalTVC like :
ReplyDeleteRetrivalTVC.h
@property (weak, nonatomic) NSString *string;
and synthesis it in RetrivalTVC.m file.
I think your retriv's scope maybe wrong.
ReplyDelete-(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.
I think you may be missing in synthesizing the variable RetrivalTVC.m class file.
ReplyDeleteChange 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
Your string is not initialized.
ReplyDeleteInitialize 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
use @property (nonatomic, strong) NSString *string; instead of @property (nonatomic, copy) NSString *string;
ReplyDeletealso 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
You should write your code like this.
ReplyDeleteRetrivalTVC.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;
Method Names may spelled wrong
ReplyDeleteUnderstanding
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