0

I have to pass a UITextField value from one view to other views(2nd,3rd...views).Actually in my 3rd ViewController I have a scrollView and I have to display value on it .But UITextField value is not getting passed.It is returning null.Couldn't get what might be wrong? THis is the code I am working with:

In ViewController1.m:

-(IBAction)butonclick:(id)sender{
ViewController2 *view2=[ViewController2 alloc];
view2.id=name.text; 
ViewController3 *view3=[ViewController3 alloc];
view3.id=name.text; 
[view2 release];
[view3 release];
}


IN ViewConroller2.h :
@interface ViewController2 : UIViewController { 
   NSString *id;
   UIlabel *displayId;
}

In ViewController2.m :
- (void)viewDidLoad
{
 self.displayId.text=self.id;
}

In ViewController3.h:
@interface ViewController2 : UIViewController { 
  NSString *id;
  UIlabel *dispId;
 }  

In ViewController3.m :
- (void)viewDidLoad
{
self.dispId.text=self.id;
}

But here the id value is not passed to ViewController3.It is returning null ..Where I m going wrong?

Honey
  • 2,840
  • 11
  • 37
  • 71
  • how did you display the view 2 and view 3 ? – Midhun MP Oct 13 '12 at 11:06
  • please declare the string in app delegate and use it!! i think it's better way then you have used one – iDhaval Oct 13 '12 at 11:06
  • view2 is a UIview and in it Iam taking a UITableView and view3 is a UIView and it it I am taking UIScroolView and displaying TableView ,UITextFields,UILabels on it .How can I do it? – Honey Oct 13 '12 at 11:16

3 Answers3

0

Declare the string globally in the AppDelegate.h this will help in keeping the value of the string constant throughout the files. Also wherever you want to add the string or change its value or assign it import the AppDelegate.h .

Also check these links :-

passing NSString from one class to the other

Pass NSString from one class to another

Community
  • 1
  • 1
IronManGill
  • 7,222
  • 2
  • 31
  • 52
  • Could u please explain me briefly which string I have to declare in Appdelegate.h .But I have a UItextField and I have to pass it in 3rd view – Honey Oct 13 '12 at 11:37
  • Declare any NSString in Appdelegate.h then use it as a buffer to transfer values from one class to the other. You can take the value from the UITextField like textfield.text = String; but declare NSString String; before in Appdelegate.h – IronManGill Oct 14 '12 at 04:33
0

You are passing the values without initializing.

ViewController2 *view2=[[ViewController2 alloc]init];
view2.id=name.text; 
ViewController3 *view3=[[ViewController3 alloc]init];
view3.id=name.text; 

If you want to use object globally within your app, you can declare it in the appDelegate.

In AppDelegate.h

 @interface AppDelegate : NSObject <NSApplicationDelegate>
    {
         NSString *idGlobal;
    }
    @property (nonatomic, retain) NSString *idGlobal;

In AppDelegate.m

@synthesize idGlobal;

In ViewController1.m:

-(IBAction)butonclick:(id)sender{

     appDelegate.idGlobal=name.text; 
}

In ViewController2.m: and
In ViewController3.m:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
id=appDelegate.idGlobal;
Ramaraj T
  • 5,184
  • 4
  • 35
  • 68
0

I am just correcting the code you have written meanwhile the suggestions given above for using AppDelegate property is a good one. The main problem with your code is that you are just declaring NSString object instead of making it a property. Check this:

-(IBAction)butonclick:(id)sender{
ViewController2 *view2=[ViewController2 alloc]init];
view2.str=name.text; 
ViewController3 *view3=[ViewController3 alloc]init;
view3.str=name.text; 
[view2 release];
[view3 release];
}


IN ViewConroller2.h :
@interface ViewController2 : UIViewController { 
   NSString *str;
   UIlabel *displayId;
}
@property(nonatomic, retain) NSString* str; //Synthesize it in .m file

In ViewController2.m :
- (void)viewDidLoad
{
 self.displayId.text=self.str;
}

In ViewController3.h:
@interface ViewController2 : UIViewController { 
  NSString *str;
  UIlabel *dispId;
 }  
    @property(nonatomic, retain) NSString* str; //Synthesize it in .m file

In ViewController3.m :
- (void)viewDidLoad
{
self.dispId.text=self.str;
}

I am not aware with your scenario but the most effective way of implementing such situations is using Delegates. Make a delegate of the class where your string is being set (ViewController1) and set delegate accordingly in your other view controllers.

Animesh
  • 1,020
  • 9
  • 8