[英]Instances in UIScrollView
I'm wondering about instances in UIScrollView. 我想知道UIScrollView中的实例。
I checked what is happening for the instances in UIScrollView just by writing a easy code. 我通过编写简单的代码检查了UIScrollView中实例的情况。
Here is the code. 这是代码。
UIViewController.m UIViewController.m
#import "ViewController.h"
#import "CustomScrollView.h"
@interface ViewController ()
@end
CustomScrollView *sv1, *sv2;
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
int height = self.view.frame.size.height;
sv1 = (CustomScrollView*)[[CustomScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, height/2)];
sv1.backgroundColor = [UIColor redColor];
[self.view addSubview:sv1];
[sv1 setIntegerWithNum:1];
sv2 = (CustomScrollView*)[[CustomScrollView alloc] initWithFrame:CGRectMake(0, height/2, 320, height/2)];
sv2.backgroundColor = [UIColor blueColor];
[self.view addSubview:sv2];
[sv2 setIntegerWithNum:2];
int returnVal1 = [sv1 getInteger];
int returnVal2 = [sv2 getInteger];
NSLog(@"Sv1:%d, Sv2:%d", returnVal1, returnVal2);
}
CustomScrollView.m CustomScrollView.m
#import "CustomScrollView.h"
int number;
@implementation CustomScrollView
-(void)setIntegerWithNum:(int)num {
number = num;
}
-(int)getInteger {
return number;
}
By this, I see the values in the output area. 这样,我在输出区域中看到了值。 I thought it will be "Sv1:1, Sv2:2" because the numbers are set as 1 and 2, respectively.
我以为它将是“ Sv1:1,Sv2:2”,因为数字分别设置为1和2。 But I got an output like "Sv1:2, Sv2:2"
但是我得到了类似“ Sv1:2,Sv2:2”的输出
What happened here? 这里发生了什么?
You have only one variable of type int
in your CustomScrollView.h
that your setting with your setter setIntegerWithNum
and then you are returning the value of " int
number" with your getter getInteger
. 你有型的只有一个变量
int
在CustomScrollView.h
您的设置与你的setter setIntegerWithNum
,然后你正在返回的“价值int
号码”与您的getter getInteger
。 essentially with your setter, you are setting your "number" variable twice. 本质上是使用您的设置员,您将两次设置“数字”变量。 The second time you set it with 2. so the variable
number
then holds 2, overwriting the previous value 1. 第二次将其设置为2,因此变量
number
保持为2,覆盖先前的值1。
Therefore when you use the getter for the NSLog
, you are returning the same value twice. 因此,当您将getter用于
NSLog
,将两次返回相同的值。
I would follow @Akhilrajtr's advice. 我会遵循@Akhilrajtr的建议。 simply set number as a property of type
int
. 只需将number设置为
int
类型的属性即可。 Then you will correctly return each object's property. 然后,您将正确返回每个对象的属性。 That is, each individual objects
int
property for sv1 and sv2. 即,sv1和sv2的每个对象的
int
属性。
You can do this with @property (nonatomic, assign) NSInteger *number;
您可以使用
@property (nonatomic, assign) NSInteger *number;
Try this, 尝试这个,
set number
as @property in CustomScrollView.h
by 在
CustomScrollView.h
中将number
设置为@property
@property(nonatomic, assign) int number;
then in ViewController
然后在
ViewController
sv1 = (CustomScrollView*)[[CustomScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, height/2)];
sv1.backgroundColor = [UIColor redColor];
[self.view addSubview:sv1];
[sv1 setNumber:1];
sv2 = (CustomScrollView*)[[CustomScrollView alloc] initWithFrame:CGRectMake(0, height/2, 320, height/2)];
sv2.backgroundColor = [UIColor blueColor];
[self.view addSubview:sv2];
[sv2 setNumber:2];
int returnVal1 = [sv1 number];
int returnVal2 = [sv2 number];
NSLog(@"Sv1:%d, Sv2:%d", returnVal1, returnVal2);
This is because you declared 这是因为您声明
int number;
not as an instance variable or property. 不作为实例变量或属性。 You can do this in two ways
您可以通过两种方式执行此操作
1. 1。
@interface CustomScrollView(){
int number;
}
@end
2. 2。
@interface CustomScrollView()
@property(nonatomic)int number;
@end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.