How to pass a textfiled value from one view to any other view xcode

前端 未结 3 446
北海茫月
北海茫月 2021-01-26 10:53

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 UIT

3条回答
  •  佛祖请我去吃肉
    2021-01-26 11:33

    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.

提交回复
热议问题