How to send the text from textfield to another class?

青春壹個敷衍的年華 提交于 2019-12-11 07:46:49

问题


In my app Iphone, I want to do a simple thing like : I have a GroupDetailViewController with a text Field and a button. When user press the button I want to send the text from textfield to another class ItemViewController and set this text to a label. I have no idea how can I do this. I am new on Iphone,I have done just some tutorials. I have looked here : How to send text field value to another class but I dont understand the answer. Can anyone explain me or give me an example?


回答1:


Let us assume that your second class is SecondViewController.
Now in SecondViewController declare one NSString and set its properties.

SecondViewController.h

NSString *strTextValue;  

....  

@property(nonatomic,retain) NSString *strTextValue; 

SecondViewController.m

@synthesize strTextValue;

Now in GroupDetailViewController, on button touch event, put the value from textfield in strTextValue.

-(IBAction)ButtonMethod:(id)sender
{
SecondViewController *controller = [[SecondViewController alloc]init];
controller.strTextValue = [txtField text];  
//Navigate to SecondViewController
}  

Put strTextValue in label created in SecondViewController

SecondViewController.m

lbl.text = strTextValue;



回答2:


Quick Solution:

Create a shared application delegate and set the value to a string in delegate and reuse it. 1) create an NSString variable say passVal and synthesize it in youtAppDelegate file. 2) In GroupDetailViewController

yourAppDelegate *del=[[UIApplication SharedApplication]delegate];
del.passVal=textField.text;

3) In ItemViewController

yourAppDelegate *del=[[UIApplication SharedApplication]delegate];
label.text=del.passVal;


来源:https://stackoverflow.com/questions/7076762/how-to-send-the-text-from-textfield-to-another-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!