在类之间传递NSString值 [英] Passing NSString value between classes

查看:128
本文介绍了在类之间传递NSString值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一个解决方案如何传递值从一个类到另一个。基本上,我通过@property在Settings类中创建一个NSString,它是模态显示。然后我根据选择的设置将NSString设置为一个特定的值,我想有一个类中显示的值,设置应该改变。我还通过@property在第二个类中声明一个字符串,并使用代码

  myController * controller = [[myController alloc ] 在里面]; 
secondClassString = controller.firstClassString



如果我NSLog的字符串,它显示第二类...
任何想法如何使它通过的价值?感谢

解决方案

假设您有两个视图控制器说 A B



{
NSString * strData;
int cId;
}

@property(nonatomic,retain)NSString * strData;
@property(readwrite)int cId;

现在在您的Am

  @synthesize strData,cId; 

您的Bh

  @class A 

{
A * aObj;
}

现在在您的Bm

  #importAh

- (void)viewDidLoad
{
aObj = [A alloc] init]; // alloc object A
[aObj setCId:10]; //为cId设置值
[aObj setStrData:@Hello from B]; //为strData设置值
//做什么
[aObj release]; //不要忘记
}

希望这会帮助你!


I have been looking long for a solution how to pass the value from one class to another. Basically I create a NSString via @property in a Settings class, which is displayed modally. I then set the NSString to a certain value depending on the settings chosen, and I want to have the value shown also in a class where the settings are supposed to make change. I also declare a string via @property in the second class, and I use the code

myController *controller = [[myController alloc] init];
secondClassString = controller.firstClassString

If I NSLog the string, it shows (null) in the second class... Any ideas how to make it pass the value? Thanks

解决方案

suppose you've two viewcontrollers say A & B

Your A.h

{
  NSString *strData;
  int cId;
}

@property (nonatomic, retain) NSString *strData;
@property (readwrite) int cId;

Now In your A.m

@synthesize strData,cId;

Your B.h

@class A

{
   A *aObj;
}

Now In your B.m

#import "A.h"

- (void) viewDidLoad
{
  aObj=[A alloc] init]; //alloc object of A
  [aObj setCId:10]; //set value for cId
  [aObj setStrData:@"Hello from B"]; //set value for strData
  //do what ever
  [aObj release]; //don't forget
}

Hope this helps you!

这篇关于在类之间传递NSString值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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