如何将数组、字符串或任何包含数据的对象调用到另一个类 - iPhone
我的应用程序中有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
很容易。您可以在 AppDelegate 中分配此数组,然后从 UIView 中调用它。或者您可以在您的 UIView 之一中分配它,并将指向该数组的点设置到 AppDelegate 中。
示例:
就是这样
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:
Thats it
您确定为 view1 中的变量赋予初始值吗?
请确保 view1 的 init 方法为参数提供了一些值,即
确保您有:。
顺便说一句,按照您的方式初始化数组是错误的代码,您应该使用 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
make sure you have:.
by the way, it is wrong code to initialize array the way you did, you should use one of NSArray initialize methods.