如何将数组、字符串或任何包含数据的对象调用到另一个类 - iPhone

发布于 2024-12-27 22:43:46 字数 903 浏览 2 评论 0原文

我的应用程序中有 2 个课程。查看1 &视图2; 我想在另一个类中使用像数组、字符串、label.text 这样的对象 当我使用它们时,显示 null ;

View1.h:
{
NSMutableArray *array;
IBOutlet UILabel *lbl;
NSString *str;
}

@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain)IBOutlet UILabel *lbl;
@property(nonatomic,retain) NSString *str;

@end;

View1.m

@synthesize array;
@synthesize lbl;
@synthesize str;


array = (1,2,3,..., nil) some dataa
str = @"HAI";
lbl.text = @"Text in label aaaa";

NSLog( @" %@", array );
NSLog( @" %@", lbl.text );
NSLog( @" %@", str );

给出正确的输出,

但在 View2:

@implementation View2
    #import "View1"
    .
..
....
View1 *one = [View1 alloc]initwit.....................];

NSLog( @" %@", one.array );
NSLog( @" %@", one.lbl.text );
NSLog( @" %@", one.str );

为什么打印空值

?该怎么办?

提前致谢..

i have 2 classes in my app. View1 & View2;
i want to use an object like array, strin, label.text in another class
when i used them the shows null ;

View1.h:
{
NSMutableArray *array;
IBOutlet UILabel *lbl;
NSString *str;
}

@property(nonatomic,retain) NSMutableArray *array;
@property(nonatomic,retain)IBOutlet UILabel *lbl;
@property(nonatomic,retain) NSString *str;

@end;

View1.m

@synthesize array;
@synthesize lbl;
@synthesize str;


array = (1,2,3,..., nil) some dataa
str = @"HAI";
lbl.text = @"Text in label aaaa";

NSLog( @" %@", array );
NSLog( @" %@", lbl.text );
NSLog( @" %@", str );

gives correct out put

but in
View2:

@implementation View2
    #import "View1"
    .
..
....
View1 *one = [View1 alloc]initwit.....................];

NSLog( @" %@", one.array );
NSLog( @" %@", one.lbl.text );
NSLog( @" %@", one.str );

prints null value

why ? what to do?

thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶底世界 2025-01-03 22:43:46

很容易。您可以在 AppDelegate 中分配此数组,然后从 UIView 中调用它。或者您可以在您的 UIView 之一中分配它,并将指向该数组的点设置到 AppDelegate 中。
示例:

//UIView 1 
NSArray *array = [[NSArray alloc] init];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.mySharedArray = array;

//UIView2
 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
 NSArray *array = appDelegate.mySharedArray;

就是这样

Very easy. You can allocate this array in your AppDelegate, and then call it from your UIViews. Or you can allocate it in one of your UIViews and set point to this array into AppDelegate.
Example:

//UIView 1 
NSArray *array = [[NSArray alloc] init];
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.mySharedArray = array;

//UIView2
 MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
 NSArray *array = appDelegate.mySharedArray;

Thats it

别再吹冷风 2025-01-03 22:43:46

您确定为 view1 中的变量赋予初始值吗?

请确保 view1 的 init 方法为参数提供了一些值,即

initwit..............

确保您有:。

array = (1,2,3,..., nil) some dataa
str = @"HAI";
lbl.text = @"Text in label aaaa";

顺便说一句,按照您的方式初始化数组是错误的代码,您应该使用 NSArray 初始化方法之一。

Are you sure you give initial values to the variables from view1?

please make sure the init method for view1 gives some values to the argument, i.e. in

initwit..............

make sure you have:.

array = (1,2,3,..., nil) some dataa
str = @"HAI";
lbl.text = @"Text in label aaaa";

by the way, it is wrong code to initialize array the way you did, you should use one of NSArray initialize methods.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文