【问题标题】:How to pass a textfiled value from one view to any other view xcode如何将文本字段值从一个视图传递到另一个视图 xcode
【发布时间】:2012-10-15 22:27:16
【问题描述】:

我必须将 UITextField 值从一个视图传递到其他视图(2nd,3rd...views)。实际上,在我的第三个 ViewController 中,我有一个 scrollView,我必须在其上显示值。但是 UITextField 值没有得到已通过。它返回 null。不知道可能出了什么问题? 这是我正在使用的代码:

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。它返回 null ..我哪里出错了?

【问题讨论】:

  • 你是如何显示视图 2 和视图 3 的?
  • 请在app delegate中声明字符串并使用!!我认为这比你用过的方法更好
  • view2 是一个 UIview,我在其中使用 UITableView,view3 是一个 UIView,我正在使用 UIScroolView 并在其上显示 TableView、UITextFields、UILabels。我该怎么做?

标签: iphone xcode


【解决方案1】:

您在没有初始化的情况下传递值。

ViewController2 *view2=[[ViewController2 alloc]init];
view2.id=name.text; 
ViewController3 *view3=[[ViewController3 alloc]init];
view3.id=name.text; 

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

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

【讨论】:

  • 你的意思是我必须在 AppDelegate.h 中声明 id 和 idGlobal ?
  • 我的意思是在AppDelegate中声明idGlobal。请参阅我的更新答案。
  • 嘿,我做了和你说的一样的事,但是在这一行得到了错误'Expected identifier or '(': id=appDelegate.idGlobal;你能告诉我吗?
  • NSString *myStr=appDelegate.idGlobal;不要使用 'id' 来声明变量。因为 'id' 是一种数据类型。
  • k 谢谢你的帮助..明白了
【解决方案2】:

在AppDelegate.h 中全局声明字符串,这将有助于在整个文件中保持字符串的值不变。同样,无论您要添加字符串或更改其值或分配它,都可以导入 AppDelegate.h 。

还要检查这些链接:-

passing NSString from one class to the other

Pass NSString from one class to another

【讨论】:

  • 能否请您简要解释一下我必须在 Appdelegate.h 中声明的字符串。但我有一个 UItextField,我必须在第三个视图中传递它
  • 在 Appdelegate.h 中声明任何 NSString,然后将其用作缓冲区以将值从一个类传输到另一个类。您可以从 UITextField 中获取值,例如 textfield.text = String;但声明 NSString 字符串;之前在 Appdelegate.h
【解决方案3】:

我只是在更正您编写的代码,同时上面给出的使用 AppDelegate 属性的建议是一个很好的建议。 您的代码的主要问题是您只是声明 NSString 对象而不是使其成为属性。检查这个:

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

我不知道您的情况,但实现这种情况的最有效方法是使用委托。为设置字符串的类(ViewController1)创建一个委托,并在其他视图控制器中相应地设置委托。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多