기본적으로 클래스의 각 인스턴스에 고유 한 ID를 부여하려고합니다.Objective-C : 정적 변수의 값을 인스턴스 변수에 할당
그래서 정적 정수를 만들었습니다. 나는 새로운 객체가 생성 될 때마다 그것을 증가시킨 다음 정적 변수의 값을 ivar에 할당합니다. 하지만 분명히 나는 어떤 것을 이해하지 못한다. 내가 세 개의 객체를 생성한다고 가정 해 보자. 인스턴스 변수 인 thisPageNumber는 내가 참조하는 객체에 상관없이 항상 3이다.
자세한 정보 :
이 클래스는 여러 "페이지"개체를 만듭니다. 각 페이지가 올바른 페이지 아트를 표시 할 수있을뿐만 아니라 다양한 다양한 작업을 수행 할 수 있도록 페이지 번호를 알고 싶습니다.
.H 부분 코드 :
@implementation Page
static int pageCount = 0;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
pageCount++;
thisPageNumber = pageCount;
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
CGRect defaultFrame = CGRectMake(0.0, 0.0, 1024.0, 768.0);
if (thisPageView == nil) {
thisPageView = [[UIImageView alloc]
initWithImage:[UIImage
imageNamed:[NSString stringWithFormat:@"Page%i.png", [thisPageNumber intValue]]]];
thisPageView.frame = defaultFrame;
[self.view addSubview:thisPageView];
}
if (nextPageView == nil && [thisPageNumber intValue] < BOOK_PAGE_COUNT) {
nextPageView = [[UIImageView alloc]
initWithImage:[UIImage
imageNamed:[NSString stringWithFormat:@"Page%i.png", [thisPageNumber intValue]+1]]];
nextPageView.frame = defaultFrame;
[self.view addSubview:nextPageView];
}
if (prevPageView == nil && [thisPageNumber intValue] > 1) {
prevPageView = [[UIImageView alloc]
initWithImage:[UIImage
imageNamed:[NSString stringWithFormat:@"Page%i.png", [thisPageNumber intValue]-1]]];
prevPageView.frame = defaultFrame;
[self.view addSubview:prevPageView];
}
}
확인 하시겠습니까? 현재 접속하고있는 코드를 표시 할 수 있습니까? –
또한 클래스의 인터페이스 – JeremyP
몇 가지 추가 소스 코드를 추가했습니다. – RyJ