【问题标题】:How to send the text from textfield to another class?如何将文本从文本字段发送到另一个类?
【发布时间】:2011-08-16 10:27:56
【问题描述】:

在我的应用程序 Iphone 中,我想做一个简单的事情,例如:我有一个 GroupDetailViewController 与一个文本字段和一个按钮。当用户按下按钮时,我想将文本从文本字段发送到另一个类 ItemViewController 并将此文本设置为标签。我不知道我该怎么做。我是 Iphone 新手,我只做了一些教程。我看过这里:How to send text field value to another class 但我不明白答案。谁能给我解释一下或者举个例子吗?

【问题讨论】:

    标签: iphone class textfield


    【解决方案1】:

    让我们假设您的第二个类是 SecondViewController。
    现在在 SecondViewController 中声明一个 NSString 并设置其属性。

    SecondViewController.h

    NSString *strTextValue;  
    
    ....  
    
    @property(nonatomic,retain) NSString *strTextValue; 
    

    SecondViewController.m

    @synthesize strTextValue;
    

    现在在 GroupDetailViewController 中,在按钮触摸事件中,将 textfield 中的值放入 strTextValue。

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

    将strTextValue 放入SecondViewController中创建的标签中

    SecondViewController.m

    lbl.text = strTextValue;
    

    【讨论】:

    • 我应该把 lbl.text = strTextValue;以及如何导航到其他班级?我知道这对您来说似乎微不足道,但我对此真的很陌生,我不知道在哪里放置这些行。
    • 把 lbl.text = strTextValue;在 viewDidLoad 中或者更好地放在 ViewWillAppear 中。
    • 您必须为此使用 UINavigationController。
    • 然后写[self.navigationController pushViewController:controller animated:YES];
    【解决方案2】:

    快速解决方案:

    创建一个共享应用程序委托并将值设置为委托中的字符串并重用它。 1)创建一个NSString变量说passVal并在youtAppDelegate文件中合成它。 2) 在 GroupDetailViewController 中

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

    3) 在 ItemViewController 中

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      • 2020-03-14
      • 1970-01-01
      • 2017-08-07
      • 1970-01-01
      相关资源
      最近更新 更多