从另一个类设置UITextField.text [英] set UITextField.text from another class

查看:34
本文介绍了从另一个类设置UITextField.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图,并且我试图显示从另一个视图的 UITextField 中的第一个视图获得的文本.-()显示的第二个视图,因此ViewWillAppear和ViewDidLoad方法将无法使用.启动应用程序时,将运行第二个视图的viewDidLoad方法.

I have two views, and i'm trying to show text that i getting from first view in UITextField of another . Second view shown by - (source) so methods ViewWillAppear and ViewDidLoad won't work. And viewDidLoad method of second view is runs when app is started.

我试图制作第二类的方法

I'm tried to make method of second class

secondClass.h:

secondClass.h:

@property (strong, nonatomic) IBOutlet UITextField *itemName;//all hooked up in storyboard
-(void)SetName:(NSString *)name;

secondClass.m:

secondClass.m:

-(void)SetName:(NSString *)name{
    NSLog(@"%@",name);
    itemName.text = name;//itemName - textField
}

并在第一个中使用它:

secondViewConroller *secondView = [[secondViewConroller alloc]init];
[secondView SetName:@"Bill"];

NSlog 显示帐单",但 textField.text 不会更改任何内容.

NSlog shows "Bill" but textField.text won't change anything.

我猜该应用程序显示的UITextField没有更改,因为它显示了它是从viewDidLoad方法获取的第二个视图,我需要以某种方式对其进行更新

My guess that app shows UITextField without changes because it shows second view that it gets from viewDidLoad method and i need to update it somehow

我的问题:从不同类更改UI元素的属性的最佳方法是什么?

My question: What is the best approach to change attributes of UI elements from different classes?

推荐答案

所以我找到了一个解决方案:调用方法SetName出了点问题:我从第一个 UIViewController 获取参数.

So i found a solution: There's was something wrong with calling method SetName: with parameters that i getting from first UIViewController.

基本上,解决方案是:创建 NSObject 并从第一个 UIViewConroller 中放入值,然后在第二个中使用它.

Basically the solution is : create NSObject and put in there value from first UIViewConroller and then use it in second.

教程帮助我解决了问题.

这篇关于从另一个类设置UITextField.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆