如何将textfiled值从一个视图传递到任何其他视图xcode [英] How to pass a textfiled value from one view to any other view xcode

查看:58
本文介绍了如何将textfiled值从一个视图传递到任何其他视图xcode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将UITextField值从一个视图传递到其他视图(第二个,第三个...视图)。实际上在我的第三个ViewController中我有一个scrollView,我必须在其上显示值。但是UITextField值没有得到传递。它返回null。不可能得到什么可能是错的?
这是我正在使用的代码:

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;
}

但是这里的id值没有传递给ViewController3.It返回null。 。我哪里出错?

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

推荐答案

您正在传递值而不进行初始化。

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; 

如果你想在你的应用程序中全局使用对象,你可以在appDelegate中声明它。

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

在AppDelegate.h中

In AppDelegate.h

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

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;

这篇关于如何将textfiled值从一个视图传递到任何其他视图xcode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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